Based on the Linux Master 1 exam syllabusReport an error
find → grep → awk → tar → vi mesh together in one flow.cp -r (required for copying directories), rmdir only for empty directories, and {} and \; in find -exec.grep (line filter) · sed s///g (substitute) · awk $1 (field) · sort|uniq -c (frequency).i/a/o, dd·yy·p, :%s/old/new/g, the difference between :wq and :x.tar czvf (create) · xzvf (extract) · tzvf (view). For help: man · info · whatis · apropos.The command-line mindset. The essence of the command line is "connect small tools with pipes." Instead of one giant program handling everything, you assemble complex jobs by chaining simple tools like find, grep, sort, and awk with |. The commands you learned this week are all parts for this assembly.
/) and a relative path based on your current location.. (current), .. (parent), and ~ (home).| Command | Role | Commonly used options |
|---|---|---|
pwd | Print current location | -P (physical path) |
cd | Change directory | cd - (previous location) |
ls | List contents | -l, -a, -h, -ltr |
mkdir | Create a directory | -p (create intermediate paths) |
rmdir | Delete an empty directory | -p |
touch | Create an empty file / update time | -t (specify time) |
cp | Copy | -r, -p, -i |
mv | Move/rename | -i, -f |
rm | Delete | -r, -f |
find | Real-time file search | -name, -type, -exec |
locate | DB-based search | -i, updatedb |
which | Find a command's path | -a |
⚠️ Pitfall roundup: Day 1 Copying a directory requires
cp -r(error without it),rmdirdeletes only empty directories, notgzipbutrmhas no recycle bin, andlocateis DB-based so it may not find the newest files. Infind's-exec,{}is the file-name substitution and\;is the end of the command.
The starting point of text processing is the standard streams and the pipe (|). It connects the output of one command to the input of the next.
| Command | Role | Key usage |
|---|---|---|
cat | Print all / concatenate | -n (line numbers) |
head/tail | First/last part | -n, tail -f |
grep | Pattern search | -i, -v, -r, -n, -c |
sed | Stream editing | s/old/new/g, -i |
awk | Field processing | {print $1}, -F, NR, NF |
sort | Sort | -n, -r, -k, -u |
uniq | Remove duplicates | -c, -d (paired with sort) |
cut | Extract columns | -d, -f, -c |
tr | Convert characters | -d, -s |
wc | Count | -l, -w, -c |
. (any single character), * (zero or more of the preceding character), ^ (start of line), $ (end of line), [] (character set).grep -E), +, ?, |, and () work without a backslash.Commonly used pipeline patterns. "Count errors" is grep error log | wc -l, "unique IP frequency" is awk '{print $1}' log | sort | uniq -c | sort -nr, and "info for a specific user" is grep user /etc/passwd | cut -d: -f1,6. Once these combinations are in your fingers, log analysis becomes fun.
The three modes of vi are as follows. Command mode (default at startup, movement/editing), insert mode (entered with i/a/o, text input), and last-line mode (entered with :, save/quit/substitute).
| Task | Command |
|---|---|
| Start typing | i (before), a (after), o (line below), O (line above) |
| Move | h j k l, w/b, 0/$, gg/G |
| Delete | x (character), dd (line), dw (word) |
| Copy/paste | yy (copy), p/P (paste) |
| Undo/repeat | u (undo), Ctrl+r (redo), . (repeat) |
| Search | /pattern, ?pattern, n/N |
| Save/quit | :w, :q, :q!, :wq, :x, ZZ |
| Substitute | :%s/old/new/g |
⚠️ Pitfall roundup: Day 3
atypes after the cursor (ibefore it),ois the line below/Othe line above,:wqalways saves/:xandZZsave only on change, and the%in:%sis the whole file. Innano, save isCtrl+Oand exit isCtrl+X.
Archiving (bundling) and compression (shrinking size) are different jobs. tar bundles, and gzip/bzip2/xz compress.
| Task | Command |
|---|---|
| Bundle and gzip-compress | tar czvf x.tar.gz dir/ |
| Extract a gzip archive | tar xzvf x.tar.gz |
| View the content listing | tar tzvf x.tar.gz |
| Extract to a specific location | tar xvf x.tar -C /tmp |
| Compress a single file | gzip file (original deleted), preserve with -k |
| zip compression | zip -r x.zip dir/ |
tar options: c (create), x (extract), t (list), v (verbose), f (file name), z (gzip), j (bzip2), J (xz).man (section-based manuals), info (detailed documents), whatis (one-line summary, man -f), and apropos (keyword search, man -k).The Unix philosophy running through the week. This week's tools all follow the Unix philosophy set down by Ken Thompson and Dennis Ritchie in the 1970s. "Do one thing well," "make programs work together," "handle text streams." grep, sort, and tar each do just one thing well and cooperate through pipes. This is why the design is still valid fifty years later.
This is a comprehensive exercise that weaves everything you learned into one flow.
# 1. Create a workspace and log data
mkdir -p ~/review/logs
cd ~/review
cat > logs/access.log << 'EOF'
192.168.0.1 GET /index 200
192.168.0.2 POST /login 401
192.168.0.1 GET /about 200
192.168.0.3 GET /index 500
192.168.0.2 GET /index 200
EOF
# 2. Find the log with find
find . -name "*.log" -type f
# 3. Count errors (401, 500) with grep + wc
grep -E "401|500" logs/access.log | wc -l
# 4. Rank IP access frequency with awk + sort + uniq
awk '{print $1}' logs/access.log | sort | uniq -c | sort -nr
# 5. Mark status code 200 as OK with sed
sed 's/ 200$/ OK/' logs/access.log
# 6. Back up with tar and verify
tar czvf backup.tar.gz logs/
tar tzvf backup.tar.gz
# 7. Check with help
whatis awk
# 8. Clean up
cd ~
rm -rf ~/review/, and an address based on your current location.cp -r : The recursive option that is essential when copying a whole directory.find -exec : An option that runs a command for each found file. {} is replaced with the file name and it is terminated with \;.|) : A connector that links the output of the preceding command to the input of the following command. The core of the command-line mindset.sort | uniq -c : A combination that sorts and then counts duplicates. The fundamental of log frequency analysis.grep -E. You can use + ? | ( ) without a backslash.:%s/old/new/g : The vi command that substitutes across the whole file. % means "all lines."man -k.Click a choice to reveal the answer and explanation.
Question 1
Which command finds only regular files ending in .conf under the current directory?
Question 2
Which is correct about the behavior of sed -i 's/dev/prod/g' config.txt?
Question 3
What is the result of awk -F: '{print $1}' /etc/passwd?
Question 4
In vi, which key creates a new line below the line where the cursor currently is and enters insert mode?
Question 5
In vi, which command saves and quits only if changed, preserving the timestamp if there are no changes?
Question 6
Which command bundles the directory data into an archive in bzip2 compression format?
Question 7
Which is correct about the result of running gzip -k report.txt?
Question 8
Which pair of commands is equivalent to searching the manual descriptions for the keyword "network" to find related commands?
Question 8
You want to sort the number of connections per IP from a log file in descending order. What is the most suitable pipeline?
Question 9
Which of the following is the most suitable option combination to copy an entire directory with the cp command while also preserving permissions and timestamps?
Question 10
Which lines does grep -E "^[0-9]+$" data.txt find?
Question 11
Which command checks only the listing of backup.tar.gz, made with tar, without extracting it?