|
Page: Regular expressions - Regex - renaming multiple files
REGULAR EXPRESSIONS _ REGEX _ RENAMING MULTIPLE FILES
If you want to rename files or e.g. remove spaces in file names with one bash command then you are right here. |
There is a program called "rename". You can use it to fulfil this task. |
It makes use of regular expressions to do so. Here are some examples: |
rename 'y/A-Z/a-z/' *.txt |
|
would transform all characters from A to Z to lowercase in all files having a .txt extension. |
This one would substitute all space characters of all file names into "_" (no matter how often it is repeated or how often space occurs in the file name). |
Some basic regex information: |
| • | ^ matches at the start of the string |
|
| • | & matches at the end of the string |
|
| • | . any single character except line break characters |
|
| • | \w means word characters (letters and digits) |
|
| • | \D, \W, \S negative versions |
|
Search And Replace:
| • | e Evaluate the right side as an expression. |
|
| • | g Replace globally, i.e., all occurrences. |
|
| • | i Do case-insensitive pattern matching. |
|
| • | m Treat string as multiple lines. |
|
| • | o Compile pattern only once. |
|
| • | s Treat string as single line. |
|
| • | x Use extended regular expressions. |
|
String Transliteration:
y/searchlist/replacementlist/options |
|
tr/searchlist/replacementlist/options |
|
| • | c Complement the SEARCHLIST. |
|
| • | d Delete found but unreplaced characters. |
|
| • | s Squash duplicate replaced characters. |
|
|