How do I display the nth result of an ls command?

I used this command to display the first result of files in my directory.

  ls | head -n 1

My simple question is, how can I modify this command to display say the nth result?

Thanks!

Asked By: Arjun

||

There may be better ways to do this, but this is probably the easiest: ls | head -<n> | tail -1

Answered By: John

You could use sed to select a single line, for example line 12:

ls | sed -n 12p

Option -n asks sed not to print every line (which is what it normally does), and 12p asks to print the pattern space when the address is 12.

Answered By: dhag

Straight forward with awk:

$ ls / | awk 'NR==4'
etc
Answered By: user62916

I have this in my .bashrc.

lsn () {
    ls ${@:2} | head -n $1 | tail -n 1
}

This is called as such: lsn 4 for example. The $2 allows you to use
options on ls so lsn 4 -lah is also valid.

Note: when using the -l flag on ls there is an additional line at the top of the result. Which would skew this function’s result.

Answered By: robjtede

With tail and head (added nl for clarity). Let’s fetch the fifth line :

ychaouche@ychaouche-PC ~ $ ls | nl
     1  total 8.3M
     2  -rw-r--r--  1 ychaouche ychaouche  20K Jul  8 15:53 2.docx
     3  -rw-r--r--  1 ychaouche ychaouche  20K Jul  8 15:53 3.docx
     4  drwxr-xr-x  2 ychaouche ychaouche 4.0K Jul  7 10:11 AUDIO
     5  drwxr-xr-x  3 ychaouche ychaouche 4.0K May 28 09:33 BACKUPS
     6  drwxr-xr-x  6 ychaouche ychaouche 4.0K May 17 13:49 BZR
     7  drwxr-xr-x  6 ychaouche ychaouche 4.0K Jun 15 17:10 CODE
     8  drwxr-xr-x  2 ychaouche ychaouche 4.0K May 25 10:24 Desktop
     9  -rw-r--r--  1 ychaouche ychaouche  34K Dec  2  2014 dget_problem.ods
    10  drwxr-xr-x 12 ychaouche ychaouche 4.0K Jul  7 10:00 DOCUMENTS
    11  drwx------  2 ychaouche ychaouche 4.0K Apr  6 11:39 Downloads
    12  drwxr-xr-x 16 ychaouche ychaouche 4.0K Jul 12 11:06 DOWNLOADS
    13  drwxr-xr-x  6 ychaouche ychaouche 4.0K Jun 22 15:38 IMAGES
    14  -rw-r--r--  1 ychaouche ychaouche  690 Jan 25 14:25 IT_Alias.vcf
    15  drwxr-xr-x  4 ychaouche ychaouche 4.0K Dec  2  2014 MACONF
    16  -rw-r--r--  1 ychaouche ychaouche 6.2M May 12 10:44 mtr.ogv
    17  drwxr-xr-x  4 ychaouche ychaouche 4.0K Jan  5  2015 MUSIQUE
    18  drwxr-xr-x  2 ychaouche ychaouche 4.0K Mar  2 16:29 NOTES
    19  -rw-r--r--  1 ychaouche ychaouche 2.0M May 12 10:42 out.ogv
    20  -rw-r--r--  3 ychaouche ychaouche  62K Apr  2  2014 php-mode.el
    21  drwxr-xr-x  2 ychaouche ychaouche 4.0K Apr  8 13:25 Pictures
    22  drwxr-xr-x  2 ychaouche ychaouche 4.0K Jan  6  2015 Projects
    23  -rw-r--r--  1 ychaouche ychaouche  353 Jan 12  2015 proxylog.dat
    24  drwxr-xr-x  2 ychaouche ychaouche 4.0K Mar 19 15:35 public_html
    25  -rw-r--r--  1 ychaouche ychaouche   21 Nov 23  2014 registrationcode
    26  drwxr-xr-x  6 ychaouche ychaouche 4.0K Jun 21 10:55 REPOS
    27  drwxr-xr-x  2 ychaouche ychaouche 4.0K Jul 20 10:56 SCRIPTS
    28  drwxr-xr-x  8 ychaouche ychaouche 4.0K Jun 11 10:36 SRV
    29  drwxr-xr-x 14 ychaouche ychaouche 4.0K Jun 30 16:26 TMP
    30  drwxr-xr-x  5 ychaouche ychaouche 4.0K Jun 28 16:56 VIDEOS
    31  drwxr-xr-x  7 ychaouche ychaouche 4.0K Mar 18 11:35 VirtualBox VMs
    32  drwxr-xr-x  3 ychaouche ychaouche 4.0K Jan 22 14:28 VIRTUALENVS
    33  drwxr-xr-x  2 ychaouche ychaouche 4.0K Jul  7 12:24 WTMP
ychaouche@ychaouche-PC ~ $ 

This will jump to the fifth line :

ychaouche@ychaouche-PC ~ $ ls | nl | tail -n +5
     5  drwxr-xr-x  3 ychaouche ychaouche 4.0K May 28 09:33 BACKUPS
     6  drwxr-xr-x  6 ychaouche ychaouche 4.0K May 17 13:49 BZR
     7  drwxr-xr-x  6 ychaouche ychaouche 4.0K Jun 15 17:10 CODE
     8  drwxr-xr-x  2 ychaouche ychaouche 4.0K May 25 10:24 Desktop
     9  -rw-r--r--  1 ychaouche ychaouche  34K Dec  2  2014 dget_problem.ods
    10  drwxr-xr-x 12 ychaouche ychaouche 4.0K Jul  7 10:00 DOCUMENTS
    11  drwx------  2 ychaouche ychaouche 4.0K Apr  6 11:39 Downloads
    12  drwxr-xr-x 16 ychaouche ychaouche 4.0K Jul 12 11:06 DOWNLOADS
    13  drwxr-xr-x  6 ychaouche ychaouche 4.0K Jun 22 15:38 IMAGES
    14  -rw-r--r--  1 ychaouche ychaouche  690 Jan 25 14:25 IT_Alias.vcf
    15  drwxr-xr-x  4 ychaouche ychaouche 4.0K Dec  2  2014 MACONF
    16  -rw-r--r--  1 ychaouche ychaouche 6.2M May 12 10:44 mtr.ogv
    17  drwxr-xr-x  4 ychaouche ychaouche 4.0K Jan  5  2015 MUSIQUE
    18  drwxr-xr-x  2 ychaouche ychaouche 4.0K Mar  2 16:29 NOTES
    19  -rw-r--r--  1 ychaouche ychaouche 2.0M May 12 10:42 out.ogv
    20  -rw-r--r--  3 ychaouche ychaouche  62K Apr  2  2014 php-mode.el
    21  drwxr-xr-x  2 ychaouche ychaouche 4.0K Apr  8 13:25 Pictures
    22  drwxr-xr-x  2 ychaouche ychaouche 4.0K Jan  6  2015 Projects
    23  -rw-r--r--  1 ychaouche ychaouche  353 Jan 12  2015 proxylog.dat
    24  drwxr-xr-x  2 ychaouche ychaouche 4.0K Mar 19 15:35 public_html
    25  -rw-r--r--  1 ychaouche ychaouche   21 Nov 23  2014 registrationcode
    26  drwxr-xr-x  6 ychaouche ychaouche 4.0K Jun 21 10:55 REPOS
    27  drwxr-xr-x  2 ychaouche ychaouche 4.0K Jul 20 10:56 SCRIPTS
    28  drwxr-xr-x  8 ychaouche ychaouche 4.0K Jun 11 10:36 SRV
    29  drwxr-xr-x 14 ychaouche ychaouche 4.0K Jun 30 16:26 TMP
    30  drwxr-xr-x  5 ychaouche ychaouche 4.0K Jun 28 16:56 VIDEOS
    31  drwxr-xr-x  7 ychaouche ychaouche 4.0K Mar 18 11:35 VirtualBox VMs
    32  drwxr-xr-x  3 ychaouche ychaouche 4.0K Jan 22 14:28 VIRTUALENVS
    33  drwxr-xr-x  2 ychaouche ychaouche 4.0K Jul  7 12:24 WTMP
ychaouche@ychaouche-PC ~ $ 

Then, obviously head -1

ychaouche@ychaouche-PC ~ $ ls | nl | tail -n +5 | head -1
     5  drwxr-xr-x  3 ychaouche ychaouche 4.0K May 28 09:33 BACKUPS
ychaouche@ychaouche-PC ~ $ 
Answered By: ychaouche

With zsh and glob qualifiers:

ls -d -- *([7])

or

print -rl -- *([7])

will print the 7th file from the list of files sorted by name.

ls -d -- *(om[7])

or

print -rl -- *(om[7])

will print the 7th file from the list of files sorted by mtime (newest first – similar to extracting the 7th entry from ls -t output).


Ideally, if you’re processing ls output, you should use something like ls -q | command(s) where -q prints ? instead of funky chars (like newlines) and command(s) processes only N lines of output, prints the Nth and then stops.

Answered By: don_crissti
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.