Philippe Simard

Menu Principal
Home
Liens externes
Photos
Ressources
Météo
Password Generator
Sujets
Musique
Bricolage
Informatique
SherBroue
Related Items



Accueil Informatique Bash script to remove special characters in a file name with the sed editor
Bash script to remove special characters in a file name with the sed editor PDF Print E-mail
Written by Philippe Simard   
Friday, 03 October 2008 18:15
Article Index
Bash script to remove special characters in a file name with the sed editor
rrename script
Rename multiple files
All Pages

I have often the need to rename many files in order to remove the specials characters in the file name. Many applications will dislike such characters, and I want to get rid of them. Of course, I don't want to rename all these files by hand, I wrote a script that will replace any special character with some replacement character.

For instance, suppose that you have the following file:

01 I don't like these specials é,à characters.mp3

If you have only one file, that's ok, but if you have tens of mp3 files to rename the task would be tedious.

Typically, you want to replace the "é", "à", ",", "'", the space or any character that you may encounter. There is command available to help you: rename. Unfortunately, this will replace only the first occurrence of the string you want to replace. You would need to run it as long as you have spaces in your file name for example.

The following script will exactly do the trick. It will take the file name as input, create a string with this file name, and use the sed editor to replace the unwanted characters. It will replace all occurrences of all the strings you want to replace. You will define an array containing all of the characters you want to replace, and an array containing all the replacement characters. You also have the option to replace all the unwanted characters with a single replacement character.



Last Updated ( Monday, 06 October 2008 07:00 )
 


(C) 2008 philippesimard.com. Tous droits réservés. Powered by Webboréal hébergement Web.
Your IP: 38.107.191.102

Valid XHTML 1.0 Transitional Valid CSS!