是的,find ./work -print0 | xargs -0 rm
将执行类似的操作rm ./work/a "work/b c" ...
。您可以使用进行检查echo
,find ./work -print0 | xargs -0 echo rm
将打印将要执行的命令(空格将被适当地转义,尽管echo
不会显示出来)。
为了xargs
将名称放在中间,您需要添加-I[string]
,在哪里[string]
要用参数替换,在这种情况下,您将使用-I{}
,例如<strings.txt xargs -I{} grep {} directory/*
。
您实际要使用的是grep -F -f strings.txt
:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by
newlines, any of which is to be matched. (-F is specified by
POSIX.)
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. (-f is
specified by POSIX.)
因此,grep -Ff strings.txt subdirectory/*
将找到所有出现的字符串strings.txt
作为文字,如果您删除该-F
选项,则可以在文件中使用正则表达式。您实际上grep -F "$(<strings.txt)" directory/*
也可以使用。如果您想练习find
,可以使用摘要中的最后两个示例。如果您想进行递归搜索而不是仅仅进行一级搜索,那么在摘要中也有一些选择。
摘要:
# grep for each string individually.
<strings.txt xargs -I{} grep {} directory/*
# grep once for everything
grep -Ff strings.txt subdirectory/*
grep -F "$(<strings.txt)" directory/*
# Same, using file
find subdirectory -maxdepth 1 -type f -exec grep -Ff strings.txt {} +
find subdirectory -maxdepth 1 -type f -print0 | xargs -0 grep -Ff strings.txt
# Recursively
grep -rFf strings.txt subdirectory
find subdirectory -type f -exec grep -Ff strings.txt {} +
find subdirectory -type f -print0 | xargs -0 grep -Ff strings.txt
-l
如果您不需要查看实际的行,则可能需要使用该选项来获取每个匹配文件的名称:
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)