【问题标题】:find a .txt file and write in it找到一个 .txt 文件并写入其中
【发布时间】:2018-03-07 10:42:02
【问题描述】:

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

find . -depth -type d -path "/path/to/files/*.txt" -exec echo > *.txt;

我设法创建了这样的文件,但现在我想把它们都写进去。

提前致谢。

【问题讨论】:

  • 一旦你找到一个txt文件你想做什么就不是那么清楚了。
  • @abhijitPritam 编辑了我的帖子,谢谢你没有注意到
  • touch > file 应该做什么?!
  • @GéCusters 我认为您要查找的内容类似于find /path/to/files/ -name '*.txt' -exec sh -c 'something > "{}"',但问题有点不清楚。
  • @Biffen 我编辑了我的帖子,它应该是echo > file 它并不像我所知的那样存在,但我想做类似的事情

标签: linux shell find


【解决方案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

【讨论】:

  • 感谢您的帮助,我会尝试并告诉您它是否有效,或者我是否设法让它工作
猜你喜欢
  • 2021-02-06
  • 2018-08-09
  • 2012-06-17
  • 2016-06-18
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多