How to use mv command to rename multiple files in unix?

余生颓废 提交于 2019-12-22 06:08:07

问题


I am trying to rename multiple files with extension xyz[n] to extension xyz

example :

mv *.xyz[1] to *.xyz

but the error is coming as - " *.xyz No such file or directory"


回答1:


Don't know if mv can directly work using * but this would work

find ./ -name "*.xyz\[*\]" | while read line
do 
mv "$line" ${line%.*}.xyz
done



回答2:


Let's say we have some files as shown below.Now i want remove the part -(ab...) from those files.

> ls -1 foo*
foo-bar-(ab-4529111094).txt
foo-bar-foo-bar-(ab-189534).txt
foo-bar-foo-bar-bar-(ab-24937932201).txt

So the expected file names would be :

> ls -1 foo*
foo-bar-foo-bar-bar.txt
foo-bar-foo-bar.txt
foo-bar.txt
> 

Below is a simple way to do it.

> ls -1 | nawk '/foo-bar-/{old=$0;gsub(/-\(.*\)/,"",$0);system("mv \""old"\" "$0)}'

for detailed explanation check here




回答3:


I think mv can't operate on multiple files directly without loop. Use rename command instead. it uses regular expressions but easy to use once mastered and more powerful.

rename 's/^text-to-replace/new-text-you-want/' text-to-replace*

e.g to rename all .jar files in a directory to .jar_bak

rename 's/^jar/jar_bak/' jar*


来源:https://stackoverflow.com/questions/13967572/how-to-use-mv-command-to-rename-multiple-files-in-unix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!