Remove multiple characters from filenames

Aug 07, '02 09:13:50AM

Contributed by: victory

Here's a method for 'squeezing' filenames that contain duplicate runs of characters; the results are similar as to when doing a 'tr -s'. For example, suppose you have a directory full of files named like this:

   LOG_FILE___STATUS1__0001.TXT
and would like to eliminate the multiple underscore characters so that the files are named this way instead:
   LOG_FILE_STATUS1_0001.TXT
A fairly quick way to do this is from the shell. Open a terminal and go to the directory that contains your files, then use:
find . -type f -name '*__*' | awk -F\? '{ s=$1 ; gsub \
( "_+","_",s ) ; print "echo n \| mv -i","\""$1"\"","\""s"\""}' \
| /bin/sh
NOTES:

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020807091350125