gpt4 book ai didi

linux - 用查找重命名

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:53:21 24 4
gpt4 key购买 nike

我设法用 find 命令找到了几个文件。这些文件的类型为 file_sakfksanf.txt、file_afsjnanfs.pdf、file_afsnjnjans.cpp、现在我想用 rename 和 -exec 命令重命名它们 mywish_sakfksanf.txt, mywish_afsjnanfs.pdf, mywish_afsnjnjans.cpp只有第一个前缀被改变。我试了一段时间了,别怪我傻。

最佳答案

如果您通读 -execman find 的页面你会遇到 {}允许您将匹配项用作 -exec 中的参数的字符串.这将允许您使用 rename在你的 find匹配方式如下:

find . -name 'file_*' -exec rename 's/file_/mywish_/' {} \;

来自手册:

-exec command ;

Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ;' is encountered. The string{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a `\') or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified command is run once for each matched file. The command is executed in the starting directory.There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.

尽管您要求查找/执行解决方案,如Mark Reed建议,您可能需要考虑将结果传送到 xargs .如果这样做,请确保使用 -print0带有 find 的选项和 -0-null带有 xargs 的选项以避免因文件名中出现空格或 shell 元字符而导致意外行为。另外,考虑使用 + -exec 的版本(也在手册中)因为这是 find 的 POSIX 规范因此,如果您想在其他地方运行您的命令(不是总是 true),应该更便携;它还以类似于 xargs 的方式构建其命令行这应该会减少对 rename 的调用.

关于linux - 用查找重命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12654819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com