rename all images in folder with prefix of date and time from exif data

for i in `ls` ; do date=$(identify -format %[exif:DateTime] $i); date=${date//:/-}; date=${date// /_}; mv $i ${date}__$i; done
imagemagick is required
Sample Output
DSC_9274.JPG will be renamed to 2014-10-26_16-23-32__DSC_9274.JPG

By: klausman
2014-10-30 20:35:16

1 Alternatives + Submit Alt

What Others Think

I can see a few problems. First: for i in `ls`; do This is a bad habit you want to get out of. If your filenames contain whitespace the script won't work. Here, this will work fine: for i in *; do My improved for loop variable might contain whitespace, so I need to quote them "$i" Next, it's better to use $(command) rather than `command` The $(...) form can be nested. It doesn't matter here, but it's often useful. Then we have your substitutions. That's a very nice use of the shell, but "tr" is perfect: ... | tr ': ' '-_' That's quote, colon, space, quote, space, quote, dash, underscore, quote "tr" replaces all colons with dashes and then all spaces with underscores. That saves a lot of typing. My final worry is that my camera can take several pictures in one second. If you have multiple pictures taken in the same second, the last one will overwrite the others. My tidied command looks like this: for i in *; do date=$(identify -format %[exif:DateTime] "$i" | tr ': ' '-_'); mv $i ${date}__$i; done
flatcap · 377 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: