Autocoder

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 coveredgrep, find, awk, sed, xargs, sort, uniq, tar, ps, kill, ss, df, du
StyleOne command or pipeline per question with an explanation
Shellbash 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. 1

    How do you search for a word recursively in all files under a directory, ignoring case, and show line numbers?

    easy

    Use 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.

    bash
    grep -rni "timeout" /etc/nginx
    grep -rl --include="*.py" "import requests" .
  2. 2

    How do you find all files larger than 100 MB modified in the last 7 days under /var/log?

    easy

    find 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.

    bash
    find /var/log -type f -size +100M -mtime -7 -exec ls -lh {} +
  3. 3

    How do you list the 10 largest files or directories in a path?

    medium

    du reports sizes, sort -h orders human-readable sizes, and head takes the top ten. -x stays on one file system.

    bash
    du -ah /var 2>/dev/null | sort -rh | head -10
    du -xh --max-depth=1 / | sort -rh | head
  4. 4

    Given an access log where the first field is the client IP, show the top 5 IPs by request count.

    medium

    Extract 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.

    bash
    awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -5
  5. 5

    How do you use awk to sum a column?

    medium

    awk 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,.

    bash
    awk '{sum += $3} END {print sum}' data.txt
    awk -F, 'NR>1 {s += $3} END {printf "%.2f\n", s}' sales.csv
  6. 6

    How do you replace a string in a file, and in many files, with sed?

    medium

    s/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.

    bash
    sed -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 20
  7. 7

    What does xargs do and why is it used instead of a loop?

    medium

    xargs 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.

    bash
    find . -name "*.tmp" -print0 | xargs -0 rm -f
    cat urls.txt | xargs -n 1 -P 4 curl -s -O
  8. 8

    How do you count lines, words and characters, and the number of unique values in a file?

    medium

    wc -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.

    bash
    wc -l file.txt
    cut -d, -f2 data.csv | sort -u | wc -l
    sort file.txt | uniq -d
  9. 9

    How do you view the last lines of a log and follow new lines as they are written?

    medium

    tail -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.

    bash
    tail -n 50 /var/log/syslog
    tail -F app.log | grep --line-buffered ERROR
  10. 10

    How do you find which process is listening on port 8080 and stop it?

    medium

    ss -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.

    bash
    ss -ltnp | grep :8080
    lsof -i :8080
    kill 12345        # SIGTERM
    kill -9 12345     # last resort
  11. 11

    What is the difference between kill, kill -9 and pkill?

    medium

    kill 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. 12

    How do you create, list and extract a gzip tar archive?

    medium

    Flags: 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.

    bash
    tar -czvf backup.tar.gz /home/user/project
    tar -tzf backup.tar.gz
    tar -xzvf backup.tar.gz -C /tmp/restore
  13. 13

    How do you change permissions and ownership recursively?

    medium

    chmod 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.

    bash
    chown -R alice:devs /srv/app
    find /srv/app -type d -exec chmod 755 {} +
    find /srv/app -type f -exec chmod 644 {} +
  14. 14

    What is the difference between df and du?

    medium

    df 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. 15

    How do you display processes and sort them by memory usage?

    medium

    ps 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.

    bash
    ps aux --sort=-%mem | head -6
    ps -eo pid,ppid,stat,cmd | grep defunct
  16. 16

    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.

    bash
    make > 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 file
  17. 17

    How do you compare two files, and how do you find lines in one file but not in another?

    hard

    diff -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.

    bash
    diff -u old.conf new.conf
    comm -23 <(sort a.txt) <(sort b.txt)
    grep -Fvxf b.txt a.txt
  18. 18

    How do you schedule a job to run every day at 2 AM, and what are the five cron fields?

    hard

    Edit 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.

    bash
    0 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

More in CS Fundamentals Interview Questions

See all in this section