LICM study materials
LICM study resources

LICM: The Power of the Command Line

LICM · Work through your exam, one topic at a time.

All guide sections

The Power of the Command Line

Domain 3 - The Power of the Command Line

This domain tests your ability to combine small tools: pipes and redirection, text searching and processing, archiving, and writing a short shell script.

Core rules

  • > truncates and writes stdout to a file, >> appends, < reads stdin, 2> redirects stderr, 2>&1 merges stderr into stdout, and | connects stdout of one command to stdin of the next.
  • grep -i ignores case, -r recurses, -v inverts, -c counts matching lines, -n shows line numbers. Basic regex: ^ start of line, $ end of line, . any character, * zero or more of the previous, [] a character class.
  • cut -d: -f1 /etc/passwd takes field 1 with : as delimiter; sort -n sorts numerically, sort -r reverses; uniq -c counts adjacent duplicates, so sort first; wc -l counts lines; head -n 5, tail -n 5, and tail -f follows a growing log.
  • Archives: tar -czf out.tar.gz dir/ creates (c) a gzip (z) file (f); tar -xzf extracts; tar -tf lists. gzip compresses one file and replaces it; bzip2 and xz compress harder and slower.
  • A script starts with #!/bin/bash and needs execute permission (chmod +x). $? holds the last exit status, where 0 means success.

Worked example

Count unique login shells on a system: cut -d: -f7 /etc/passwd | sort | uniq -c | sort -nr. The cut pulls field 7, sort groups identical lines adjacently, uniq -c counts each run, and the final numeric reverse sort ranks them. Skipping the first sort would count only consecutive repeats.

Traps

  • > overwrites a file you meant to append to.
  • Piping does not capture stderr unless you add 2>&1 before the pipe.
  • uniq without a preceding sort gives wrong totals.
  • In tar -czf, the archive filename goes immediately after f.