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 want to write a command that gives me the newest file in a directory recursively. But that's not my only limitation. The files has to be an mp3 or a jpg file. (case insensitive prefered) I only need the creating date of that newest file. If possible I want it formatted like this: 30-12-2014 (so: day-month-year)

This is currently wath I've got:

find .  -name '*.mp3' -or -name '*.JPG'   -printf "%TD \n" | sort -rn | head -n 1

But it doesn't work well. I only get JPG's and the date isn't formatted.

share|improve this question
up vote 5 down vote accepted

Something like this should work:

find . \( -iname "*.mp3" -o -iname "*.jpg" \) -printf '%TY%Tm%Td %TT %p\n' | sort -r

This should find the files that (case-insensitively) find files ending with mp3 or jpg, print out the modification time, then sort it in reverse order.

It seems to show both file-types when you run it effectively as two commands:

( find . -iname "*.mp3" -printf '%TY%Tm%Td %TT %p\n' ; find . -iname "*.jpg" -printf '%TY%Tm%Td %TT %p\n' ) | sort -r
share|improve this answer
I'm only getting the .jpg files with that command. No mp3's are shown. Switching over themp3 and jpg, gives me only mp3's – user2815780 Jul 30 '14 at 15:54
@Wilf you need \( -iname '*.mp3 -o -iname '*.jpg' \) otherwise it will match one or the other. – val0x00ff Jul 30 '14 at 16:06
@val0x00ff - thanks - wondered why it was being weird... it there a actual reason why find does this? I have previously found it has had problems with paths as well... – Wilf Jul 30 '14 at 16:07
Works great! Thanks ;) – user2815780 Jul 30 '14 at 16:09
@Wilf scroll just a bit down on this site: mywiki.wooledge.org/UsingFind and you'll find the answer. – val0x00ff Jul 30 '14 at 16:10

With zsh:

setopt extendedglob
zmodload zsh/stat
zstat -F %F +mtime -- **/(#i)*.(mp3|jpg)(Om[1])

Note that it's based on last modification time, the creation time (whatever that means) is generally not readily available on Linux.

It doesn't consider hidden files. I you want them, add the D globbing qualifier above.

share|improve this answer
euhm I'm using debian (and I'm a linux noob). These commands don't work. Do I need to install something, because I'd rather not. – user2815780 Jul 30 '14 at 15:41
You just have to type zsh and try the solution. In most of the systems, the zsh shell comes by default. – Ramesh Jul 30 '14 at 15:50
Nothing happens on my raspberry pi. – user2815780 Jul 30 '14 at 16:00

Also see the following:

find . -type f -mtime -1 \( -name '*.mp3' -o -name '*.jpg' \) -printf '%AY-%Am-%Ad %P \n'
share|improve this answer
I'm getting no output :/. Didn't crash though. – user2815780 Jul 30 '14 at 15:56
@user2815780 You can play with -mtime -1 by changing the modification time. For example -mtime -10 wich will search for mp3 or jpg files which are created 10 days from now and ofcourse you can set |head -n4 or whatever you want at the end – val0x00ff Jul 30 '14 at 16:00
doesn't work, sorry. Still no output. Altought according to file creation date it should give some output :/ – user2815780 Jul 30 '14 at 16:05
@user2815780 'Still no output' is not enough. Maybe you need to increase -mtime -100 to search for latest 100 dasys – val0x00ff Jul 30 '14 at 16:08
Yeah, that's what I did, but it didn't work. I even set it to 999. Checked the clock on my system and checked the date of the files. sdtill no luck. – user2815780 Jul 30 '14 at 17:59

You specified -or, that needs to be -o, as other have corrected. You also need parentheses I think (correction on my original post). I'd recommend using %T@ for the format specifier (seconds since 1970) which is cleaner to sort; then you can convert it using

date --date="@`find . \\( -iname "*.mp3" -o -iname "*.jpg" \\) -printf '%T@\n' | sort -rn | head -n1`" 

or whatever. You can also put in whatever date format you want using +.

share|improve this answer

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.