找到一个.txt文件并写入其中

时间:2018-03-07 10:42:03

标签: linux shell find

我想知道是否有可能在目录及其子目录中找到多个.txt文件并在其中写入内容。

之类的东西
find . -depth -type d -path "/path/to/files/*.txt" -exec echo > *.txt;

我设法创建了这样的文件,但现在我想写所有这些文件。

提前感谢。

1 个答案:

答案 0 :(得分:1)

我想回答一个例子:

将'blablubb'写入:

中的每个文件(和子目录)
me@my:/tmp$ ls /tmp/*txt
-rw-r--r-- 1 v v  9 Mar  7 13:24 /tmp/0txt
-rw-r--r-- 1 v v  9 Mar  7 13:24 /tmp/1txt
-rw-r--r-- 1 v v  9 Mar  7 13:24 /tmp/2txt
-rw-r--r-- 1 v v  9 Mar  7 13:24 /tmp/3txt
-rw-r--r-- 1 v v  9 Mar  7 13:24 /tmp/4txt

您可以使用find ... -exec ... xargs撰写:

me@my:/tmp$ find . -maxdepth 2 -regex '.*txt' | xargs -I{} sh -c "echo 'blablubb' >> {}" 

me@my:/tmp$ find . -maxdepth 1 -regex '.*txt' -exec cat {} \;
blablubb
blablubb
blablubb
blablubb
blablubb

更多信息可以在这里找到:https://unix.stackexchange.com/questions/22229/xargs-with-stdin-stdout-redirection