Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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

For a bash script I am currently developing I want to access the name of the most recently edited file (including folders but not subdirectories) in a specified directory. How do I do this?

share|improve this question
up vote 2 down vote accepted

As @fedorqui suggested You can do:

ls -1t | awk '{ print $9; exit}'

Old post:

You can get it using awk and tail

ls -lrt /path/to/folder/ | tail -1 | awk '{ print $9 }'

share|improve this answer
Note you can improve it by using ls -lt (without -r that reverses) and then skip tail -1 by doing awk '{print $9; exit}' – user75460 Apr 11 '14 at 20:02
ls -lt gives "total XXXX" on the first line. – Mateusz Szlosek Apr 11 '14 at 20:06
As per the -l part. You can use 1 (one) instead. – user75460 Apr 11 '14 at 20:08
Good point, this works. Thanks! – Mateusz Szlosek Apr 11 '14 at 20:10
ls -t /path/to/folder/ | sed -n 1p

-t sorts ls by date modified. The output is piped to sed to get the first line of the output.

share|improve this answer
thanks for the help. – David Knaack Apr 11 '14 at 16:56
Don't You print only the third file in first column by using this sed command? – Mateusz Szlosek Apr 11 '14 at 18:02
@Mateusz ls, when piped, doesn't print in columns. I've made the answer much simpler though, printing the first line since if you haven't aliased ls to show hidden files etc then the first line is all that's necessary since there's no total/etc. – grgarside Apr 11 '14 at 18:18
You can also do ls -t /path/to/folder/ | head -1 which I think is slightly more readable. – sgauria Apr 11 '14 at 21:12

Parsing ls is helpful in most of the cases, but sometimes it is not a good idea.

To have a 100% sure solution, you can use the find command and make it print the last time all files were accessed. By printing it in a numeric way, they you can sort the list and print the last one.

find /your/path -maxdepth 1 -print0 | xargs -0 stat -c "%Y %n" | sort -n | tail -1 | cut -d' ' -f2-


  • -maxdepth 1 does not go deeper in the directory structure.
  • xargs -0 stat -c "%Y %n" gets the file name and prints its time of last modification plus the name of the file. (from man stat: "%Y time of last modification, seconds since Epoch")
  • sort -n orders the list numerically, being the first the oldest.
  • tail -1 gets the last of the list.
  • cut -d' ' -f2- removes the printing of the datetime.
share|improve this answer
There's no -printf operator on OS X find. find: -printf: unknown primary or operator – Mateusz Szlosek Apr 11 '14 at 19:50
Oh, thanks @MateuszSzlosek Just updated with an approach with print0 and then xargs. I am curious to see if it works. It should... – user75460 Apr 11 '14 at 19:57
stat: illegal option -- c – Mateusz Szlosek Apr 11 '14 at 20:02
Let's try with stat -f as described in Last modified date of a directory in OSX – user75460 Apr 11 '14 at 20:05
-print0 option gives me concatenated output of ls – Mateusz Szlosek Apr 11 '14 at 20:13

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.