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 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 .

sudo -i vs sudo -s

December 28th, 2009 by Leandro Morgado

A long time ago, every time I needed to do some admin work on Linux I used to “su -” into a root shell. After moving from Debian to Ubuntu, I noticed that “su -” would not work as the root account was disabled. So I started fooling the system with:

shell> sudo su -

But that was more a hack than anything else. You don’t actually need to run su with sudo to get a root shell. You can either sudo right away with:

shell> sudo -i
shell> sudo -s

So what are the differences between these? Well, the -i switch gives you the root shell environment, working the same way as “su -“. The -s switch preserves your own environment, and is equivalent to just running “su” with the dash.

Obviously, I’m not the only one who has come across this question and Ubuntu docs have a full page on this.

Happy rooting! 🙂