HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
cancel
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:

CTI.123456789.987654321-1601-XY.48.1.1.ALAN

What command syntax can I use to facilitate renaming all the files in question? Thanks.
A Journey In The Quest Of Knowledge
1 REPLY
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"
test
$ 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
CTI.123456789.987654321-1601-XY.48.1.1.ALAN

explanation:

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.

Hein.