Bash: Sorting .jpg files into directories based on EXIF time

October 8th, 2013 by Leandro Morgado

I had a bunch of .jpg photos in one directory and wanted to place them into folders by date. Here is how I automated that:

shell> exiftool 20130823_210740.jpg | grep -i date
File Modification Date/Time     : 2013:08:23 21:07:40+01:00
Modify Date                     : 2013:08:23 21:07:40
Date/Time Original              : 2013:08:23 21:07:40
Create Date                     : 2013:08:23 21:07:40
shell> for i in $(ls -1 *.jpg); do VAR=$(exiftool $i | grep "Date/Time Original"| cut -d' ' -f16-17 | cut -d':' -f2-4| tr : _ ); echo mkdir -p $VAR; echo mv $i $VAR ; done
mkdir -p 2013_08_23
mv 20130823_210740.jpg 2013_08_23
mkdir -p 2013_08_23
mv 20130823_210744.jpg 2013_08_23
shell> for i in $(ls -1 *.jpg); do VAR=$(exiftool $i | grep "Date/Time Original"| cut -d' ' -f16-17 | cut -d':' -f2-4| tr : _ ); mkdir -p $VAR; mv $i $VAR ; done
shell> ls
2013_08_23	2013_08_31	2013_09_06	2013_09_11	2013_09_15	2013_09_22	2013_10_05
2013_08_26	2013_09_01	2013_09_08	2013_09_12	2013_09_20	2013_09_26	2013_10_06
2013_08_30	2013_09_03	2013_09_09	2013_09_13	2013_09_21	2013_09_27

Bash: script to remove all spaces and uppercase chars from multiple filenames

January 17th, 2012 by Leandro Morgado

I needed to remove the spaces in filenames for many files but doing it by hand was a chore. Here is a small script that did the job for me:

read -p "Are you sure you want to rename all files? (Y/N)" contflag
flag=$(echo $contflag| tr A-Z a-z)
if [ ! ${flag} == "y" ] ; then
 exit 1
for f in *; do
     file=$(echo $f | tr A-Z a-z | tr ' ' _)
     [ ! -f $file ] && mv "$f" $file
exit 0

I know this could be improved, for example, by taking a list of files as an argument, but it does what I want. Use at your own risk.

Happy file renaming! =)

Bash history format

January 16th, 2010 by Leandro Morgado

Sometimes I need to find out when a certain command was executed in the bash shell. By default, the bash shell will give you this:

shell> history | tail -n 2
 1004  history
 1005  history | tail -n 2

This won’t tell you the date but rather just the order that they were run in. If you want to temporarily see the date, then you need to set this:

shell> export HISTTIMEFORMAT='%F %T '
shell> history | tail -n 2
 1006  2010-01-16 00:55:47  export HISTTIMEFORMAT='%F %T '
 1007  2010-01-16 00:55:49 history | tail -n 2

This will last as long as your environment variable is set, so if you log out you will need to set it again. The %F and %T are standard strftime strings. Check the man page for the full range of options. If you want to make this setting permanent system wide, they add it to your /etc/profile .