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

假装没事ソ 提交于 2019-12-05 09:53:16

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
Mikaël Mayer

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

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