Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some directories of files copied from my security camera that I would like to organize into sub-directories by file date. So for example;

-rwxrwxrwx 0 root root 4935241 Jul 19  2012 DSCN1406.JPG
-rwxrwxrwx 0 root root 4232069 Jul 19  2012 DSCN1407.JPG
-rwxrwxrwx 0 root root 5015956 Jul 20  2012 DSCN1408.JPG
-rwxrwxrwx 0 root root 5254877 Jul 21  2012 DSCN1409.JPG

I would like a script that runs to see the files in that directory, then create the 3 needed directories named like;

drwxrwxrwx 1 root root     0 Sep  2 16:49 07-19-2012
drwxrwxrwx 1 root root     0 Sep  2 16:49 07-20-2012
drwxrwxrwx 1 root root     0 Sep  2 16:49 07-21-2012

And then move the files into the appropriate directories. Does anyone have any suggestions on a good scriptable way to accomplish this?

share|improve this question
Is GNU find or GNU stat available? – jordanm Mar 1 '13 at 1:17
up vote 7 down vote accepted

On Linux and Cygwin, you can use date -r to read out the modification date of a file.

for x in *.JPG; do
  d=$(date -r "$x" +%Y-%m-%d)
  mkdir -p "$d"
  mv -- "$x" "$d/"

(I use the unambiguous, standard and easily-sorted YYYY-MM-DD format for dates.)

share|improve this answer
Perfect Gilles once I drop cased the .jpg it worked beautifully. Thank you! – Richard Ahlquist Mar 1 '13 at 1:37
I wasn't aware of this date feature, +1. – jordanm Mar 1 '13 at 2:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.