Rename multiple files in Linux
Important notes:
All examples of this article are tested successfully in Ubuntu/Debian environment.
And I have tested these examples also in RHEL/Non-Debian environment, but failed.
So, pls note about this.-------------------------------------------------------------------------
Many of you may know the ways how to rename multiple files in Linux(such as by using awk , using mv together with for , etc...), But today, I want to share with you the way by using the command "rename " to change names of bulk files in Linux.
Let talk about the advantages of this "rename " command.
First of all, rename is actually a a perl script, so it support the perl REGEX , it's very helpful when we handle the multiple files.
Secondly, the scripts can be retrenched to be very simple, I mean, we can use one command line to achieve our targets.
-------------------------------------------------------------------------
rename:
Syntax:
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
-v
Verbose: print names of files successfully renamed.
-n
No Action: show what files would have been renamed.
-f
Force: overwrite existing files.
perlexpr
Perl Expression
Regular Expressions
^
matches the beginning of the line$
matches the end of the line.
Matches any single character(character)*
match arbitrarily many occurences of (character)(character)?
Match 0 or 1 instance of (character)[abcdef]
Match any character enclosed in [] (in this instance, a b c d e or f) ranges of characters such as [a-z] are permitted. The behaviour of this deserves more description.[^abcdef]
Match any character NOT enclosed in [] (in this instance, any character other than a b c d e or f)(character)/{m,n/}
Match m-n repetitions of (character)(character)/{m,/}
Match m or more repetitions of (character)(character)/{,n/}
Match n or less (possibly 0) repetitions of (character)(character)/{n/}
Match exactly n repetitions of (character)/(expression/)
Group operator./n
Backreference - matches nth groupexpression1/|expression2
Matches expression1 or expression 2. Works with GNU sed, but this feature might not work with other forms of sed./w
matches any single character classified as a “word” character (alphanumeric or “_”)/W
matches any non-“word” character/s
matches any whitespace character (space, tab, newline)/S
matches any non-whitespace character/d
matches any digit character, equiv. to [0-9]/D
matches any non-digit character-------------------------------------------------------------------------
As rename is a perl script you will need perl to run it, and here are some examples about how to use it.
Examples:
rename -v 's//.htm$//.html/' *.htmThis is going to change htm to html in every file ending with .htm in its name.
If you want to change the name of something like this:
-rw-r--r-- 1 bob bob 0 2011-05-18 19:33 1.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 19:33 2.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 19:34 3.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 20:35 b.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 20:35 c.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 20:35 d.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 20:35 e.txt
That is the output of ls -l, and are files created with touch by me for this examples.
Now lets say I want to add a more descriptive string to the name of these files like Thesis, so here we go.rename -n 's/(/w{1})/.txt$/$1_thesis/.txt/' *.txtNote: Using -n to make only a test and see if the result is what you want
1.txt renamed as 1_thesis.txt
2.txt renamed as 2_thesis.txt
3.txt renamed as 3_thesis.txt
b.txt renamed as b_thesis.txt
c.txt renamed as c_thesis.txt
d.txt renamed as d_thesis.txte.txt renamed as e_thesis.txt
As you see that is what I wanted, now lets suppose I only want to change the name to files with a number in the name and with a letter in it.
rename -n 's/(/d{1})/.txt$/$1_thesis/.txt/' *.txt1.txt renamed as 1_thesis.txt
2.txt renamed as 2_thesis.txt
3.txt renamed as 3_thesis.txtYou can also match only the ones with non-digit names
rename -n 's/(/D{1})/.txt$/$1_thesis/.txt/' *.txtAnd the output will be:
b.txt renamed as b_thesis.txt
c.txt renamed as c_thesis.txt
d.txt renamed as d_thesis.txt
e.txt renamed as e_thesis.txt
As you may see, it is just a "using the right regexp" thing.
In case you do not have rename on your system (I think non-Debian does not have ) you can use mv .
转自:http://scmbob.org/rename_multiple_files_in_linux.html