How to use mv command to rename multiple files in

2019-05-02 11:08发布

问题:

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