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

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


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

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


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).txtfoo-bar-foo-bar-(ab-189534).txtfoo-bar-foo-bar-bar-(ab-24937932201).txt

So the expected file names would be :

> ls -1 foo*foo-bar-foo-bar-bar.txtfoo-bar-foo-bar.txtfoo-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


Here is another way using the automated tools of StringSolver. Let us say your first file is named abc.xyz[1] a second named def.xyz[1] and a third named ghi.jpg (not the same extension as the previous two).

First, filter the files you want by giving examples (ok and notok are any words such that the first describes the accepted files):

filter abc.xyz[1] ok def.xyz[1] ok ghi.jpg notok

Then perform the move with the filter it created:

mv abc.xyz[1] abc.xyzmv --filter --all

The second line generalizes the first transformation on all files ending with .xyz[1].

The last two lines can also be abbreviated in just one, which performs the moves and immediately generalizes it:

mv --filter --all abc.xyz[1] abc.xyz

DISCLAIMER: I am a co-author of this work for academic purposes. Other examples are available on youtube.