xargs/find/grep 目录列表中的文件列表
xargs/find/grep a list of files from a list of directories
我在一个文本文件中有一个目录列表,我想在其中查找和更改共享特定命名约定的文件。
示例文件:
dir1
dir2
dir3
包含文件的示例文件夹结构
dir1/
thing.txt
thing.blah
dir2/
rar.txt
thing.blah
dir3/
apple.txt
another.text.file.txt
thing.blah
首先,我找到了 .txt 的名称,但随后我想对其进行更改。例如,我想在 thing.txt、rar.txt 和 apple.txt 上执行 sed 命令,而不是 another.text.file.txt.
我的问题是,获得所有文件名后,如何对具有这些名称的文件执行命令?我怎样才能把这些文本行如:
cat dirFile.txt | xargs ls | grep <expression>.txt
thing.txt
rar.txt
apple.txt
!cat | some command
和运行对目录下的实际文件的操作?
我得到的是上面的结果,
但我需要的是
dir1/thing.txt
dir2/rar.txt
dir3/apple.txt
假设您有一个名为 dirs
的文件,其中包含您需要搜索的所有目录:
while IFS= read -r i; do
find "$i" -name '<expression>' -print0
done < dirs | xargs -0 some_command
如果您知道目录没有空格或其他类型的分隔符,您可以稍微简化一下:
find $(<dirs) -name '<expression>' -print0 | xargs -0 some_command
也许您的 some_command
期望一次只有一个文件,在这种情况下使用 -n1
:
... | xargs -0 -n1 some_command
或移动some_command
找到自己:
find $(<dirs) -name '<expression>' -exec some_command {} \;
$(<dirs)
是一个 comand substitution。它读取 dirs
文件的内容(如 cat
)并将其用作 find
的第一个参数。空的 dirs
在 GNU find 上是安全的(例如 Linux),但在 BSD 上您至少需要一行 - 它被转换为一个参数(例如 Mac OS X)
-print0
用 null
分隔文件
-0
需要这些 null
个字符。
-n1
说 xargs
只发送一个参数给 some_command
我想我的评论没有得到充分表达。得到你想要的输出;
cat dirfile.txt | xargs -I % find % -name <your file spec> or -regex <exp>
我在一个文本文件中有一个目录列表,我想在其中查找和更改共享特定命名约定的文件。
示例文件:
dir1
dir2
dir3
包含文件的示例文件夹结构
dir1/
thing.txt
thing.blah
dir2/
rar.txt
thing.blah
dir3/
apple.txt
another.text.file.txt
thing.blah
首先,我找到了 .txt 的名称,但随后我想对其进行更改。例如,我想在 thing.txt、rar.txt 和 apple.txt 上执行 sed 命令,而不是 another.text.file.txt.
我的问题是,获得所有文件名后,如何对具有这些名称的文件执行命令?我怎样才能把这些文本行如:
cat dirFile.txt | xargs ls | grep <expression>.txt
thing.txt
rar.txt
apple.txt
!cat | some command
和运行对目录下的实际文件的操作?
我得到的是上面的结果,
但我需要的是
dir1/thing.txt
dir2/rar.txt
dir3/apple.txt
假设您有一个名为 dirs
的文件,其中包含您需要搜索的所有目录:
while IFS= read -r i; do
find "$i" -name '<expression>' -print0
done < dirs | xargs -0 some_command
如果您知道目录没有空格或其他类型的分隔符,您可以稍微简化一下:
find $(<dirs) -name '<expression>' -print0 | xargs -0 some_command
也许您的 some_command
期望一次只有一个文件,在这种情况下使用 -n1
:
... | xargs -0 -n1 some_command
或移动some_command
找到自己:
find $(<dirs) -name '<expression>' -exec some_command {} \;
$(<dirs)
是一个 comand substitution。它读取dirs
文件的内容(如cat
)并将其用作find
的第一个参数。空的dirs
在 GNU find 上是安全的(例如 Linux),但在 BSD 上您至少需要一行 - 它被转换为一个参数(例如 Mac OS X)-print0
用null
分隔文件
-0
需要这些null
个字符。-n1
说xargs
只发送一个参数给some_command
我想我的评论没有得到充分表达。得到你想要的输出;
cat dirfile.txt | xargs -I % find % -name <your file spec> or -regex <exp>