Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

e.g. directory containing jpeg files: how to easily open just the most recent jpeg in the current directory?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

ls -rt | tail -1

The -1 tells tail to show only one line if you want to be clearer you can use the long option instead

ls -rt | tail --lines=1

share|improve this answer
1  
Even better is "ls -t | head -1". See my comment below. –  Andy Lester Oct 9 '10 at 13:53
    
Thanks. - all so simple, you just have to know how... –  ajo Oct 10 '10 at 17:01

What you really want is first file of a non-reversed time-based list.

ls -t | head -1

The -r is for humans because we want the last thing on the screen that has scrolled by, rather than the first. In the case of the machine figuring it out, it might as well use head and stop after the first one, rather than have tail run through the list.

share|improve this answer

With zsh:

gnome-open *.jpg(om[1])

The glob qualifier (om) sorts the matches by increasing age (i.e. in anti-chronological order). The glob qualifier ([1]) selects only the first match. You could use *.jpg(om[1,4]) to open the 4 most recent files, and so on.

In other shells:

gnome-open "$(\ls -t *.jpg | head -1)"

but beware that if you have nonprintable characters or bytes in your file names (which typically happens if you have file names in a different character set from your locale), ls will replace them by ?, so this won't work.

share|improve this answer

Your Answer

 
discard

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.