Have you ever needed to display line number (n) from some configuration file on your Linux command line? Here is how to do it with the help of head, tail, and piping (|).

head -n 10 /etc/passwd | tail -n 1

# or shorter version

head -10 /etc/passwd | tail -1

This command will display line number 10 from the /etc/passwd configuration file.

The output is as follows:

news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

The output will be different from system to system.

If you want a penultimate line from the /etc/passwd file, you can use:

tail -n 2 /etc/passwd | head -n 1

For me the output is nginx system user.

nginx:x:112:115:nginx user,,,:/nonexistent:/bin/false

Pro Tip

With older versions of head and tail, you had to use the -n option to specify the number of lines you wanted to see. With current versions of both utilities, you may also omit the -n option. So, using either tail -5 /etc/passwd or tail -n 5 /etc/passwd gives you the exact same results.

You can do this with a sed command too, however, sed is what I consider a complex tool for seasoned Linux users.

sed -n 10p /etc/passwd

Nice way to output a specific line from a file to standard output without the need of using sed or some other more complex tools. I always try to keep things simple and head and tail are tools that allow me to be simple on the command-line.