Interview Q&A / Linux & Shell

INTERVIEW Q&A · 63 QUESTIONS

Linux & Shell interview questions

Permissions, processes, pipes and the commands you are expected to know cold.

  1. How do you count how often each line appears, sorted from most to least?

    Sort the input, count duplicates with uniq -c, then sort numerically in reverse: sort file | uniq -c | sort -nr. uniq only compares adjacent lines, so the first sort is required. Add head to keep the top results.

    BeginnerQuick answer1 min read
  2. How do you print specific columns of a command's output with awk?

    awk splits each line into fields on whitespace and gives them as $1, $2 and so on, with $NF as the last and $0 as the whole line. awk '{print $1, $3}' prints columns 1 and 3. Use -F to set another separator, such as -F: for /etc/passwd.

    BeginnerQuick answer1 min read
  3. How do you replace text in a file using sed?

    sed 's/old/new/' replaces the first match on each line; add g to replace every match on a line. Add -i to edit the file in place, ideally with a backup suffix like -i.bak, otherwise sed only prints the changed text.

    BeginnerQuick answer1 min read
  4. What do the grep options -i, -v, -c, -n and -w do?

    -i ignores case, -v inverts the match to show non-matching lines, -c prints only a count of matching lines, -n adds line numbers and -w matches whole words only. They combine, so grep -ivn error file works.

    BeginnerQuick answer1 min read
  5. What does the wc command count, and how do you use its options?

    wc counts lines, words and bytes in a file or input. wc -l counts lines, -w words, -c bytes and -m characters. It is most often used at the end of a pipeline, such as grep error log | wc -l, to count matches.

    BeginnerQuick answer1 min read
  6. When do you need grep -E, and what is the difference from basic grep patterns?

    Basic grep treats +, ?, | and () as ordinary characters unless escaped. grep -E (extended regex) gives them their regex meaning, so grep -E 'cat|dog' matches either word. Use -F when you want a plain fixed string with no regex at all.

    IntermediateQuick answer1 min read
  7. When would you use cut instead of awk?

    cut extracts fixed fields by a single-character delimiter or character positions and is simple and fast. awk handles runs of spaces, conditions and calculations. Use cut for neat CSV-like data and awk for messy whitespace-aligned output.

    IntermediateQuick answer1 min read