Supposed I make a listing and sort the files by its temporal attribute:

ls -ltr

-rwxrwxrwx 1 bla bla 4096 Feb 01 20:10 foo1
-rwxrwxrwx 1 bla bla 4096 Feb 01 20:12 foo2
-rwxrwxrwx 1 bla bla 4096 Mar 05 13:25 foo1000

What should I add behind the ls -ltr in a pipe chain in order to obtain only the last line of the listing ? I know there are sed and awk, but I do not know how to use them, I only know what they can do.


Since you asked about sed specifically,

ls -ltr | sed '$!d'
  • thank you, i am now a little proud having learned some concrete sed. – Abdul Al Hazred Mar 26 '15 at 22:35
  • 3
    Also, sed -n '$p' does the same thing, but really tail -1 is the right tool for the job. – imiric Jul 9 '17 at 19:39
  • Thanks! I managed to extract the last line in /proc/cpuinfo and trim the leading zero with ease (used for extracting RPi3 serial number with ease): sed '$!d s/.*: 0\+//' /proc/cpuinfo – Yaron Jan 8 '18 at 7:40

You're looking for tail :

ls -ltr | tail -n 1

This will display only the last line of ls -ltr's output. You can control the number of lines by changing the value after -n; if you omit -n 1 entirely you'll get ten lines.


With awk:

ls -ltr | awk 'END { print }'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.