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>&1merges stderr into stdout, and|connects stdout of one command to stdin of the next.grep -iignores case,-rrecurses,-vinverts,-ccounts matching lines,-nshows 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/passwdtakes field 1 with:as delimiter;sort -nsorts numerically,sort -rreverses;uniq -ccounts adjacent duplicates, so sort first;wc -lcounts lines;head -n 5,tail -n 5, andtail -ffollows a growing log.- Archives:
tar -czf out.tar.gz dir/creates (c) a gzip (z) file (f);tar -xzfextracts;tar -tflists.gzipcompresses one file and replaces it;bzip2andxzcompress harder and slower. - A script starts with
#!/bin/bashand 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>&1before the pipe. uniqwithout a precedingsortgives wrong totals.- In
tar -czf, the archive filename goes immediately afterf.