我想重命名几个文件夹中的所有文件,文件名包含 '*file*' '*doc*'。我试过了
find . -name "*file*" -exec mv {} `echo {} | sed "s/file/doc/"` \;
但出现错误(见下文)。
~$ ls
my_file_1.txt my_file_2.txt my_file_3.txt
~$ find . -name "*file*"
./my_file_1.txt
./my_file_3.txt
./my_file_2.txt
~$ echo my_file_1.txt | sed "s/file/doc/"
my_doc_1.txt
~$ find . -name "*file*" -exec echo {} \;
./my_file_1.txt
./my_file_3.txt
./my_file_2.txt
~$ find . -name "*file*" -exec mv {} `echo {} | sed "s/file/doc/"` \;
mv: './my_file_1.txt' and './my_file_1.txt' are the same file
mv: './my_file_3.txt' and './my_file_3.txt' are the same file
mv: './my_file_2.txt' and './my_file_2.txt' are the same file
非常感谢您的帮助!
有一千种方法可以做到,我会用 Perl 来做,这样的事情会起作用:
find files -type f -name "file*" | perl -ne 'chomp; $f=$_; $f=~s/\/file/\/doc/; `mv $_ $f`;'
-ne
处理每一行输入的内联脚本chomp
清理换行符$f
是新文件名,与旧文件名相同s/\/file/\/doc/
将新文件名中的“/file”替换为“/doc”mv $_ $f
通过运行带有反勾号的操作系统命令来重命名文件本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句