Usage

The rename script uses the perl substitution expression on a list of file names and directories in working directory.


Command Line:

rename is called with the following command line prompt:

$ rename [-v] [-n] [-f] <perl expression> [<file 1> <file 2> ...]


Arguments:

The following is a list of arguments for rename:

-v, --verbose
Prints names of files successfully renamed.
-n, --no-act
Shows what files would have been renamed, but makes no changes.
-f, --force
Overwrites existing files.
<perl expression>
Perl substitution expression.
[<file 1> <file 2> ...]
List of files and directory names that will be changed according to perl substitution expression.


Example:

Removing the '.txt' extension from all files and directories from a folder would use the following command line prompt:

$ rename 's/\.txt$//' *.txt


This finds every instance of a file or directory name, ending with '.txt', and replacing that portion of the name with an empty string. This is only done to files that follow the regular expression *.txt, which means files and directories without the .txt extension will not even be considered.


This would change the following files from this:

file1.txt
file2.txt
file3.txt
file4.gz
file5.gz


Into this:

file1
file2
file3
file4.gz
file5.gz


Perl Substitution Expression

The <perl expression> uses a substitution expression built into Perl. More information on substitution expressions for Perl can be found here


Substitution:

Substitution replaces instances of matching characters in a string with a different group of characters. For example, replacing the name 'Jim' with 'James' in the sentence 'My name is Jim and his name is Jim.'


This can be done with the following expression:

's/Jim/James/'


Resulting in the sentence:

'My name is James and his name is Jim.'


This only changes the first instance of the string 'Jim'. To replace all instances of 'Jim' into 'James', include the flag 'g' at the end of the expression.


The following expression will replace ALL instances of 'Jim' with 'James':

's/Jim/James/g'


Resulting in the sentence:

'My name is James and his name is James.'


Translation:

Translation allows for character by character translation. For example, in the string 'qwe', each instance of 'q' can be replaced 'a',  each instance of 'w' can be replaced with 's', and each instance of 'e' can be replaced with 'd'


This can be done with the following expression:

'tr/qwe/asd/'


Resulting in the string:

'asd'


This can be taken one step further by making an entire string upper case by using a range of characters.


The following expression will all lower case characters with upper case characters:

'tr/a-Z/A-Z/'


Regular Expressions for File List

The[<file 1> <file 2> ...] portion of the command line prompt for rename can be used in one of two ways. The regular expression list can either be included directly in the initial prompt as shown in the examples above, or the list can be added afterwards with Perl STDIN.


Include Regular Expression List in Initial Call to rename:

Suppose we want to make all files and directories that end with the extensions '.txt', '.gz', and '.fasta' completely uppercase.


This can be done with the following rename call:

$ rename 'tr/a-z/A-Z/' *.txt *.gz *.fasta


This would change the following files from this:

file1.txt      file6.fasta
file2.txt      file7.fasta
file3.txt      file8.fasta
file4.gz       file9.fastq
file5.gz       file10.fastq


Into this:

FILE1.TXT      FILE6.FASTA
FILE2.TXT      FILE7.FASTA
FILE3.TXT      FILE8.FASTA
FILE4.GZ       file9.fastq
FILE5.GZ       file10.fastq


Notice that the filenames for files ending in the extension '.fastq' have not been effected, because '.fastq' has not been added into the regular expression list.


Include Regular Expression List in Perl STDIN After Call to rename:

The above can be recreated by using Perl STDIN after the call to rename by leaving the list blank.


This can be done with the following rename call:

$ rename 'tr/a-z/A-Z/'


After the initial call, each regular expression can be typed in separated by pressing the enter button, resulting in the following command line:

$ rename 'tr/a-z/A-Z/'
*.txt
*.gz
*.fasta


After all of the regular expressions have been entered, accept the Perl STDIN by pressing ctrl+d. This will result in the exact same file changes as shown in the previous example.


Known Bugs

Error Message Displayed With No Error:

On occasion an error message will be displayed when renaming files is actually successful. The error warns that the change did not take place, when it actually has.


For example, using the command line prompt from above to make all files and directories that end with the extensions '.txt', '.gz', and '.fasta' completely uppercase:

$ rename 'tr/a-z/A-Z/' *.txt *.gz *.fasta


The following error message may be diplayed:

Can't rename file1.txt FILE1.TXT: Input/output error
Can't rename file2.txt FILE2.TXT: Input/output error
Can't rename file3.txt FILE3.TXT: Input/output error
Can't rename file4.gz FILE4.GZ: Input/output error
Can't rename file5.gz FILE5.GZ: Input/output error
Can't rename file6.fasta FILE6.FASTA: Input/output error
Can't rename file7.fasta FILE7.FASTA: Input/output error
Can't rename file8.fasta FILE8.FASTA: Input/output error


Although, the $ls bash command will show that the changes to the files was actually successful:

FILE1.TXT      FILE6.FASTA
FILE2.TXT      FILE7.FASTA
FILE3.TXT      FILE8.FASTA
FILE4.GZ       file9.fastq
FILE5.GZ       file10.fastq


It is recommended to use the $ls bash command to check for changes after using the rename script.