How to use mv command to rename multiple files in

2019-05-02 10:56发布

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"

2条回答
何必那么认真
2楼-- · 2019-05-02 11:13

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

查看更多
Lonely孤独者°
3楼-- · 2019-05-02 11:22

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
查看更多
登录 后发表回答