cat-ing 子目录中某些扩展名的文件
cat-ing files of certain extensions in subdirectories
我有一个目录 parallel/
,其中的子目录包含以 *.en
扩展名结尾的文件。
这样做会得到我需要的文件列表。
find parallel/ -name "*.en" -type f
现在我需要 cat
所有这些文件来得到一个组合文件,即
cat *.en > all.en
我尝试了以下方法,但没有用:
$ for i in (find parallel/ -name "*.en" -type f): do cat $i ; done
-bash: syntax error near unexpected token `('
$ for i in ((find parallel/ -name "*.en" -type f)): do cat $i ; done
-bash: syntax error near unexpected token `('
有没有办法让我遍历所有子目录并将它们"cat"全部放入一个文件中?
你们很亲近;只是缺少美元符号。
使bash计算一个命令并得到输出;使用 $()
:
for i in $(find parallel/ -name "*.en" -type f); do cat $i ; done
$()
等同于,但比旧的靠背
更好更安全
var=`cmd` #do not use!
您可以使用 -exec
选项在 find
本身中调用 cat
:
find parallel/ -name "*.en" -type f -exec cat {} +
要将其重定向到文件,请使用:
find parallel/ -name "*.en" -type f -exec cat {} + > all.en
根据man find
:
-exec utility [argument ...] {} +
Same as -exec, except that ``{}'' is replaced with as many pathnames as possible
for each invocation of utility. This behaviour is similar to that of xargs(1).
我有一个目录 parallel/
,其中的子目录包含以 *.en
扩展名结尾的文件。
这样做会得到我需要的文件列表。
find parallel/ -name "*.en" -type f
现在我需要 cat
所有这些文件来得到一个组合文件,即
cat *.en > all.en
我尝试了以下方法,但没有用:
$ for i in (find parallel/ -name "*.en" -type f): do cat $i ; done
-bash: syntax error near unexpected token `('
$ for i in ((find parallel/ -name "*.en" -type f)): do cat $i ; done
-bash: syntax error near unexpected token `('
有没有办法让我遍历所有子目录并将它们"cat"全部放入一个文件中?
你们很亲近;只是缺少美元符号。
使bash计算一个命令并得到输出;使用 $()
:
for i in $(find parallel/ -name "*.en" -type f); do cat $i ; done
$()
等同于,但比旧的靠背
var=`cmd` #do not use!
您可以使用 -exec
选项在 find
本身中调用 cat
:
find parallel/ -name "*.en" -type f -exec cat {} +
要将其重定向到文件,请使用:
find parallel/ -name "*.en" -type f -exec cat {} + > all.en
根据man find
:
-exec utility [argument ...] {} +
Same as -exec, except that ``{}'' is replaced with as many pathnames as possible
for each invocation of utility. This behaviour is similar to that of xargs(1).