如何递归地`touch`文件?

Una*_*rai 67 command-line files touch-command

我需要更改大约 5000 个文件的时间戳。

打字touch file1.txttouch file2.txt将永远带我。

有没有办法做一些事情touch -R *

g_p*_*g_p 107

您可以使用find命令来查找所有文件并touch使用-exec

find . -type f -exec touch {} +
Run Code Online (Sandbox Code Playgroud)

如果您只想过滤文本文件的结果,您可以使用

find . -type f -name "*.txt" -exec touch {} +
Run Code Online (Sandbox Code Playgroud)

  • 我建议使用`-exec` 的`+` 终止符(而不是`\;`)。这会将多个参数链接到每个 `touch` 实例上(达到系统参数限制),因此将 * 少得多 * (并且可能更快)。 (20认同)
  • 谢谢,虽然我认为 'find' 缺少他的第一个参数。`找到。-type f -exec touch {} +` (5认同)
  • 值得注意的是,“find”的手册页(Ubuntu 19.10)表明使用“-execdir”比“-exec”更安全,因为“-execdir”从“find”结果所在的目录运行每个命令位于。它还表示,当从 shell 调用时,“[大括号对] 应加引号(例如,'{}')以保护它不被 shell 解释”。 (5认同)
  • 对于试运行,只需省略`-exec touch {} +` 部分,它就会将它*将*影响的内容打印到您的终端。 (3认同)
  • @g_p ...有关来源,请参阅[使用“+”和“\;”的区别是什么 在 -exec 命令中?](http://askubuntu.com/a/558819/283843) (2认同)
  • 省略“-type f”,它也会影响目录:“find . -execdir touch '{}' +` (还纳入了上述其他改进) (2认同)