RSS Feed

List the last 10 files in a directory and order

Listing of files is a common procedure day in day out when on the command line and more often than not you will want to list the last X amount of files. Doing so is pretty simple:

ls -ltr | head -n 10

Let’s break it down. Listing files in a directory:

ls -l

List files by time (Ascending order):

ls -lt

List files by time in descending order. -r represents reverse order:

ls -ltr

Limit the results using head. Head takes a list (line by line) and heads off the results:

ls -ltr | head -n X

Where -n is number and X is the value:

ls -ltr | head -n 10

Like I said, it’s pretty simple.

Posted in Linux on the 12th April 2010

2 people have spoken their minds!

  1. IByDev says:

    I found your blog a couple weeks ago but it wasn’t being updated so I didn’t look twice though came back yesterday to take a look and what do you know, a post on limiting files in a list, just what I needed.

    Thanks srcnix, I will bookmark your blog.


Your email address will not be published. Required fields are marked *