Files – How to Find Newest File with Multiple Filetype Restrictions

filesfindsorttimestamps

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.

Best Answer

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
Related Question