HTTP/1.1/GET Sat, 31 Jul 2010 02:46:39 +0000
URL » www.hafenscher.net/wiki/index.php?page=Regular_expressions_-_Regex_-_renaming_multiple_files

Wiki › Linux, Mac OS X, Windows and more.

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:

The code

rename 'y/A-Z/a-z/' *.txt

would transform all characters from A to Z to lowercase in all files having a .txt extension.

rename 's/\s+/_/g' *

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
\d means digits
\w means word characters (letters and digits)
\s means whitespace
\D, \W, \S negative versions

Search And Replace:

s/search/replace/options

 
options
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

 
options
c Complement the SEARCHLIST.
d Delete found but unreplaced characters.
s Squash duplicate replaced characters.


© 2000 - 2010 Wolfgang Hafenscher
About   Impressum   Disclaimer

Online Consulting

validate
# 20001