Loopy loop
Filed in archive Tips & Tricks by mateusz on November 19, 2007
With couple of additional, standard tools, you can build ultimate tool for renaming files. Unlike the GUI solutions which are only as good as what the author thought would be useful, in shell you can do anything. You are the author!
Let's first tackle filename extension changing. Say you've got 20 javascript files you want to become templates in your CMS system:
We will need a handy small tool for our task: basename
This is how it works:
The last param is the optional suffix you want stripped. This way we can obtain clean filename without any suffix and replace it with a new one.
See what happens here? We loop through all the files renaming each one of them to its basename (backticks is inline command execution) concatenated with a new extension. Simple!
From here it's easy. How about renaming all the JPGs in current dir to their respective exif time? Very useful for sorting pictures timewise. Let's check how exiftool works:
Let's adjust the format a bit:
Looks good now, sorting will be in the order pictures were taken.
You can see the pattern already.
(The above, simple solution has one drawback, it will overwrite pictures which were taken at the exact same second. Solving this I leave up to you. You will probably need to make yet another loop inside... But hey, it's all about loops here).
One more useful thing, especially when transferring files from non-linux filesystems is changing all the filenames to lowercase (or uppercase if you wish). Easy way to do this is:
(As with every simple solution beware of its shortcomings: this one will change also directory names and overwrite possibly created duplicates, i.e. when lowercase named file already exists. Add -i option to mv to be asked before overwrite).
Permalink: Loopy loop
Tags:
bash for loop linux shell
Trackback: http://www.creative-weblogging.com/cgi-bin/mt-tb.pl/102752
























