7

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.

1

4 Answers 4

5

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
4
  • 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 Commented Jul 30, 2014 at 15:54
  • @Wilf you need \( -iname '*.mp3 -o -iname '*.jpg' \) otherwise it will match one or the other. Commented Jul 30, 2014 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
    Commented Jul 30, 2014 at 16:07
  • 1
    @Wilf scroll just a bit down on this site: mywiki.wooledge.org/UsingFind and you'll find the answer. Commented Jul 30, 2014 at 16:10
4

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.

3
  • 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. Commented Jul 30, 2014 at 15:41
  • 1
    You just have to type zsh and try the solution. In most of the systems, the zsh shell comes by default.
    – Ramesh
    Commented Jul 30, 2014 at 15:50
  • Nothing happens on my raspberry pi. Commented Jul 30, 2014 at 16:00
2

Also see the following:

find . -type f -mtime -1 \( -name '*.mp3' -o -name '*.jpg' \) -printf '%AY-%Am-%Ad %P \n'
5
  • I'm getting no output :/. Didn't crash though. Commented Jul 30, 2014 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 Commented Jul 30, 2014 at 16:00
  • doesn't work, sorry. Still no output. Altought according to file creation date it should give some output :/ Commented Jul 30, 2014 at 16:05
  • 1
    @user2815780 'Still no output' is not enough. Maybe you need to increase -mtime -100 to search for latest 100 dasys Commented Jul 30, 2014 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. Commented Jul 30, 2014 at 17:59
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .