editing large ammounts of filenames

Soldato
Joined
1 Jun 2005
Posts
5,152
Location
Kent
I have read that there are some usefull linux commands that can search for files and edit them in certain ways, so i am hoping someone can help me with what i need to do.

Basicaly, i have a folder full of files with filenames like 'example_d1m1yyyy' 'example_d2m2yyyy' etc..., and i need the files to be renamed to 'Example d1 m1'. Is there any way for me to do a command that will do this change to every file in the folder, replacing the _ with spaces, adding capital letters where needed, and adding spaces where there were none.

Or is things like that a bit to complex and would i be better off doing it manualy, file by file?
 
It might be too simplistic to do what you're wanting to do, but the Bulk File Rename utility that comes with Thunar might be an easy, GUI way to do it.

Of course if you know regexes this is a 5 minute job sitting at the command line.
 
SPACES IN FILENAMES ON UNIX FILESYSTEMS MAKES BABY LLAMAS CRY!

OK now that that outburst is over with, if you are still looking for an answer to this try command, to be run from the directory with the files in:

for file in * ; do newname=`echo ${file} | sed "s;^\([a-zA-Z]\)\([^_]*\)_\(..\)\(..\)\(.*\);\U\1\E\2 \3 \4 \5;"` ; echo Renaming \"$file\" to \"$newname\" ; done

When you are happy with the output from this command (its just printing what it will rename the files to), replace the echo with a mv, eg:

for file in * ; do newname=`echo ${file} | sed "s;^\([a-zA-Z]\)\([^_]*\)_\(..\)\(..\)\(.*\);\U\1\E\2 \3 \4 \5;"` ;mv "${file}" "${newname}" ; done

But please think long and hard if you really want spaces, in my experience they gain very little and will cause you headaches in the future with further scripting.

This will move files of the form:
Renaming "example_d1m1yyyy" to "Example d1 m1 yyyy"
Renaming "example_d2m2yyyy" to "Example d2 m2 yyyy"

etc. (and you'll need GNU version of sed, eg. that supplied with Linux).
 
Back
Top Bottom