Linux Commands Interview Questions with Examples
Last updated 2026-11-19
Interviewers test Linux commands by giving you a small problem: find the ten largest files, count the unique IP addresses in a log, kill the process listening on a port, or replace a string in every file. The expected answer is a short command or pipeline, and a quick explanation of each part. This page collects the commands that come up most, with one example for each and the reasoning behind the flags.
All examples run on GNU Linux with bash. For the concepts behind them (permissions, processes, signals) see Linux interview questions, and once a pipeline works you can wrap it in a script as in shell scripting interview questions. The wider DevOps picture is in how to crack a DevOps interview.
| Commands covered | grep, find, awk, sed, xargs, sort, uniq, tar, ps, kill, ss, df, du |
|---|---|
| Style | One command or pipeline per question with an explanation |
| Shell | bash on GNU Linux |
How to explain a pipeline in an interview
Read it left to right and say what each stage does to the stream: produce lines, filter lines, transform columns, count or sort. Interviewers value hearing the data shape at each step more than a long list of flags. Mention edge cases such as spaces in file names and prefer find with -print0 and xargs -0 when file names are untrusted.
Linux Commands Interview Questions with Examples: questions with worked answers
1 How do you search for a word recursively in all files under a directory, ignoring case, and show line numbers?
easyUse grep with -r (recursive), -i (ignore case) and -n (line numbers). Add -l to print only file names, and --include to restrict file types. grep -rn matches both the file name and the line.
bashgrep -rni "timeout" /etc/nginx grep -rl --include="*.py" "import requests" .2 How do you find all files larger than 100 MB modified in the last 7 days under /var/log?
easyfind takes tests joined by AND. -type f restricts to files, -size +100M means more than 100 megabytes, and -mtime -7 means modified less than 7 days ago. Add -exec ls -lh {} + to display them.
bashfind /var/log -type f -size +100M -mtime -7 -exec ls -lh {} +3 How do you list the 10 largest files or directories in a path?
mediumdu reports sizes, sort -h orders human-readable sizes, and head takes the top ten. -x stays on one file system.
bashdu -ah /var 2>/dev/null | sort -rh | head -10 du -xh --max-depth=1 / | sort -rh | head4 Given an access log where the first field is the client IP, show the top 5 IPs by request count.
mediumExtract the field, sort so equal values are adjacent, count adjacent duplicates with uniq -c, sort numerically in reverse, and take five. uniq only collapses adjacent lines, so the first sort is essential.
bashawk '{print $1}' access.log | sort | uniq -c | sort -nr | head -55 How do you use awk to sum a column?
mediumawk reads a line at a time and splits it into fields $1, $2, and so on. Accumulate in a variable and print it in the END block. NR is the line number and NF the number of fields. To sum the third column of a CSV set the field separator with -F,.
bashawk '{sum += $3} END {print sum}' data.txt awk -F, 'NR>1 {s += $3} END {printf "%.2f\n", s}' sales.csv6 How do you replace a string in a file, and in many files, with sed?
mediums/old/new/g substitutes every occurrence on each line. -i edits the file in place (use -i.bak to keep a backup). For many files combine find or grep -l with xargs. Use a different delimiter such as | when the pattern contains slashes.
bashsed -i 's/http:/https:/g' config.txt grep -rl "oldname" src | xargs sed -i 's/oldname/newname/g' sed -n '10,20p' file.txt # print lines 10 to 207 What does xargs do and why is it used instead of a loop?
mediumxargs reads items from standard input and builds command lines from them, running the command with many arguments at once, which is faster than starting one process per item. Because file names may contain spaces, pair find -print0 with xargs -0. -n 1 passes one argument per invocation and -P runs several in parallel.
bashfind . -name "*.tmp" -print0 | xargs -0 rm -f cat urls.txt | xargs -n 1 -P 4 curl -s -O8 How do you count lines, words and characters, and the number of unique values in a file?
mediumwc -l counts lines, -w words, -c bytes. For unique lines use sort -u or sort | uniq. To count distinct values in the second CSV column use cut then sort -u then wc -l. uniq -d shows only duplicated lines.
bashwc -l file.txt cut -d, -f2 data.csv | sort -u | wc -l sort file.txt | uniq -d9 How do you view the last lines of a log and follow new lines as they are written?
mediumtail -n 50 prints the last 50 lines, tail -f follows the file as it grows, and tail -F also follows through log rotation, reopening the file by name. Combine with grep to filter, using --line-buffered so output is not delayed in the pipe. less +F gives follow mode with the option to scroll back.
bashtail -n 50 /var/log/syslog tail -F app.log | grep --line-buffered ERROR10 How do you find which process is listening on port 8080 and stop it?
mediumss -ltnp lists listening TCP sockets with their processes (older systems use netstat -tulnp). lsof -i :8080 does the same for one port. Then terminate with kill using the PID, trying SIGTERM first and SIGKILL only if it does not exit.
bashss -ltnp | grep :8080 lsof -i :8080 kill 12345 # SIGTERM kill -9 12345 # last resort11 What is the difference between kill, kill -9 and pkill?
mediumkill sends SIGTERM (15) by default, which lets the program clean up. kill -9 sends SIGKILL, which cannot be caught, so there is no cleanup and children or temp files may be left behind. pkill and killall select processes by name or attributes instead of PID, for example pkill -f "python app.py". Check with pgrep before pkill so you know what will match.
12 How do you create, list and extract a gzip tar archive?
mediumFlags: c create, x extract, t list, v verbose, f file name, z gzip (j for bzip2, J for xz). -C changes the destination directory when extracting.
bashtar -czvf backup.tar.gz /home/user/project tar -tzf backup.tar.gz tar -xzvf backup.tar.gz -C /tmp/restore13 How do you change permissions and ownership recursively?
mediumchmod changes mode bits and chown changes owner and group. -R recurses. To set directories to 755 and files to 644, use find with -type, because a plain chmod -R 755 would make every file executable.
bashchown -R alice:devs /srv/app find /srv/app -type d -exec chmod 755 {} + find /srv/app -type f -exec chmod 644 {} +14 What is the difference between df and du?
mediumdf reports free and used space per mounted file system from the file system's own counters (df -h for readable units, df -i for inodes). du walks the directory tree and adds up file sizes (du -sh dir). They can disagree: a deleted file still open by a process counts in df but not in du, until the process closes it. Mount points and hidden files can also cause differences.
15 How do you display processes and sort them by memory usage?
mediumps aux lists all processes with user, CPU and memory percentages. ps aux --sort=-%mem sorts descending by memory, and head limits it. top then pressing M sorts by memory in the interactive view, and htop is a friendlier alternative. ps -ef shows the parent PID, useful for tracing trees.
bashps aux --sort=-%mem | head -6 ps -eo pid,ppid,stat,cmd | grep defunct16 Explain the difference between >, >>, 2>&1 and the pipe, using one example.
hard> overwrites a file with stdout, >> appends, and | connects stdout of one command to stdin of another. 2>&1 duplicates descriptor 2 (stderr) onto wherever descriptor 1 currently points, so order matters. In the first line below both streams go to the file; in the second, stderr goes to the terminal because 2>&1 was applied before stdout was redirected.
bashmake > build.log 2>&1 # both streams in build.log make 2>&1 > build.log # errors still on screen make 2>&1 | tee build.log # both on screen and in file17 How do you compare two files, and how do you find lines in one file but not in another?
harddiff -u shows a unified diff. For set operations on sorted lists, comm compares columns: lines only in file 1, only in file 2, and in both; -23 prints lines only in the first file. grep -Fvxf b.txt a.txt does it without sorting by using b.txt as a list of fixed patterns, -v inverts and -x requires whole-line matches.
bashdiff -u old.conf new.conf comm -23 <(sort a.txt) <(sort b.txt) grep -Fvxf b.txt a.txt18 How do you schedule a job to run every day at 2 AM, and what are the five cron fields?
hardEdit the user's crontab with crontab -e. The fields are minute, hour, day of month, month and day of week, so 0 2 * * * means 02:00 every day. Cron runs with a minimal environment, so use absolute paths and redirect output, otherwise failures are silent. systemd timers are the modern alternative with better logging.
bash0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1 */15 * * * * /usr/bin/python3 /opt/jobs/poll.py # every 15 minutes
Frequently asked questions
Which Linux commands are asked most in interviews?
grep, find, awk, sed, sort, uniq, wc, cut, tail, ps, top, kill, chmod, chown, df, du, tar, ssh and curl. Be ready to combine three or four of them in a pipeline.
What is the difference between grep, egrep and fgrep?
grep uses basic regular expressions, egrep (grep -E) uses extended ones with +, ?, | and grouping, and fgrep (grep -F) treats the pattern as a fixed string, which is fastest and safe for text with special characters.
What is the difference between find and locate?
find walks the live directory tree and can filter by size, time, type and permissions, but is slower. locate searches a prebuilt database updated by updatedb, so it is instant but may be out of date and lacks those filters.
How do I learn a command quickly during an interview?
Use man command, command --help or tldr. In an interview it is acceptable to say which option you would look up, as long as you know what the tool does and what the output looks like.
Related guides
- Linux Interview Questions and Answers: Concepts, Permissions and Processes
- Shell Scripting Interview Questions with Bash Examples
- File System Interview Questions: Inodes, Allocation and Disk Scheduling
- Git Interview Questions and Answers with Commands
- Operating Systems Interview Questions with Worked Answers
- Docker Interview Questions and Answers
More in CS Fundamentals Interview Questions
- Linux Interview Questions and Answers: Concepts, Permissions and Processes
- Load Balancing Interview Questions and Answers
- Low-Level Design Interview Questions with Code
- Memory Management Interview Questions with Numericals
- Message Queue and Kafka Interview Questions
- Microservices Interview Questions and Answers
- MongoDB Interview Questions and Answers
- Network Security Interview Questions: Cryptography, Firewalls and Attacks
- Normalization Interview Questions with Worked Examples
- NoSQL Interview Questions and Answers
- OOPs Interview Questions and Answers
- Operating Systems Interview Questions with Worked Answers