
Today, I had a technical interview with a company, and a Senior Linux System Administrator asked me a question about a Linux command that really made me think.
The question was: “How do you show only the 10th line of a file?”
At first, the question seemed easy. I began with the command:
$ head -n 10 file_name
The command outputted the first 10 lines of the file. However, then came the challenge: how to extract only the 10th line from that file? I thought about using the tail command, but at that moment, I couldn’t piece together the solution. I kept pondering over it, and after the interview, I did a quick search and found three 🕵️♂️ elegant solutions.
🔢 Solution 1: Using awk
You can use the awk command with a condition to print only the 10th line:
$ awk -v line=10 'current_line==line' file_name
Here, current_line refers to the current line number, line=10 is specified and then the command should print only the 10th line or the line number specified on line variable.
🔢 Solution 2: Using sed
The sed command also provides a straightforward way to achieve this:
$ sed -n '10p' file_name
The -n flag suppresses the default output, and 10p tells sed to print only the 10th line of the file_name.
🌐 Solution 3: Combining head and tail
This approach I liked most combines head and tail:
$ head -n 10 file_name | tail -1
Here, head -n 10 extracts the first 10 lines, and tail -1 selects only the last line from those, which is the 10th line of the file.
I really would like to thank the Senior Linux System Administrator for making this question.










