Globally Renaming Files in a Directory

Asked by Julianloui

April 27, 2014
----------------------------------------------------------------------------------
Globally Renaming Files with Same Extension in a Directory
----------------------------------------------------------------------------------
I frequently need to add a prefix to the file names in a directory from the command line globally. An example would be .wav or .mp3 files in a particular directory. This way, I can distinguish similarly named files from other directories.

I have forgotten how to do it using the mv command. I've also tried the rename command without much luck. I've looked into the two commands' manuals but to no avail. As a matter of fact, one recommended 'rename' example does not work in my Ubuntu 12.04 system.

I would appreciate seeing some simple illustrative examples.

Julianloui

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Thomas Krüger
Solved:
Last query:
Last reply:
Revision history for this message
actionparsnip (andrew-woodhead666) said :
#1

Are the files to manipulate without file extension altogether?

Revision history for this message
Julianloui (julianloui) said :
#2

They will always have the same extension. An example would be track01.wav, track02.wav, track03.wav, etc. In a simple case I just like to rename them to 01-track01.wav, 01-track02.wav,... In another case I may want to change the names to trk01.wav, trk02.wav, trk03.wav, etc.

I know how to do the work the long, non-global way instead of using just one single command.

Thanks.

Julianloui.

Revision history for this message
actionparsnip (andrew-woodhead666) said :
#3

Run:

gedit renamescript; chmod +x renamescript

add the below code

Revision history for this message
actionparsnip (andrew-woodhead666) said :
#4

#!/bin/bash

for i in `ls *.wav`; do

num=`echo $i | grep -o '[0-9]\{2\}'`
name="trck$num.wav"
echo "renaming $i to $name"
cp $i $name

done

Revision history for this message
actionparsnip (andrew-woodhead666) said :
#5

If you put 'renamescript in the folder of files to work with it will work there, or you can put it in /usr/bin and you can run it anywhere you like (easier)

If you would ranther move than copy the files change cp to mv (I do it that way as it is less destructive and means you have to delete the old files once you are happy with the result.)

Revision history for this message
Best Thomas Krüger (thkrueger) said :
#6

1. case:
rename -n 's/^/01-/' *.wav

2. case:
rename -n 's/^track/trk/' *.wav

If you are happy with the result, remove the -n to write the changes.

Explanation: The regular expression between the first two slashes is replaced with the text between the 2nd and 3rd slash.
^ is the start of the string.

Revision history for this message
Julianloui (julianloui) said :
#7

Thanks Thomas Krüger, that solved my question.

Revision history for this message
Julianloui (julianloui) said :
#8

Thomas and Andrew,

Thanks very much for your helpful suggestions.

Julianvb