使用“查找”在 bash 中过滤多个匹配项
可能的重复:
Unix“find”命令用法
这是一个 bash 安装脚本。脚本 foo.sh 将“DIRECTORY”作为参数。假设有一个目录 /TEST/TEST_1A/TEST_2A/TEST_3 和另一个目录 /TEST/TEST_1B/TEST_2B/TEST_3。
脚本:简单来说是 foo.sh。
DIR=`find $HOME -type d -name $1 | head 1'
if [ DIR is set to a directory ]
then
rm -rf $DIR
用法:foo.sh TEST_3
现在,从脚本中只能删除 /TEST/TEST_1A/TEST_2A/TEST_3。要删除 /TEST/TEST_1B/TEST_2B/TEST_3,我需要在 find 命令中使用 reg exp 来微调删除以解决目录冲突。
修改了上述脚本的find部分,如下
DIR='find $HOME -type d -regexp $1 | head 1
新用法:foo.sh TEST_2B/TEST_3
但是“find”命令无法将DIR设置为/TEST/TEST_1B/ TEST_2B/TEST_3 并返回空 &结果 DIR 为空,我永远无法删除 /TEST/TEST_1B/TEST_2B/TEST_3
如何更改脚本,以便 find 可以仅作用于目录名称,以及目录的路径,而没有问题。事实上,一些用户可能会给出部分目录路径作为“foo.sh”的参数。我希望“foo.sh”能够工作,即使在这种情况下
Possible Duplicate:
Unix “find” command usage
This is for a bash installation script. The script foo.sh takes "DIRECTORY" as an argument. Say, there is a dir /TEST/TEST_1A/TEST_2A/TEST_3 and another dir /TEST/TEST_1B/TEST_2B/TEST_3.
Script: foo.sh in brief.
DIR=`find $HOME -type d -name $1 | head 1'
if [ DIR is set to a directory ]
then
rm -rf $DIR
Usage: foo.sh TEST_3
Now from the script, only the /TEST/TEST_1A/TEST_2A/TEST_3 can be removed. To remove /TEST/TEST_1B/TEST_2B/TEST_3, I need to use a reg exp in my find command, to fine tune the remove to resolve the directory conflict.
Modified the find part of the above script as below
DIR='find $HOME -type d -regexp $1 | head 1
NEW Usage: foo.sh TEST_2B/TEST_3
But "find" command FAILS to get the DIR set to /TEST/TEST_1B/TEST_2B/TEST_3 and instead returns empty & as a result DIR is empty and I can never ever remove /TEST/TEST_1B/TEST_2B/TEST_3
How do I change the script, so that find can act on JUST the directory name, as well as on the path to the directory too with NO issues. Infact, some users may give a partial directory path as argument to "foo.sh". I expect "foo.sh" to work, even in such cases
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我这样做,我只需将其全部链接起来:
仅当所有目录位于包含“TEST_A”的路径上时,这才会删除所有目录
If I were doing this, I would simply chain it all up:
This will delete all directories only if they are on a path that contains "TEST_A"