\chapter{Linux Command Problem Set} \ChapterLabel{LinuxProblemSet} \index{Linux!problem set} \section{Execution Guidance} The following answers are written for the test environment created in \ChapterRef{LinuxCommands}. Commands were suggested by AI for the team to run, then verified by the team before execution; the team made changes as needed, ran the commands, and gathered the outputs. All problem-set commands and recorded outputs were executed and collected by humans, not by AI. Commands were executed from \texttt{/root/lx-test}. \section{Problem Set} \subsection*{A) Navigation \& File Ops} \begin{enumerate}[label=\textbf{\arabic*.},leftmargin=*,start=1] \item \textbf{Show your present working directory path only.} \index{Linux command!pwd} \begin{lstlisting}[language=bash] pwd \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} /root/lx-test \end{lstlisting} The command reports the current working directory used for the problem set. \item \textbf{List all entries in the current directory, one per line, including dotfiles.} \index{Linux command!ls} \begin{lstlisting}[language=bash] ls -a1 \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} . .. archive blob.bin link-to-file1 old.txt people.csv src sys.log tmp words.txt \end{lstlisting} The `-a` option includes dotfiles and the `-1` option displays one entry per line. \item \textbf{Copy `src/file1.txt` to `tmp/` only if `tmp` exists; do it verbosely.} \index{Linux command!cp} \begin{lstlisting}[language=bash] [ -d tmp ] && cp -v src/file1.txt tmp/ \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} 'src/file1.txt' -> 'tmp/file1.txt' \end{lstlisting} The conditional copy runs only when `tmp` exists, and verbose mode reports the copy operation. \item \textbf{Move `old.txt` into `archive/` and keep its original timestamp.} \index{Linux command!mv} \begin{lstlisting}[language=bash] mv -v old.txt archive/ \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} renamed 'old.txt' -> 'archive/old.txt' \end{lstlisting} The move is performed as a rename within the same filesystem, so the file's modification timestamp is retained. \item \textbf{Create a new empty file `notes.md` only if it does not already exist.} \index{Linux command!touch} \begin{lstlisting}[language=bash] [ -e notes.md ] || touch notes.md \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} (no terminal output) \end{lstlisting} The existence test prevents `touch` from changing an existing file's timestamp. \item \textbf{Show human-readable disk usage for the `src` directory only.} \index{Linux command!du} \begin{lstlisting}[language=bash] du -sh src \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} 16K src \end{lstlisting} The command reports the allocated disk usage for `src` in human-readable form. \end{enumerate} \subsection*{B) Viewing \& Searching} \begin{enumerate}[label=\textbf{\arabic*.},leftmargin=*,start=7] \item \textbf{Print line numbers while displaying `sys.log`.} \index{Linux command!nl} \begin{lstlisting}[language=bash] nl -ba sys.log \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} 1 INFO boot ok 2 WARN disk low 3 ERROR fan fail 4 INFO shutdown \end{lstlisting} The `-b` option numbers all lines, including blank lines if present. \item \textbf{Show only lines in `sys.log` containing `ERROR`, case-sensitively.} \index{Linux command!grep} \begin{lstlisting}[language=bash] grep 'ERROR' sys.log \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} ERROR fan fail \end{lstlisting} The pattern is matched with case sensitivity, so only matching lines are returned. \item \textbf{Count the distinct words in `words.txt`, case-insensitively.} \index{Linux command!sort} \begin{lstlisting}[language=bash] sort -fu words.txt | wc -l \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} 3 \end{lstlisting} Sorting with `-f` and `-u` provides a case-insensitive unique count. \item \textbf{Show lines in `words.txt` that start with `g` or `G`.} \index{Linux command!grep} \begin{lstlisting}[language=bash] grep -i '^g' words.txt \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} Gamma gamma \end{lstlisting} The case-insensitive anchored pattern selects lines beginning with the letter `g`. \item \textbf{Display the first two lines of `people.csv` without an editor.} \index{Linux command!head} \begin{lstlisting}[language=bash] head -n 2 people.csv \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} id,name,dept 1,Ada,EE \end{lstlisting} The command returns the first two lines of the CSV. \item \textbf{Show the last three lines of `sys.log` and keep following if it grows.} \index{Linux command!tail} \begin{lstlisting}[language=bash] tail -n 3 -f sys.log \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} WARN disk low ERROR fan fail INFO shutdown ^C \end{lstlisting} The command displays the current final three lines and then waits for additional lines until interrupted. \end{enumerate} \subsection*{C) Text Processing} \begin{enumerate}[label=\textbf{\arabic*.},leftmargin=*,start=13] \item \textbf{Print only the second, name column from `people.csv`, excluding the header.} \index{Linux command!awk} \begin{lstlisting}[language=bash] awk -F, 'NR > 1 {print $2}' people.csv \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} Ada Linus Grace Dennis \end{lstlisting} The `awk` field separator is a comma, and records after the header print field 2. \item \textbf{Sort `words.txt` case-insensitively and remove duplicates.} \index{Linux command!tr} \begin{lstlisting}[language=bash] tr '[:upper:]' '[:lower:]' < words.txt | sort -u \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} alpha beta gamma \end{lstlisting} The pipeline lowercases the input before sorting uniquely. \item \textbf{Replace every `three` with `3` in all files under `src/` in place, creating `.bak` backups.} \index{Linux command!sed} \begin{lstlisting}[language=bash] find src -type f ! -name '*.bak' -exec sed -i.bak 's/three/3/g' {} + \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} (no terminal output) \end{lstlisting} The command edits matching files in place while excluding existing backup files and creating `.bak` copies. \textbf{Verification step:} \begin{lstlisting} $ cat src/file1.txt one two 3 four \end{lstlisting} The verification command is included as evidence of the resulting file state and is not part of the solution command. \item \textbf{Print the line, word, and byte counts for every `*.txt` file in `src/`.} \index{Linux command!wc} \begin{lstlisting}[language=bash] find src -type f -name '*.txt' -exec wc -l -w -c {} + \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} 1 4 15 src/file1.txt 1 4 16 src/file2.txt 2 8 31 total \end{lstlisting} The command reports line, word, and byte counts for the matching text files and a total. \end{enumerate} \subsection*{D) Permissions \& Ownership} \begin{enumerate}[label=\textbf{\arabic*.},leftmargin=*,start=17] \item \textbf{Make `tmp/` readable, writable, and searchable only by its owner.} \index{Linux command!chmod} \begin{lstlisting}[language=bash] chmod 700 tmp \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} (no terminal output) \end{lstlisting} Mode `700` grants the owner read, write, and execute \phantomsection\label{GlossaryRef:permissions}\gls{permissions} while granting none to group or others. \item \textbf{Give group execute permission to `src/lib` recursively without changing owner or other bits.} \index{Linux command!chmod} \begin{lstlisting}[language=bash] chmod -R g+x src/lib \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} (no terminal output) \end{lstlisting} The recursive `g+x` operation adds group execute permission without changing the other \gls{permissions}. \item \textbf{Show the numeric octal permissions of `src/file2.txt`.} \index{Linux command!stat} \begin{lstlisting}[language=bash] stat -c '%a' src/file2.txt \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} 644 \end{lstlisting} The command prints the file's numeric \gls{permissions} mode. \item \textbf{Make `notes.md` append-only via file attributes, if supported.} \index{Linux command!chattr} \begin{lstlisting}[language=bash] chattr +a notes.md && lsattr notes.md \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} -----a--------e------- notes.md \end{lstlisting} The command sets the append-only attribute and then displays the file attributes to verify it. \end{enumerate} \section*{E) Links and Find} \begin{enumerate}[label=\textbf{\arabic*.},leftmargin=*,start=21] \item \textbf{Verify that `link-to-file1` is a symbolic link and show its target.} \index{Linux command!symbolic links} \begin{lstlisting}[language=bash] [ -L link-to-file1 ] && readlink link-to-file1 \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} src/file1.txt \end{lstlisting} The test succeeds only for a \phantomsection\label{GlossaryRef:symboliclink}\gls{symboliclink}, after which `readlink` prints its target. \item \textbf{Find all regular files under the current tree larger than 40 KiB.} \index{Linux command!find} \begin{lstlisting}[language=bash] find . -type f -size +40k -print \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} ./blob.bin \end{lstlisting} The search restricts results to regular files whose size exceeds 40 KiB. \item \textbf{Find files modified in the last ten minutes under `tmp/` and print their sizes.} \index{Linux command!find} \begin{lstlisting}[language=bash] find tmp -type f -mmin -10 -printf '%s %p\n' \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} 19 tmp/file1.txt \end{lstlisting} The search filters by modification time and prints each matching size followed by its path. \end{enumerate} \section*{F) Processes \& Job Control} \begin{enumerate}[label=\textbf{\arabic*.},leftmargin=*,start=24] \item \textbf{Show your processes in a tree view.}\phantomsection\label{AcronymRef:PPID} \index{Linux command!processes} \begin{lstlisting}[language=bash] ps -u "$USER" --forest -o pid,ppid,stat,cmd \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} PID PPID STAT CMD 2 0 S [kthreadd] 3 2 S \_ [pool_workqueue_release] 4 2 I< \_ [kworker/R-rcu_g] 5 2 I< \_ [kworker/R-rcu_p] 6 2 I< \_ [kworker/R-slub_] 7 2 I< \_ [kworker/R-netns] 9 2 I< \_ [kworker/0:0H-kblockd] 12 2 I< \_ [kworker/R-mm_pe] 13 2 I \_ [rcu_tasks_kthread] 14 2 I \_ [rcu_tasks_rude_kthread] 15 2 I \_ [rcu_tasks_trace_kthread] 16 2 S \_ [ksoftirqd/0] 17 2 I \_ [rcu_preempt] 18 2 S \_ [migration/0] 19 2 S \_ [idle_inject/0] 20 2 S \_ [cpuhp/0] 21 2 S \_ [kdevtmpfs] 22 2 I< \_ [kworker/R-inet_] 23 2 S \_ [kauditd] 24 2 S \_ [khungtaskd] 26 2 S \_ [oom_reaper] 28 2 I< \_ [kworker/R-write] 29 2 S \_ [kcompactd0] 30 2 SN \_ [ksmd] 31 2 I< \_ [kworker/R-kinte] 32 2 I< \_ [kworker/R-kbloc] 33 2 I< \_ [kworker/R-blkcg] 34 2 S \_ [irq/9-acpi] 35 2 I< \_ [kworker/R-tpm_d] 36 2 I< \_ [kworker/R-ata_s] 37 2 I< \_ [kworker/R-md] 38 2 I< \_ [kworker/R-md_bi] 39 2 I< \_ [kworker/R-edac-] 40 2 I< \_ [kworker/R-devfr] 41 2 S \_ [watchdogd] 42 2 I< \_ [kworker/R-quota] 43 2 I< \_ [kworker/0:1H-kblockd] 44 2 S \_ [kswapd0] 45 2 S \_ [ecryptfs-kthread] 46 2 I< \_ [kworker/R-kthro] 47 2 I< \_ [kworker/R-acpi_] 48 2 S \_ [scsi_eh_0] 49 2 I< \_ [kworker/R-scsi_] 50 2 S \_ [scsi_eh_1] 51 2 I< \_ [kworker/R-scsi_] 52 2 S \_ [scsi_eh_2] 53 2 I< \_ [kworker/R-scsi_] 56 2 I< \_ [kworker/R-mld] 57 2 I< \_ [kworker/R-ipv6_] 64 2 I< \_ [kworker/R-kstrp] 66 2 I< \_ [kworker/u3:0] 71 2 I< \_ [kworker/R-crypt] 81 2 I< \_ [kworker/R-charg] 163 2 I< \_ [kworker/R-raid5] 200 2 S \_ [jbd2/vda1-8] 201 2 I< \_ [kworker/R-ext4-] 293 2 I< \_ [kworker/R-kmpat] 294 2 I< \_ [kworker/R-kmpat] 398 2 S \_ [jbd2/vda16-8] 399 2 I< \_ [kworker/R-ext4-] 1163 2 I< \_ [kworker/R-tls-s] 34191 2 S \_ [psimon] 52833 2 I \_ [kworker/u2:0-events_power_efficient] 53308 2 I \_ [kworker/u2:2-events_power_efficient] 53346 2 I \_ [kworker/u2:3-events_power_efficient] 53379 2 I \_ [kworker/0:1-cgroup_free] 53425 2 S \_ [psimon] 53433 2 S \_ [psimon] 53615 2 I \_ [kworker/0:2-events] 53633 2 I \_ [kworker/u2:4-events_unbound] 1 0 Ss /usr/lib/systemd/systemd --system --deserialize=75 837 1 Ss /usr/lib/systemd/systemd-logind 914 1 Ss+ /sbin/agetty -o -p -- \u --keep-baud 115200,57600,38400,9600 - vt220 920 1 Ssl /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal 960 1 Ss+ /sbin/agetty -o -p -- \u --noclear - linux 8058 1 Ssl /opt/digitalocean/bin/droplet-agent 20679 1 SLsl /sbin/multipathd -d -s 32446 1 Ss /usr/sbin/cron -f -P 34152 1 S 1 && $3 == "CS"' people.csv > cs.txt \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} (no terminal output) \end{lstlisting} The `awk` condition selects records after the header whose third field equals `CS`. \textbf{Verification step:} \begin{lstlisting} $ cat cs.txt 2,Linus,CS 4,Dennis,CS \end{lstlisting} The verification command is included as evidence of the resulting file state and is not part of the solution command. \item \textbf{Create an environment variable `X` with value `42`, print it, and remove it from the environment.} \index{Linux command!environment variables} \begin{lstlisting}[language=bash] export X=42; printf '%s\n' "$X"; unset X \end{lstlisting} \textbf{Observed output:} \begin{lstlisting} 42 \end{lstlisting} The \phantomsection\label{GlossaryRef:environmentvariable}\gls{environmentvariable} is exported, printed, and then unset in the current shell. \end{enumerate}