Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

System utilities

Practical notes on common system utilities (file, text, archive, process and diagnostic tools), their definitions, important commands/syntax, worked command-line examples, exam-style hints and quick self-test questions.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

System utilities are the small programs an engineer uses every day to manage files, processes and diagnostics on a development or production machine. Knowing them saves time while debugging, packaging, profiling and preparing code for linking or deployment — all tasks that appear in system programming and in related topics such as Linkers, Loaders and Debuggers.

Key ideas

  • Redirection — changing where a process reads input from or writes output to. ">" redirects stdout to a file (truncate), ">>" appends, "2>" redirects stderr. Assumption: the shell performs redirection before executing the utility.

  • Pipe — an inter-process communication mechanism created by the shell with the pipe operator "|". It connects stdout of the left process to stdin of the right process as a FIFO buffer. For an exam diagram, draw two boxes labelled Process A and Process B, arrows from A:stdout to a box labelled pipe (FIFO) to B:stdin. Show file descriptors 1 and 0, and label the kernel buffer.

  • Filter — a utility that reads from stdin and writes a transformed result to stdout (examples: grep, sed, awk, sort). Filters are combinable with pipes.

  • File-finding and argument-building utilities — find (search filesystem by predicates such as -name, -type, -mtime) and xargs (build command lines from stdin; use -0 with find -print0 to handle spaces and newlines).

  • Text-processing utilities — grep (search lines matching a pattern), sed (stream editor for substitution and simple transforms), awk (field/record oriented processing). Regex dialects: grep defaults to POSIX BRE; -E uses POSIX ERE; -P uses PCRE (GNU grep extension). sed uses BRE by default (use -E or GNU -r for ERE). awk uses ERE by default.

  • File-archiving and compression — tar groups files into one archive; compression utilities gzip, bzip2, xz reduce size. Common combined usage: tar with -c (create), -x (extract), -t (list), and -f (file), and -z/-j/-J to compress with gzip/bzip2/xz respectively.

  • Process utilities — ps (snapshot of processes), top/htop (interactive view), kill (send signals), nice/renice (adjust scheduling priority). Define signal numbers (e.g., SIGTERM, SIGKILL) and their typical meanings: SIGTERM politely asks process to terminate; SIGKILL forces termination and cannot be caught. Signal numbers vary by architecture; prefer using signal names (e.g., kill -TERM PID) over raw numbers, except well-known -9/-15.

  • System inspection — strace (trace system calls of a process), ltrace (trace library calls), ldd (list dynamic dependencies), nm/objdump/readelf (inspect object files and symbol tables). These utilities interact directly with the topics Linkers, Loaders, Symbol tables and Debuggers.

  • Package managers and services — tools to install, remove, and manage packages (examples: apt, yum, dnf, pacman); service control utilities (systemctl, service) manage daemons. Assume distribution-specific behavior; avoid assuming identical options across all distributions.

  • Assumptions and environment: commands and options vary across UNIX flavors (GNU coreutils vs. BSD). In exams assume Linux/GNU unless the question states a different OS.

Formulas

For a system-utilities topic we remember command syntax (each line: command usage and what symbols mean):

ls -l [path] — list directory entries in long format; -l shows permissions, links, owner, group, size (bytes), modification time, name.

cat file — print file contents to stdout; use for small files only.

command > file — redirect stdout of command to file (truncate file).

command >> file — append stdout to file.

command 2> file — redirect stderr to file.

command1 | command2 — pipe stdout of command1 to stdin of command2.

grep -E 'pattern' file — search file for extended-RE pattern; -i ignore case, -v invert match, -n show line numbers.

find . -type f -name 'pattern' — traverse directory tree and match files by predicates (combine with -mtime, -size, -perm, -exec).

find . -print0 | xargs -0 cmd — safely build argv from NUL-separated input (handles spaces/newlines).

sed 's/old/new/g' file — replace all occurrences of old with new per line, print to stdout (use -i for in-place with care).

awk '{print $1, $3}' file — treat whitespace-separated fields; $1 is first field, $3 is third.

cut -d: -f1 file — split lines on : and print field 1 (use -f1,3 for multiple fields).

tar -cf archive.tar files... — create archive.tar from listed files.

tar -xf archive.tar — extract files from archive.tar into current directory.

tar -czf archive.tar.gz files... — create and gzip-compress archive.

gzip file — compress file to file.gz (original removed unless -k used).

chmod 755 file — set permissions using octal (u=rwx,g=rx,o=rx).

chmod u+rwx,g+rx,o-rwx file — set permissions symbolically.

chown user:group file — change owner and group (requires root to change owner).

ps aux — show all processes with user-oriented format (BSD style); columns include %CPU, %MEM, VSZ (virtual size), RSS (resident set size in KiB).

kill PID (default SIGTERM) and kill -9 PID (SIGKILL) — send signal to process id PID.

strace -o out.txt -f -e trace=file,network program args... — run program tracing system calls related to files and network, writing trace to out.txt.

ldd ./binary — list dynamic shared object dependencies for binary.

nm -C object.o — show symbol table from object file object.o; -C demangles C++ names.

Units to remember: file sizes are in bytes (B). Many tools display sizes in KiB (2^10) or kB (10^3) depending on option; use -h for human-readable where supported.

Worked examples

Example 1 — pipeline: count unique C source files referenced in a Makefile

Given: a file Makefile that contains lines with gcc -c src/foo.c -o obj/foo.o and other commands.

Goal: list unique .c filenames referenced and count them.

Steps:

  1. Extract filenames ending with .c: grep -oE '[[:alnum:]_]+\.c' Makefile (shows each match on a separate line).
  2. Sort and unique: grep -oE '\w+\.c' Makefile | sort | uniq.
  3. Count lines: ... | wc -l.

Full command: grep -oE '[[:alnum:]_]+\.c' Makefile | sort | uniq | wc -l

If output is 7, final answer: 7 source files.

Example 2 — archiving and restoring with tar and gzip (exam numerical-style description with units)

Given: directory project/ of total uncompressed size 24 576 000 bytes (≈ 24.576 MB). Create a gzip-compressed tar and later extract only src/ into /tmp.

Steps:

  1. Create archive: tar -czf project.tar.gz project/ (tar reads files and gzip compresses output). The resulting file size depends on compressibility; assume saved size is 9 830 400 bytes (9.83 MB) — store as result.
  2. To extract only src/ to /tmp: tar -xzf project.tar.gz -C /tmp project/src.

Check: after extraction, du -sb /tmp/project/src should show approximately original src size in bytes. Final answer (example numbers): project.tar.gz = 9 830 400 B; /tmp/project/src restored successfully.

(Notes for reviewer: the compressed size depends on content; the example assumes a plausible compression ratio ~40%.)

Example 3 — diagnosing a missing shared library using ldd and strace

Given: running ./prog fails with "error while loading shared libraries: libfoo.so.1: cannot open shared object file: No such file or directory".

Steps:

  1. Run ldd ./prog — shows which shared objects the binary expects and where they are found. Look for libfoo.so.1 => not found.
  2. If libfoo exists elsewhere, note its directory, e.g., /opt/lib, and either add to LD_LIBRARY_PATH (export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH) or add to /etc/ld.so.conf.d/ and run ldconfig (requires root).
  3. Verify by re-running ldd ./prog — now should show /opt/lib/libfoo.so.1 => /opt/lib/libfoo.so.1 (0x...) and running ./prog should succeed.

If binary starts but misbehaves, run strace -o trace.txt ./prog and inspect trace.txt for open() / openat() errors that show failed file opens and permissions.

Common mistakes

  • Using sed -i on important files without backup (sed -i is in-place and behaviour differs between GNU/BSD; in BSD you need -i '').
  • Expecting tar -xzf archive.tar.gz to always restore file ownerships; ownership can be preserved only when extracting as root (use --same-owner with GNU tar if needed). --owner/--group set the recorded owner/group at archive creation; applying them on extract requires root.
  • Running ldd on untrusted binaries — ldd may execute the program on some systems; safer to use readelf -d or objdump -p for scripts or untrusted inputs.
  • Ignoring that kill without signal uses SIGTERM; students send SIGKILL prematurely instead of allowing graceful cleanup.

For your exam

Question types you will see: definitions (define utility/pipe/filter/standard streams), short notes (compare grep/sed/awk; describe tar options), diagram (draw pipe and file descriptors), derivations/steps (explain how to construct a pipeline to achieve a specified text-processing goal), commands (write correct command-line to archive/extract, search and replace, change permissions), and debugging scenarios (how to find missing shared libs or trace system calls).

What a full-marks answer includes:

  • Clear definitions with bolded terms (e.g., pipe, filter).
  • Exact command syntax with options and explanation of each option used.
  • Diagrams labelled: processes, file descriptors 0/1/2, kernel pipe buffer, arrows for data flow.
  • For step problems, show intermediate commands and expected intermediate outputs; explain assumptions (GNU vs BSD) if relevant.

Quick check

  1. What file descriptor numbers correspond to stdin, stdout and stderr?
  2. Which command shows dynamic shared libraries required by a binary?
  3. Give a one-line pipeline that finds lines containing "TODO" (case-insensitive) in all .c files and outputs unique file names.

Answers: 1) stdin=0, stdout=1, stderr=2. 2) ldd (or readelf -d as a safer alternative). 3) grep -iR --include='*.c' -n 'TODO' . | cut -d: -f1 | sort -u.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?