在目录中递归查找和替换文件名 [英] Find and replace filename recursively in a directory
本文介绍了在目录中递归查找和替换文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将文件夹中以123_xxx.txt
开头的所有文件重命名为xxx.txt
.
I want to rename all the files in a folder which starts with 123_xxx.txt
to xxx.txt
.
例如,我的目录有:
123_xxx.txt
123_yyy.txt
123_zzz.txt
我想将所有文件重命名为:
I want to rename all files as:
xxx.txt
yyy.txt
zzz.txt
我在这个论坛上看到了一些有用的 bash 脚本,但我仍然对如何使用它来满足我的要求感到困惑.
I have seen some useful bash scripts in this forum but I'm still confused how to use it for my requirement.
假设我使用:
for file in `find -name '123_*.txt'` ; do mv $file {?.txt} ; done
这是正确的做法吗?
推荐答案
你可以这样:
find . -name '123_*.txt' -type f -exec sh -c '
for f; do
mv "$f" "${f%/*}/${f##*/123_}"
done' sh {} +
没有管道,没有读取,没有机会破坏格式错误的文件名,没有非标准工具或功能.
No pipes, no reads, no chance of breaking on malformed filenames, no non-standard tools or features.
这篇关于在目录中递归查找和替换文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文