Showing results for 
Search instead for 
Did you mean: 

renaming multiple files

Andrew Kaplan
Super Advisor

renaming multiple files

Hi there --

I want to rename multiple files in a directory using the rename command. For example, a typical file looks like the following:

CTI.123456789.987654321-1601-XY.48.1 (1).ALAN

I would like to renaming all such files to read as follows:


What command syntax can I use to facilitate renaming all the files in question? Thanks.
A Journey In The Quest Of Knowledge
Hein van den Heuvel
Honored Contributor

Re: renaming multiple files

I like to use perl for these tasks to have the full power of regular expression for the job.

You for not indicate the exact replace pattern, but I suspect you want to replace a s "space plus number in parens" by a "period plus that number". Correct?

Here is a working example:

$ cat > "CTI.123456789.987654321-1601-XY.48.1 (1).ALAN"
$ dir *.ALAN
CTI.123456789.987654321-1601-XY.48.1\ (1).ALAN

# first try with a PRINT instead of rename.

$ perl -e 'for (<*ALAN>){ $o=$_; s/\s+\((\d+)\)/.$1/; print qq($o, $_\n)}'

CTI.123456789.987654321-1601-XY.48.1 (1).ALAN, CTI.123456789.987654321-1601-XY.48.1.1.ALAN

# Looks ok? Go!

$ perl -e 'for (<*ALAN>){ $o=$_; s/\s+\((\d+)\)/.$1/; rename $o, $_}'

$ dir *.ALAN


perl -e '...' # immediate perl program

for (<*ALAN>){ ... } # 'glob' through all files ending in ALAN getting the names in variable $_, executing { ...} for each.

$o = $_ # Stash away the original name.

s/\s+\((\d+)\)/.$1/; # The replace needed.

rename $o, $_ # do the deed.