我想知道是否可以在一个目录及其子目录中找到多个.txt 文件并在其中写入一些内容。类似的东西
find . -depth -type d -path "/path/to/files/*.txt" -exec echo > *.txt;
我设法创建了这样的文件,但现在我想写入所有文件。
提前致谢。
最佳答案
我想用一个例子来回答:
将“blablub”写入以下位置的每个文件(和子目录):
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
关于linux - 找到一个.txt文件并写入其中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49149905/