Find latest file in a directory

ls -rt | tail -n 1

By: m0rf
2012-09-06 07:07:35

These Might Interest You

  • Sorts by latest modified files by looking to current directory and all subdirectories Show Sample Output

    find . -name '*pdf*' -print0 | xargs -0 ls -lt | head -20
    fuats · 2013-10-03 21:58:51 0
  • zsh only If you have this command in your history, you can always re-run it and have it reference the latest file. The glob matches all timestamped files and then the resulting array is sorted by modification time (m) and then the first element in the sorted array is chosen (the latest)

    tail -f /path/to/timestamped/files/file-*(om[1])
    karld · 2009-02-16 22:55:16 0

  • 0
    ln -s "`find /path -type f -iname $(ls -t /path | head -1 )`" /path/latest
    logikal · 2011-12-21 07:20:17 0

  • -6
    alias wordpress='mkdir wordpress && cd wordpress && wget && tar -xvzf latest.tar.gz && mv wordpress/* . && rm -rf latest.tar.gz wordpress && cp wp-config-sample.php wp-config.php'
    ahabman · 2009-02-26 03:23:18 1

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: