在一个目录中有多个以fgh开头的文件,例如:
fghfilea
fghfileb
fghfilec
我想将它们全部重命名为以前缀jkl开头。是否有一个单独的命令来执行该操作,而不是逐个重命名每个文件?
在一个目录中有多个以fgh开头的文件,例如:
fghfilea
fghfileb
fghfilec
我想将它们全部重命名为以前缀jkl开头。是否有一个单独的命令来执行该操作,而不是逐个重命名每个文件?
当前回答
rename fgh jkl fgh*
其他回答
有几种方法,但使用rename可能是最简单的。
使用一个版本的rename (Perl的rename):
rename 's/^fgh/jkl/' fgh*
使用另一个版本的rename(与Judy2K的答案相同):
rename fgh jkl fgh*
您应该检查您的平台的手册页,以确定上述哪一种方法适用。
通用命令为
找到/路径/ /文件- name ' <搜索> *’- bash - c”mv $ 0 ${0 / <搜索> / <取代>}“{}\;
其中<search>和<replace>应分别替换为您的源和目标。
作为针对您的问题定制的更具体的示例(应该从与您的文件所在的文件夹运行),上面的命令看起来像这样:
找到。- name的海湾金融公司* - bash - c ' mv $ 0 ${0 /金融/ . jkl} ' {} \;
对于“演练”,在mv之前添加echo,这样你就会看到生成了什么命令:
找到。- name的海湾金融公司* - bash - c的回声mv $ 0 ${0 /金融/ . jkl}’{}\;
有很多方法可以做到这一点(并不是所有的方法都适用于所有的unix系统):
ls | cut -c4- | xargs -I§ mv fgh§ jkl§ The § may be replaced by anything you find convenient. You could do this with find -exec too but that behaves subtly different on many systems, so I usually avoid that for f in fgh*; do mv "$f" "${f/fgh/jkl}";done Crude but effective as they say rename 's/^fgh/jkl/' fgh* Real pretty, but rename is not present on BSD, which is the most common unix system afaik. rename fgh jkl fgh* ls | perl -ne 'chomp; next unless -e; $o = $_; s/fgh/jkl/; next if -e; rename $o, $_'; If you insist on using Perl, but there is no rename on your system, you can use this monster.
其中一些有点复杂,列表还远远不够完整,但是您将在这里找到几乎所有unix系统所需的内容。
我编写了这个脚本来搜索所有.mkv文件,递归地将找到的文件重命名为.avi。您可以根据自己的需要定制它。我还添加了一些其他的东西,比如从文件路径中获取文件目录,扩展名,文件名,以防你将来需要引用一些东西。
find . -type f -name "*.mkv" | while read fp; do
fd=$(dirname "${fp}");
fn=$(basename "${fp}");
ext="${fn##*.}";
f="${fn%.*}";
new_fp="${fd}/${f}.avi"
mv -v "$fp" "$new_fp"
done;
重命名海量文件的我版本:
for i in *; do
echo "mv $i $i"
done |
sed -e "s#from_pattern#to_pattern#g” > result1.sh
sh result1.sh