Linux Career Pack

Linux interview workbook

Free preview · English · Edition 1.0

Download Markdown

Set up your practice directory

This free English workbook is for Linux learners preparing to explain their reasoning in a technical interview. You will diagnose three small incidents and write two Bash scripts against the same files. Use Bash on Linux or WSL with GNU Coreutils, grep and awk. Everything runs as your normal user. The examples use a real local shell. The BashQuest browser simulator covers a smaller command set.

Run this setup once in a new terminal. Keep using that terminal so every example starts in the same temporary directory.

work=$(mktemp -d) && cd -- "$work" || exit 1
mkdir -p logs archive bin
printf '%s\n' 'INFO started' 'ERROR disk full' 'INFO ready' 'ERROR retry' > 'logs/app log'
printf '%s\n' 'INFO started' > 'archive/app log'
printf '%s\n' 'worker 12' 'web 7' 'worker 5' > jobs.txt

The files are disposable. In a shared environment, review the path printed by pwd before editing any file. Each incident below gives you a check you can perform before reading its explanation.

Five interview questions

  1. A colleague says two files have the same name. How can you tell which one a command will read? A relative pathname starts at the current working directory. An absolute pathname starts at the filesystem root. Run pwd and compare wc -l < 'logs/app log' with wc -l < 'archive/app log'. The counts here are 4 and 1. The basename alone cannot identify the input.
  2. Why can a line count differ from the number of records you see? GNU wc -l counts newline characters. A final record can end without one. Compare printf x | wc -l with printf 'x\n' | wc -l. Expect 0 and 1.
  3. Why does a script work on one log and fail after someone renames it? An unquoted variable expansion can be split into words and expanded as a filename pattern. The fixture deliberately contains a space. Assign log='logs/app log' and use wc -l < "$log" to pass one path through input redirection.
  4. A log search produced no matching lines. How do you distinguish that from an unreadable log? GNU grep returns 0 for a match, 1 for no match and 2 for an error. Capture the status immediately. In the second incident below, both searches produce no matching lines on standard output, but only one indicates a problem accessing the data.
  5. Why might a successful pipeline hide a failed stage? Bash normally reports the last stage’s exit status. Compare (false | true); echo "$?" and (set -o pipefail; false | true); echo "$?" in Bash. These print 0 and 1. The subshell confines the option to this experiment.

Three troubleshooting incidents

An alert counted the wrong records

A dashboard reports one record for the current run. Its job reads the archived copy. First predict the counts, then run this read-only comparison.

wc -l < 'archive/app log'
wc -l < 'logs/app log'
grep -c '^ERROR ' 'logs/app log'

The results are 1, 4 and 2. The first difference identifies a path mismatch. The third count answers a different question about severity. Point the job at the current file, and check total records and error records separately. Running a broad search for the word “error” could count a message body or miss the uppercase severity marker.

A quiet health check concealed a missing input

A service should report “no alerts” only after it has read the log. Reproduce the two states first.

grep '^WARN ' 'logs/app log'
printf 'existing log status=%s\n' "$?"
grep '^WARN ' 'logs/missing'
printf 'missing log status=%s\n' "$?"

The first status is 1. The second is 2 on GNU grep, with a diagnostic on standard error. Preserve that diagnostic. Handle a missing input as a job error and reserve “no alerts” for status 1. Verify both branches each time you change the check.

A script runs through Bash but fails when launched directly

A new reporting utility works with bash bin/report. Direct execution in this fixture initially fails. Inspect the script and its mode before changing anything.

printf '%s\n' '#!/usr/bin/env bash' 'printf "%s\n" ready' > bin/report
chmod 600 bin/report
bash bin/report
test -x bin/report
printf 'executable check=%s\n' "$?"
chmod u+x bin/report
./bin/report

Expect ready, a check status of 1, then ready. The interpreter could read the script before the owner had execute permission. Change only the owner execute bit. If the final launch still fails on a hardened system, check whether the temporary filesystem is mounted with noexec; use a permitted practice location without changing host policy.

Two scripting exercises

Count errors in one named log

Write a Bash script that accepts exactly one regular-file path, counts records whose first field is ERROR, prints zero for a readable empty file, and fails when the file cannot be read. Preserve spaces in paths. Save this solution as count-errors.sh inside the practice directory.

#!/usr/bin/env bash
if (( $# != 1 )); then
  printf 'Usage: %s LOG\n' "$0" >&2
  exit 2
fi
if [[ ! -f $1 ]]; then
  printf 'Expected a regular file\n' >&2
  exit 1
fi
awk '$1 == "ERROR" { count++ } END { print count + 0 }' < "$1"

Bash opens the input before starting awk. A read failure produces a nonzero status. Run bash count-errors.sh 'logs/app log' for 2, and bash count-errors.sh 'archive/app log' for 0. Also try a missing file and a call with no arguments. Check their exit statuses using echo "$?".

Add up work for a named service

A two-column fixture lists a service name and its completed-job count. Sum the counts for a literal service name passed as the first argument and read the file passed as the second. The exercise assumes well-formed records with a nonnegative integer in column two. Save as service-total.sh.

#!/usr/bin/env bash
if (( $# != 2 )); then
  printf 'Usage: %s SERVICE JOB_FILE\n' "$0" >&2
  exit 2
fi
awk -v service="$1" '$1 == service { total += $2 } END { print total + 0 }' < "$2"

Run bash service-total.sh worker jobs.txt for 17, then bash service-total.sh web jobs.txt for 7. A service with no rows yields 0. Equality tests the whole service name, so work also yields 0. As a follow-up, decide how you would reject malformed records before publishing a report.

Pocket reference

Question Command Evidence
Where am I? pwd Current directory
How many terminated records? wc -l < "$file" Newline count in one file
Does this pattern match? grep '^ERROR ' "$file" Matching lines and an exit status
What happened in the last command? printf '%s\n' "$?" Most recent exit status
What can my account execute? test -x "$file" Zero means the execute check passed

Here file stands for a path you set first, such as file='logs/app log'.

A three-day roadmap sample

  1. Paths and evidence. Complete the Linux foundations chapter, open its first practice quest, then diagnose the wrong-file incident. Write down one observation that would change your diagnosis.
  2. Signals from commands. Work through streams and pipelines and one linked quest. Explain why no match and missing input need separate outcomes. Reproduce both with the fixture.
  3. Scripts you can defend. Visit predictable Bash scripts, complete a scripting quest and test both solutions with an ordinary and an edge case. Explain one assumption you would check before deploying them.

References and usage

Consult the GNU Bash reference, GNU grep manual, GNU Coreutils manual and GNU awk manual for command behavior. These worked examples were written for BashQuest.

Free preview, edition 1.0, November 2026. You may save and print this preview for personal practice. For corrections or classroom reuse, contact BashQuest.