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
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:
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.
With older versions of
tail, you had to use the
-noption to specify the number of lines you wanted to see. With current versions of both utilities, you may also omit the
-noption. So, using either
tail -5 /etc/passwdor
tail -n 5 /etc/passwdgives 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
tail are tools that allow me to be simple on the command-line.