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!
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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!
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.
Straight forward with awk
:
$ ls / | awk 'NR==4'
etc
sed
because NR
presumably stands for "number (of) record", and the equality operator makes sense. Not clear what p
means without having to look it up, nor -n
, so harder to remember.
There may be better ways to do this, but this is probably the easiest: ls | head -<n> | tail -1
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.
ls "$@"
to pass lsn
's arguments down to ls
. Of course, after using shift
to take out $1
.
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 so
rted by m
time (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.
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 ~ $