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

标签 linux shell find

我想知道是否可以在一个目录及其子目录中找到多个.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/

相关文章:

linux - exit() 和 exit_group() 有什么区别

linux - rsync 在传输过程中以固定位置挂起

python - 如何在Python脚本执行时检查bash shell脚本的状态?

javascript - 如何用另一个字符串替换字符串中的某个单词并排除原子正则表达式中某些类型的所述单词

linux - Linux 内置驱动加载顺序是什么?

linux - "Can' t解析符号的可能原因”?

javascript - 如何通过引用查找数组元素以便将数组与 typescript 合并?

excel - 如何在真正的空白单元格上进行查找和替换

linux - 制作 : nothing can be done for all

shell - 为什么Shell中多个赋值可以用空格分隔