Linux interview questions: commands with real output
The command-line questions system administrators, DevOps engineers and developers get, each answered in plain words and run on sample files.
Linux interviews test whether you can work on the command line: searching files with grep and find, slicing text with cut, awk and sed, combining tools with pipes and redirection, reading exit codes, quoting correctly, and writing safe scripts with set -euo pipefail and trap. Below are 17 questions from basic to advanced; the commands run on small sample files and the output shown is real.
The files every command runs on
Each example starts in a fresh, empty directory that this script fills with a short application log and a small CSV of fictional staff, so examples do not affect each other.
mkdir -p logs data
printf '%s\n' '2026-11-02 09:14:01 INFO api started on port 8080' '2026-11-02 09:15:22 WARN api slow response 1840ms /search' '2026-11-02 09:16:05 ERROR db connection refused host=db1' '2026-11-02 09:16:07 INFO api retry scheduled' '2026-11-02 09:17:44 ERROR api timeout /checkout' '2026-11-02 09:18:10 INFO worker job 42 done' '2026-11-02 09:19:31 ERROR db connection refused host=db2' > logs/app.log
printf '%s\n' 'name,dept,salary' 'Ava,Engineering,98000' 'Ben,Sales,61000' 'Cara,Engineering,112000' 'Dev,Support,52000' 'Eli,Sales,67000' > data/staff.csv
printf 'alpha\nbeta\n' > notes.txtBasic linux interview questions
Basic1. How do you search for text inside a file?
grep prints the lines that match a pattern. -n adds line numbers, -i ignores case, -c counts matching lines instead of printing them, and -v inverts the match.
Bashgrep -n ERROR logs/app.log
echo '---'
grep -c ERROR logs/app.log
echo '---'
grep -ic 'info' logs/app.logOutput (GNU bash 5.3.9)3:2026-11-02 09:16:05 ERROR db connection refused host=db1 5:2026-11-02 09:17:44 ERROR api timeout /checkout 7:2026-11-02 09:19:31 ERROR db connection refused host=db2 --- 3 --- 3
Checked against: GNU grep manual
Basic2. How do you show the first or last lines of a file?
head prints the start of a file and tail the end; -n sets how many lines. tail -f keeps the file open and prints new lines as they arrive, which is how people watch a live log.
Bashhead -n 2 logs/app.log
echo '---'
tail -n 2 logs/app.logOutput (GNU bash 5.3.9)2026-11-02 09:14:01 INFO api started on port 8080 2026-11-02 09:15:22 WARN api slow response 1840ms /search --- 2026-11-02 09:18:10 INFO worker job 42 done 2026-11-02 09:19:31 ERROR db connection refused host=db2
Basic3. How do you count lines, words and characters?
wc counts them: -l lines, -w words, -c bytes. Piping into wc -l is the usual way to count results from another command.
Bashwc -l logs/app.log data/staff.csv
echo '---'
grep ERROR logs/app.log | wc -lOutput (GNU bash 5.3.9)7 logs/app.log 6 data/staff.csv 13 total --- 3
Basic4. What is the difference between >, >> and 2>?
> sends standard output to a file and replaces its contents, >> appends, and 2> redirects standard error (file descriptor 2). 2>&1 sends errors to wherever standard output is going.
Bashecho first > out.txt
echo second >> out.txt
cat out.txt
echo '---'
ls missing-file 2> err.txt
echo "exit code: $?"
wc -l < err.txtOutput (GNU bash 5.3.9)first second --- exit code: 2 1
Checked against: GNU Bash Reference Manual
Basic5. How do you find files by name or type?
find walks a directory tree and filters by tests such as -name, -type f (regular file) or -type d (directory). Its output order is not sorted, so pipe it to sort when the order matters.
Bashfind . -type f -name '*.csv' | sort
echo '---'
find . -type d | sortOutput (GNU bash 5.3.9)./data/staff.csv --- . ./data ./logs
Basic6. How do you pick out a column from a CSV or log line?
cut splits on a single delimiter (-d) and keeps fields (-f). awk splits on whitespace by default, or on -F, and lets you print, compute and filter per field.
Bashcut -d, -f1,3 data/staff.csv
echo '---'
awk '{print $3, $4}' logs/app.log | head -n 3Output (GNU bash 5.3.9)name,salary Ava,98000 Ben,61000 Cara,112000 Dev,52000 Eli,67000 --- INFO api WARN api ERROR db
Intermediate linux interview questions
Intermediate7. How do you count the most frequent values?
Extract the field, sort it so equal values sit together, count runs with uniq -c, then sort numerically in reverse. This sort | uniq -c | sort -rn pattern appears in a lot of log questions.
Bashawk '{print $3}' logs/app.log | sort | uniq -c | sort -rnOutput (GNU bash 5.3.9) 3 INFO
3 ERROR
1 WARNIntermediate8. How do you sum or average a column with awk?
awk keeps variables between lines. NR is the line number, so NR>1 skips the header; the END block runs once after the last line.
Bashawk -F, 'NR>1 {sum += $3; n++} END {print "total", sum; print "average", sum/n}' data/staff.csvOutput (GNU bash 5.3.9)total 390000 average 78000
Intermediate9. How do you group and total values per key?
Use an awk associative array keyed by the group, then print it in the END block. awk does not promise any order for a for-in loop, so sort the result.
Bashawk -F, 'NR>1 {s[$2] += $3} END {for (d in s) print d, s[d]}' data/staff.csv | sortOutput (GNU bash 5.3.9)Engineering 210000 Sales 128000 Support 52000
Intermediate10. How do you replace text with sed?
sed 's/old/new/' replaces the first match on each line and s/old/new/g replaces every match. -i edits the file in place, so try the command without -i first.
Bashsed 's/ERROR/E/' logs/app.log | grep '^2026' | head -n 3
echo '---'
sed -n '3,4p' logs/app.logOutput (GNU bash 5.3.9)2026-11-02 09:14:01 INFO api started on port 8080 2026-11-02 09:15:22 WARN api slow response 1840ms /search 2026-11-02 09:16:05 E db connection refused host=db1 --- 2026-11-02 09:16:05 ERROR db connection refused host=db1 2026-11-02 09:16:07 INFO api retry scheduled
Intermediate11. What do $?, && and || do?
$? holds the exit status of the last command: 0 means success, anything else is failure. a && b runs b only if a succeeded; a || b runs b only if a failed.
Bashgrep -q timeout logs/app.log && echo 'found timeout'
grep -q panic logs/app.log || echo 'no panic lines'
grep -q panic logs/app.log
echo "status: $?"Output (GNU bash 5.3.9)found timeout no panic lines status: 1
Checked against: GNU Bash Reference Manual
Intermediate12. What is the difference between single and double quotes in bash?
Single quotes keep every character literal, so $name stays as text. Double quotes still expand variables and command substitution, but stop the result from being split into separate words.
Bashname='Ava Lee'
echo 'single: $name'
echo "double: $name"
set -- $name
echo "unquoted gives $# words"
set -- "$name"
echo "quoted gives $# word"Output (GNU bash 5.3.9)single: $name double: Ava Lee unquoted gives 2 words quoted gives 1 word
Intermediate13. How do you loop over lines of a file?
Use while IFS= read -r line; do ...; done < file. -r keeps backslashes, IFS= keeps leading spaces, and redirecting into the loop avoids running it in a subshell.
Bashcount=0
while IFS=, read -r name dept salary; do
[ "$dept" = Sales ] && count=$((count + 1))
done < data/staff.csv
echo "sales staff: $count"Output (GNU bash 5.3.9)sales staff: 2
Advanced linux interview questions
Advanced14. How do you extract only the matching part of a line?
grep -o prints just the matched text, one match per line, and -E enables extended regular expressions. That turns a log into a list of values you can count.
Bashgrep -oE 'host=[a-z0-9]+' logs/app.log
echo '---'
grep -oE '[0-9]+ms' logs/app.logOutput (GNU bash 5.3.9)host=db1 host=db2 --- 1840ms
Checked against: GNU grep manual
Advanced15. What does set -euo pipefail do, and why do scripts use it?
-e stops the script when a command fails, -u treats unset variables as errors, and -o pipefail makes a pipeline fail if any command in it fails, not just the last one. Without pipefail, a failing grep in the middle of a pipeline is hidden.
Bashfalse | true
echo "without pipefail: $?"
set -o pipefail
false | true
echo "with pipefail: $?"Output (GNU bash 5.3.9)without pipefail: 0 with pipefail: 1
Checked against: GNU Bash Reference Manual
Advanced16. How do you clean up temporary files even when a script fails?
trap registers a command to run when the shell exits or receives a signal. A trap on EXIT runs on success and on failure, so it is the standard place to delete temp files.
Bashbash -c 'tmp=scratch.tmp; touch "$tmp"; trap "rm -f $tmp; echo cleaned up" EXIT; echo working; exit 3'
echo "script exit: $?"
ls scratch.tmp 2>/dev/null || echo 'temp file is gone'Output (GNU bash 5.3.9)working cleaned up script exit: 3 temp file is gone
Checked against: GNU Bash Reference Manual
Advanced17. What is /proc and what can you read from it?
/proc is a virtual file system the kernel generates on demand: it holds no files on disk. Each running process has a numbered directory, and files such as /proc/meminfo and /proc/cpuinfo report memory and CPU details. Tools like ps and free read from it. (No output is shown here because the build machine is not a Linux kernel.)
Checked against: Linux kernel documentation: the /proc filesystem
How to practice with this page
- Read the question, write your own command, then compare with the example.
- Explain each part of a pipeline out loud: interviewers want the reasoning.
- Recreate the sample files with the script above and run the commands yourself.
- Pair this page with networking and process basics if the role is in operations.
Related interview prep and tools
CI/CD, infrastructure and operations questions.
Open →QuestionsDocker interview questionsContainers, images and Dockerfiles.
Open →QuestionsNode.js interview questionsThe event loop, streams and modules, with real output.
Open →RoleSystem administrator interview questionsRole-specific questions.
Open →Free toolResume summary generatorDraft a summary for a Systems Administrator role; the title is filled in, you add your experience.
Open →Free toolLinkedIn headline generatorWrite a headline for a Systems Administrator search.
Open →Disclosure: TryHired makes HIRED, a paid job-search program, so we have a conflict of interest when we mention it. This question bank is free, with no TryHired signup.
Questions people ask
Which Linux commands come up often in interviews?
Do I need to know shell scripting for a Linux interview?
Were these examples run on Linux?
How this page is built
TryHired wrote these questions and answers. Every code block on this page was executed with GNU bash 5.3.9 when the page was built (2026-11-15), and the output shown is the real output; the build stops if any example fails. The question list is general practice: it does not predict what a particular employer will ask.
- Interviewers ask different questions for different roles and levels.
- Answers are short explanations, not complete references.
- Examples ran in GNU bash with GNU coreutils, grep, sed and awk on the build machine; these text commands behave the same on Linux distributions, but kernel-level details (processes, /proc, permissions on special files) are explained without output.
- GNU Bash Reference Manual (read 2026-11-15)
- GNU grep manual (read 2026-11-15)
- Linux kernel documentation: the /proc filesystem (read 2026-11-15)