0

使用find我创建了一个文件,其中包含使用特定关键字的所有文件:

find . -type f | xargs grep -l 'foo' > foo.txt

我想在 foo.txt 中获取该列表,并可能使用该列表运行一些命令,即ls在文件中包含的列表上运行命令。

4

2 回答 2

3

您不需要xargs创建foo.txt. 只需-exec像这样执行命令:

find . -type f -exec grep -l 'foo' {} \; > foo.txt

然后你可以ls通过循环文件来运行文件:

while IFS= read -r read file
do
   ls "$file"
done < foo.txt

也许它有点难看,但这也可以使它:

ls $(cat foo.txt)
于 2013-09-19T15:50:14.327 回答
2

你可以xargs这样使用:

xargs ls < foo.txt

xargs 的优点是它将执行带有多个参数的命令,这比使用循环对每个参数执行一次命令更有效,例如。

于 2013-09-19T15:54:02.900 回答