Add automated LaTeX manual build (refs #12)
Automated Manual Build / manual (push) Successful in 1m27s
Automated Manual Build / manual (push) Successful in 1m27s
This commit is contained in:
@@ -0,0 +1,819 @@
|
||||
% REQUIRES IN THE PREAMBLE (main.tex, before \begin{document}):
|
||||
% \usepackage{upquote} % straight quotes inside verbatim
|
||||
% \usepackage{float} % enables [H] figure placement
|
||||
% \usepackage{fancyvrb} % colored, framed Verbatim
|
||||
% \usepackage{xcolor} % (already loaded by most templates)
|
||||
%
|
||||
% \definecolor{termrule}{RGB}{190,190,205}
|
||||
% \definecolor{termtext}{RGB}{20,55,95}
|
||||
% \fvset{frame=single, rulecolor=\color{termrule}, framesep=5pt,
|
||||
% formatcom=\color{termtext}, fontsize=\small, samepage=true}
|
||||
|
||||
\chapter{Linux Commands}
|
||||
\ChapterLabel{LinuxCommands}
|
||||
\index{linux commands}
|
||||
\section{Environment Setup}
|
||||
|
||||
The following commands were executed in a Bash shell in a Linux environment to build the
|
||||
working tree \verb|~/lx-test| used throughout this chapter.
|
||||
|
||||
\begin{figure}[H]
|
||||
\begin{minipage}{\textwidth}
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ mkdir -p ~/lx-test && cd ~/lx-test
|
||||
$ printf "alpha\nbeta\nGamma\ngamma\nbeta\n" > words.txt
|
||||
$ printf "id,name,dept\n1,Ada,EE\n2,Linus,CS\n3,Grace,EE\n4,Dennis,CS\n" > people.csv
|
||||
$ printf "INFO boot ok\nWARN disk low\nERROR fan fail\nINFO shutdown\n" > sys.log
|
||||
$ dd if=/dev/zero of=blob.bin bs=1K count=48 status=none
|
||||
$ mkdir -p src/lib tmp archive
|
||||
$ printf "one two three four\n" > src/file1.txt
|
||||
$ printf "two three four five\n" > src/file2.txt
|
||||
$ ln -s src/file1.txt link-to-file1
|
||||
$ touch -t 202401020304 old.txt
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
\caption{Terminal input for the environment setup.}
|
||||
\label{fig:setup-input}
|
||||
\end{figure}
|
||||
|
||||
None of the setup commands emit output on success, so the resulting directory
|
||||
tree was verified with \verb|ls -laR|.
|
||||
|
||||
\begin{figure}[H]
|
||||
\begin{minipage}{\textwidth}
|
||||
{\scriptsize
|
||||
\begin{Verbatim}[fontsize=\scriptsize]
|
||||
billy@DESKTOP-04AFIRH:~/lx-test$ ls -laR ~/lx-test
|
||||
/home/billy/lx-test:
|
||||
total 80
|
||||
drwxr-xr-x 5 billy billy 4096 Sep 13 11:24 .
|
||||
drwxr-x--- 25 billy billy 4096 Sep 13 11:24 ..
|
||||
drwxr-xr-x 2 billy billy 4096 Sep 13 11:24 archive
|
||||
-rw-r--r-- 1 billy billy 49152 Sep 13 11:24 blob.bin
|
||||
lrwxrwxrwx 1 billy billy 13 Sep 13 11:24 link-to-file1 -> src/file1.txt
|
||||
-rw-r--r-- 1 billy billy 0 Jan 2 2024 old.txt
|
||||
-rw-r--r-- 1 billy billy 56 Sep 13 11:24 people.csv
|
||||
drwxr-xr-x 3 billy billy 4096 Sep 13 11:24 src
|
||||
-rw-r--r-- 1 billy billy 56 Sep 13 11:24 sys.log
|
||||
drwxr-xr-x 2 billy billy 4096 Sep 13 11:24 tmp
|
||||
-rw-r--r-- 1 billy billy 28 Sep 13 11:24 words.txt
|
||||
|
||||
/home/billy/lx-test/archive:
|
||||
total 8
|
||||
drwxr-xr-x 2 billy billy 4096 Sep 13 11:24 .
|
||||
drwxr-xr-x 5 billy billy 4096 Sep 13 11:24 ..
|
||||
|
||||
/home/billy/lx-test/src:
|
||||
total 20
|
||||
drwxr-xr-x 3 billy billy 4096 Sep 13 11:24 .
|
||||
drwxr-xr-x 5 billy billy 4096 Sep 13 11:24 ..
|
||||
-rw-r--r-- 1 billy billy 19 Sep 13 11:24 file1.txt
|
||||
-rw-r--r-- 1 billy billy 20 Sep 13 11:24 file2.txt
|
||||
drwxr-xr-x 2 billy billy 4096 Sep 13 11:24 lib
|
||||
|
||||
/home/billy/lx-test/src/lib:
|
||||
total 8
|
||||
drwxr-xr-x 2 billy billy 4096 Sep 13 11:24 .
|
||||
drwxr-xr-x 3 billy billy 4096 Sep 13 11:24 ..
|
||||
|
||||
/home/billy/lx-test/tmp:
|
||||
total 8
|
||||
drwxr-xr-x 2 billy billy 4096 Sep 13 11:24 .
|
||||
drwxr-xr-x 5 billy billy 4096 Sep 13 11:24 ..
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
\caption{Terminal output confirming the created file tree.}
|
||||
\label{fig:setup-output}
|
||||
\end{figure}
|
||||
|
||||
% ============================================================
|
||||
\section{(A) Navigation and File Operations}
|
||||
% ============================================================
|
||||
|
||||
\subsection*{Q1. Present working directory}
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ pwd
|
||||
/home/billy/lx-test
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
\subsection*{Q2. All entries, one per line, including dotfiles}
|
||||
The \texttt{-1} flag forces one entry per line and \texttt{-a} includes dotfiles.
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ ls -1a
|
||||
.
|
||||
..
|
||||
archive
|
||||
blob.bin
|
||||
link-to-file1
|
||||
old.txt
|
||||
people.csv
|
||||
src
|
||||
sys.log
|
||||
tmp
|
||||
words.txt
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
\subsection*{Q3. Copy \texttt{src/file1.txt} into \texttt{tmp/} verbosely}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The trailing slash forces \texttt{cp} to require an existing directory; if
|
||||
\texttt{tmp} were absent the command would fail rather than create a regular
|
||||
file named \texttt{tmp}.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ cp -v src/file1.txt tmp/
|
||||
'src/file1.txt' -> 'tmp/file1.txt'
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q4. Move \texttt{old.txt} into \texttt{archive/} preserving its timestamp}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
Within a single filesystem \texttt{mv} is a rename, so the inode and its
|
||||
modification time are unchanged and no additional flag is required.
|
||||
\texttt{mv} is silent on success, so the result is verified with \texttt{ls}.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ mv old.txt archive/
|
||||
$ ls -l archive/old.txt
|
||||
-rw-r--r-- 1 billy billy 0 Jan 2 2024 archive/old.txt
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q5. Create \texttt{notes.md} only if absent}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
A bare \texttt{touch} would update the modification time of an existing file,
|
||||
which would violate the ``only if it doesn't already exist'' requirement.
|
||||
Testing for existence first makes the command idempotent: the modification
|
||||
time is unchanged across two runs two seconds apart.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ ls -l --time-style=full-iso notes.md
|
||||
-rw-r--r-- 1 billy billy 0 2026-09-13 12:25:17.255475178 -0400 notes.md
|
||||
$ sleep 2
|
||||
$ [ -e notes.md ] || touch notes.md
|
||||
$ ls -l --time-style=full-iso notes.md
|
||||
-rw-r--r-- 1 billy billy 0 2026-09-13 12:25:17.255475178 -0400 notes.md
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q6. Human-readable disk usage of \texttt{src} only}
|
||||
\texttt{-s} summarizes rather than listing \texttt{src/lib} separately, and
|
||||
\texttt{-h} selects human-readable units. \texttt{du} reports directory usage,
|
||||
unlike \texttt{df}, which reports filesystem totals.
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ du -sh src
|
||||
16K src
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
% ============================================================
|
||||
\section{(B) Viewing and Searching}
|
||||
% ============================================================
|
||||
|
||||
\subsection*{Q7. Display \texttt{sys.log} with line numbers}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ cat -n sys.log
|
||||
1 INFO boot ok
|
||||
2 WARN disk low
|
||||
3 ERROR fan fail
|
||||
4 INFO shutdown
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q8. Lines containing \texttt{ERROR}, case-sensitive}
|
||||
\texttt{grep} matches case-sensitively by default, so no flag is required;
|
||||
adding \texttt{-i} would violate the requirement.
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ grep ERROR sys.log
|
||||
ERROR fan fail
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
\subsection*{Q9. Count distinct words in \texttt{words.txt}, case-insensitive}
|
||||
Folding to lowercase before sorting collapses \texttt{Gamma} and \texttt{gamma}
|
||||
into one entry and removes the duplicate \texttt{beta}, leaving \texttt{alpha},
|
||||
\texttt{beta}, and \texttt{gamma}.
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ tr '[:upper:]' '[:lower:]' < words.txt | sort -u | wc -l
|
||||
3
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
\subsection*{Q10. Lines beginning with \texttt{g} or \texttt{G}}
|
||||
The caret anchors the match to the start of the line; the bracket expression
|
||||
accepts either case.
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ grep -E '^[gG]' words.txt
|
||||
Gamma
|
||||
gamma
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
\subsection*{Q11. First two lines of \texttt{people.csv}}
|
||||
The header row is included, as the question asks for the first two lines of the
|
||||
file rather than the first two records.
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ head -n 2 people.csv
|
||||
id,name,dept
|
||||
1,Ada,EE
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
\subsection*{Q12. Last three lines of \texttt{sys.log}, following growth}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
\texttt{tail -f} does not exit after printing. It emits the final three lines
|
||||
and then blocks, writing each new line as the file grows. The fourth line below
|
||||
was appended from a second terminal while the command was running, and appeared
|
||||
immediately. The session is ended with \texttt{Ctrl-C}.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ tail -n 3 -f sys.log
|
||||
WARN disk low
|
||||
ERROR fan fail
|
||||
INFO shutdown
|
||||
WARN temp high
|
||||
^C
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
% ============================================================
|
||||
\section{(C) Text Processing}
|
||||
% ============================================================
|
||||
|
||||
\subsection*{Q13. Print the second column of \texttt{people.csv}, excluding the header}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
\texttt{tail} skips the header row, and \texttt{cut} uses a comma delimiter to
|
||||
select only the second field from each remaining record.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ tail -n +2 people.csv | cut -d, -f2
|
||||
Ada
|
||||
Linus
|
||||
Grace
|
||||
Dennis
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q14. Sort \texttt{words.txt} case-insensitively and remove duplicates}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The \texttt{-f} option folds letter case for comparison, while \texttt{-u}
|
||||
emits one representative of each case-insensitive equivalence class. Thus
|
||||
\texttt{Gamma} and \texttt{gamma} are treated as duplicates.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ sort -fu words.txt
|
||||
alpha
|
||||
beta
|
||||
Gamma
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q15. Replace \texttt{three} with \texttt{3} under \texttt{src/}}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
\texttt{find} recursively selects regular files, excluding any pre-existing
|
||||
backup files. GNU \texttt{sed -i.bak} edits each selected file in place and
|
||||
retains its original contents in a sibling file with the \texttt{.bak} suffix.
|
||||
The file listing and contents below verify both effects.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ find src -type f ! -name '*.bak' -exec sed -i.bak 's/three/3/g' {} +
|
||||
$ find src -type f -print | sort
|
||||
src/file1.txt
|
||||
src/file1.txt.bak
|
||||
src/file2.txt
|
||||
src/file2.txt.bak
|
||||
$ cat src/file1.txt src/file2.txt
|
||||
one two 3 four
|
||||
two 3 four five
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q16. Count lines, words, and bytes for each \texttt{*.txt} file in \texttt{src/}}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
With no count-selection flags, \texttt{wc} reports lines, words, and bytes in
|
||||
that order. Because Q15 shortened each file by replacing five-byte
|
||||
\texttt{three} with one-byte \texttt{3}, the observed byte counts are 15 and
|
||||
16; \texttt{wc} also prints their aggregate total.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ wc src/*.txt
|
||||
1 4 15 src/file1.txt
|
||||
1 4 16 src/file2.txt
|
||||
2 8 31 total
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
% ============================================================
|
||||
\section{(D) Permissions and Ownership}
|
||||
% ============================================================
|
||||
|
||||
\subsection*{Q17. Restrict \texttt{tmp/} to the owner}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
Read, write, and execute for the owner is $4+2+1=7$; group and other receive
|
||||
no bits. On a directory the execute bit is the search bit, which permits
|
||||
traversal into the directory. Figure~\ref{fig:setup-output} records the
|
||||
baseline mode as \texttt{drwxr-xr-x}. The \texttt{-d} flag makes \texttt{ls}
|
||||
report the directory itself rather than its contents.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ chmod 700 tmp
|
||||
$ ls -ld tmp
|
||||
drwx------ 2 billy billy 4096 Sep 13 12:23 tmp
|
||||
$ stat -c '%a %n' tmp
|
||||
700 tmp
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q18. Add group execute to \texttt{src/lib} recursively}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
Symbolic mode with \texttt{+} adds a single bit and leaves the remaining
|
||||
owner and other bits untouched; an octal argument such as \texttt{750} would
|
||||
instead set all nine bits at once. The mode is unchanged because
|
||||
Figure~\ref{fig:setup-output} shows \texttt{src/lib} already carried group
|
||||
execute at \texttt{drwxr-xr-x}, so the command is idempotent. Note that
|
||||
\texttt{-R g+x} would also set the execute bit on regular files;
|
||||
\texttt{-R g+X} restricts the change to directories and is generally
|
||||
preferable, though the two are equivalent here because \texttt{src/lib} is
|
||||
empty.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ chmod -R g+x src/lib
|
||||
$ stat -c '%a %A %n' src/lib
|
||||
755 drwxr-xr-x src/lib
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q19. Octal permissions of \texttt{src/file2.txt}}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The \texttt{-c} option takes a format string: \texttt{\%a} is the octal mode,
|
||||
\texttt{\%A} the symbolic mode, and \texttt{\%n} the file name. Both forms are
|
||||
shown to confirm they describe the same permission set.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ stat -c '%a %A %n' src/file2.txt
|
||||
644 -rw-r--r-- src/file2.txt
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q20. Make \texttt{notes.md} append-only}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
File attributes are an ext-filesystem mechanism distinct from permission bits.
|
||||
The append-only attribute is stored in the inode and overrides the permission
|
||||
bits, so setting it requires the \texttt{CAP\_LINUX\_IMMUTABLE} capability and
|
||||
therefore \texttt{sudo}. The \texttt{a} in the \texttt{lsattr} field indicates
|
||||
the attribute is set.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ sudo chattr +a notes.md
|
||||
$ lsattr notes.md
|
||||
-----a--------e------- notes.md
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The attribute is verified by attempting both kinds of write. A truncating
|
||||
redirect is refused while an appending redirect succeeds, which is precisely
|
||||
the intended semantics.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ echo "overwrite attempt" > notes.md
|
||||
bash: notes.md: Operation not permitted
|
||||
$ echo "appended line" >> notes.md
|
||||
$ cat notes.md
|
||||
appended line
|
||||
appended line
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
Two lines appear because an earlier appending redirect had already succeeded
|
||||
without visible output; only the refused truncating redirect produced a
|
||||
message. The attribute is then removed so the file remains writable for the
|
||||
remainder of the exercise.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ sudo chattr -a notes.md
|
||||
$ lsattr notes.md
|
||||
--------------e------- notes.md
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
% ============================================================
|
||||
\section{(E) Links and Find}
|
||||
% ============================================================
|
||||
|
||||
\subsection*{Q21. Verify \texttt{link-to-file1} is a symlink and show its target}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
A symbolic link is a file whose contents are a path string; the leading
|
||||
\texttt{l} in the mode field identifies the file type, and the size of 13 bytes
|
||||
is exactly the length of the stored path \texttt{src/file1.txt}. The permission
|
||||
bits of a symlink are always \texttt{rwxrwxrwx} and are not consulted; access is
|
||||
governed by the permissions of the target. The \texttt{-L} test operator
|
||||
confirms the file type and \texttt{readlink} prints the stored target.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ ls -l link-to-file1
|
||||
lrwxrwxrwx 1 billy billy 13 Sep 13 11:24 link-to-file1 -> src/file1.txt
|
||||
$ [ -L link-to-file1 ] && echo "is a symlink"
|
||||
is a symlink
|
||||
$ readlink link-to-file1
|
||||
src/file1.txt
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The stored target is a relative path, which is resolved against the directory
|
||||
containing the link rather than the current working directory; moving the link
|
||||
elsewhere would therefore break it. \texttt{readlink -f} resolves the link and
|
||||
reports the absolute path of the target. \texttt{stat} reports both the file
|
||||
type and the target in a single command.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ readlink -f link-to-file1
|
||||
/home/billy/lx-test/src/file1.txt
|
||||
$ stat -c '%F %N' link-to-file1
|
||||
symbolic link 'link-to-file1' -> 'src/file1.txt'
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q22. Regular files under the current tree larger than 40 KiB}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
\texttt{-type f} restricts the match to regular files, excluding directories and
|
||||
the symbolic link. The \texttt{k} suffix denotes 1024-byte units, matching the
|
||||
KiB required by the question. Note that \texttt{find} rounds each file size up
|
||||
to a whole unit before comparing, so \texttt{-size +40k} excludes a file of
|
||||
exactly 40960 bytes and effectively means 41 KiB or larger; \texttt{-size
|
||||
+40960c} would compare exact byte counts instead. Either form selects
|
||||
\texttt{blob.bin} at 49152 bytes.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ find . -type f -size +40k
|
||||
./blob.bin
|
||||
$ find . -type f -size +40k -printf '%s %p\n'
|
||||
49152 ./blob.bin
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q23. Files modified in the last 10 minutes under \texttt{tmp/}}
|
||||
In \texttt{-mmin} the sign is a comparison operator rather than a negation:
|
||||
\texttt{-10} selects files modified fewer than 10 minutes ago, while
|
||||
\texttt{+10} would select those older than 10 minutes. The \texttt{-printf}
|
||||
format codes \texttt{\%s} and \texttt{\%p} print the size in bytes and the path.
|
||||
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The first invocation returns no output, which is the correct result:
|
||||
\texttt{tmp/file1.txt} was created earlier in Q3 and its modification time falls
|
||||
outside the ten-minute window. Updating the timestamp with \texttt{touch} brings
|
||||
the file into range and the same command then matches it, confirming that the
|
||||
time filter is active rather than the expression being malformed.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ find tmp -type f -mmin -10 -printf '%s %p\n'
|
||||
$ touch tmp/file1.txt
|
||||
$ find tmp -type f -mmin -10 -printf '%s %p\n'
|
||||
19 tmp/file1.txt
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
% ============================================================
|
||||
\section{(F) Processes and Job Control}
|
||||
% ============================================================
|
||||
|
||||
\subsection*{Q24. Show the current user's processes in a tree view}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The \texttt{-u} selector limits the listing to the current user. The selected
|
||||
columns show process and parent IDs, state, and command, while
|
||||
\texttt{--forest} draws parent--child relationships. Process IDs and the exact
|
||||
process set are execution-specific snapshots.
|
||||
\par\medskip
|
||||
{\scriptsize
|
||||
\begin{Verbatim}[fontsize=\scriptsize]
|
||||
$ ps -u "$USER" -o pid,ppid,stat,cmd --forest
|
||||
PID PPID STAT CMD
|
||||
587 549 S sshd-session: vsun@notty
|
||||
588 587 Ss \_ bash -c tr -d '\r' | bash
|
||||
590 588 S \_ bash
|
||||
593 590 R \_ ps -u vsun -o pid,ppid,stat,cmd --forest
|
||||
407 337 Ss+ -bash
|
||||
336 335 Ss+ -bash
|
||||
379 1 Ss /usr/lib/systemd/systemd --user
|
||||
381 379 S \_ (sd-pam)
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q25. Start \texttt{sleep 120} in the background and show its PID}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The ampersand starts a background job. Bash places the PID of the most recent
|
||||
background process in \texttt{\$!}; \texttt{ps} verifies the PID and command.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ sleep 120 &
|
||||
$ echo $!
|
||||
596
|
||||
$ ps -p 596 -o pid=,stat=,cmd=
|
||||
596 S sleep 120
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q26. Send TERM to all owned \texttt{sleep} processes}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
\texttt{pkill} selects the exact process name and current owner. Explicitly
|
||||
naming TERM documents the graceful signal and avoids the prohibited
|
||||
\texttt{kill -9}; the follow-up query confirms that no matches remain.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ pkill -TERM -u "$USER" -x sleep
|
||||
$ pgrep -a -u "$USER" -x sleep || echo "No sleep processes remain"
|
||||
No sleep processes remain
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q27. Top five processes by memory usage}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
\texttt{ps} produces a one-shot snapshot. Descending \texttt{\%mem} sorting
|
||||
places the largest consumers first; six lines retain the header and five
|
||||
process rows. These observed values change with system workload.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ ps -eo pid,user,%mem,rss,comm --sort=-%mem | head -n 6
|
||||
PID USER %MEM RSS COMMAND
|
||||
296 root 0.2 32652 unattended-upgr
|
||||
170 root 0.2 29760 networkd-dispat
|
||||
48 root 0.1 16496 systemd-journal
|
||||
182 root 0.1 15648 wsl-pro-service
|
||||
1 root 0.1 15536 systemd
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
% ============================================================
|
||||
\section{(G) Archiving and Compression}
|
||||
% ============================================================
|
||||
|
||||
\subsection*{Q28. Create \texttt{src.tgz} from \texttt{src/}}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The flags create an archive (\texttt{-c}), apply gzip compression
|
||||
(\texttt{-z}), and name the output (\texttt{-f}). Starting above
|
||||
\texttt{src/} records relative member names rather than absolute paths.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ tar -czf src.tgz src/
|
||||
$ ls -lh src.tgz
|
||||
-rw-rw-r-- 1 vsun vsun 254 Sep 13 17:05 src.tgz
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q29. List \texttt{src.tgz} without extracting it}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The \texttt{-t} operation lists members; because no extraction operation is
|
||||
present, it does not write archive contents into the working tree.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ tar -tzf src.tgz
|
||||
src/
|
||||
src/file2.txt
|
||||
src/lib/
|
||||
src/file1.txt
|
||||
src/file2.txt.bak
|
||||
src/file1.txt.bak
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q30. Extract only \texttt{file2.txt} into \texttt{tmp/}}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The archive-member argument selects only \texttt{src/file2.txt}.
|
||||
\texttt{-C tmp} chooses the destination, and
|
||||
\texttt{--strip-components=1} removes the leading \texttt{src/}, producing
|
||||
\texttt{tmp/file2.txt}. The final commands verify the selected extraction.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ tar -xzf src.tgz -C tmp --strip-components=1 src/file2.txt
|
||||
$ find tmp -maxdepth 1 -type f -printf "%f\n"
|
||||
file2.txt
|
||||
$ cat tmp/file2.txt
|
||||
two 3 four five
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
% ============================================================
|
||||
\section{(H) Networking and System Information}
|
||||
% ============================================================
|
||||
|
||||
\subsection*{Q31. Listening TCP sockets with associated PIDs}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
In \texttt{ss -ltnp}, \texttt{-l} selects listening sockets, \texttt{-t}
|
||||
selects TCP, \texttt{-n} prevents name resolution, and \texttt{-p} requests
|
||||
process information. The command requires no privilege escalation. In this
|
||||
unprivileged run, the kernel listed every listening TCP socket but withheld
|
||||
process details for sockets owned by other users, including root-owned DNS and
|
||||
SSH services; blank \texttt{Process} fields accurately reflect that access
|
||||
boundary.
|
||||
\par\medskip
|
||||
{\scriptsize
|
||||
\begin{Verbatim}[fontsize=\scriptsize]
|
||||
$ ss -ltnp
|
||||
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
|
||||
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
|
||||
LISTEN 0 4096 127.0.0.54:53 0.0.0.0:*
|
||||
LISTEN 0 1000 10.255.255.254:53 0.0.0.0:*
|
||||
LISTEN 0 4096 0.0.0.0:22 0.0.0.0:*
|
||||
LISTEN 0 4096 [::]:22 [::]:*
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\clearpage
|
||||
\subsection*{Q32. Print the default route concisely}
|
||||
\texttt{ip route show default} filters the routing table to the default route,
|
||||
showing the gateway and interface without unrelated routes.
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ ip route show default
|
||||
default via 192.168.80.1 dev eth0 proto kernel
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
\subsection*{Q33. Kernel name, release, and architecture}
|
||||
The \texttt{-s}, \texttt{-r}, and \texttt{-m} fields report precisely the
|
||||
kernel name, kernel release, and machine architecture requested.
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ uname -srm
|
||||
Linux 6.18.33.2-microsoft-standard-WSL2 x86_64
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
\subsection*{Q34. Last five recorded sessions}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
This Linux environment does not provide the legacy \texttt{last} command.
|
||||
The problem explicitly permits last sessions, so the systemd login journal is
|
||||
queried instead. \texttt{-o cat} removes journal metadata, and \texttt{tail}
|
||||
retains the five most recent session records at execution time.
|
||||
\par\medskip
|
||||
{\scriptsize
|
||||
\begin{Verbatim}[fontsize=\scriptsize]
|
||||
$ journalctl -o cat -u systemd-logind --no-pager | grep 'New session' | tail -5
|
||||
New session 'c4' of user 'vsun' with class 'user' and type 'tty'.
|
||||
New session 'c5' of user 'vsun' with class 'user' and type 'tty'.
|
||||
New session 'c6' of user 'vsun' with class 'user' and type 'tty'.
|
||||
New session 'c7' of user 'vsun' with class 'user' and type 'tty'.
|
||||
New session 'c8' of user 'vsun' with class 'user' and type 'tty'.
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
% ============================================================
|
||||
\section{(I) Package and Services (Debian/Ubuntu)}
|
||||
% ============================================================
|
||||
|
||||
\subsection*{Q35. Installed version of \texttt{coreutils}}
|
||||
\texttt{dpkg-query -W} reads the installed package database, and the format
|
||||
string prints only the installed version followed by a newline.
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ dpkg-query -W -f='${Version}\n' coreutils
|
||||
9.5-1ubuntu2+0.0.0~ubuntu25
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
\subsection*{Q36. Available package names containing \texttt{ripgrep}}
|
||||
\texttt{apt-cache search} uses the locally available APT package metadata;
|
||||
\texttt{--names-only} restricts matching to package names rather than their
|
||||
descriptions.
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ apt-cache search --names-only ripgrep
|
||||
ripgrep - Recursively searches directories for a regex pattern
|
||||
ripgrep-all - search pdfs, ebooks, documents, archives
|
||||
\end{Verbatim}
|
||||
}
|
||||
|
||||
\subsection*{Q37. Print only the \texttt{cron} service status line}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
\texttt{systemctl status} checks the service without changing it. The
|
||||
\texttt{sed} expression prints only the indented \texttt{Active:} line, while
|
||||
\texttt{--no-pager} keeps the command noninteractive.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ systemctl status cron --no-pager | sed -n '/^[[:space:]]*Active:/p'
|
||||
Active: active (running) since Mon 2026-09-14 18:01:01 EDT; 8min ago
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
% ============================================================
|
||||
\section{(J) Bash and Scripting}
|
||||
% ============================================================
|
||||
|
||||
\subsection*{Q38. Loop over \texttt{src/*.txt} and print file line counts}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The source PDF visibly retains only the separator in the requested
|
||||
\texttt{filename: line-count} format, so that conventional interpretation is
|
||||
used here. The glob supplies each text file, command substitution obtains its
|
||||
line count, and quoted expansions preserve filenames safely.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ for f in src/*.txt; do printf '%s: %s\n' "$f" "$(wc -l < "$f")"; done
|
||||
src/file1.txt: 1
|
||||
src/file2.txt: 1
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q39. Export \texttt{dept == "CS"} rows to \texttt{cs.txt}}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
The comma field separator makes department the third field. \texttt{NR > 1}
|
||||
excludes the header, and awk's default action prints each complete matching
|
||||
row; redirection writes those rows to \texttt{cs.txt}.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ awk -F, 'NR > 1 && $3 == "CS"' people.csv > cs.txt
|
||||
$ cat cs.txt
|
||||
2,Linus,CS
|
||||
4,Dennis,CS
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
|
||||
\subsection*{Q40. Create, print, and remove environment variable \texttt{X}}
|
||||
\noindent\begin{minipage}{\textwidth}
|
||||
\texttt{export} creates \texttt{X} in the shell environment. After printing
|
||||
its value, \texttt{unset} removes it; the final \texttt{printenv} check fails
|
||||
to find the name and therefore prints the confirmation message.
|
||||
\par\medskip
|
||||
{\small
|
||||
\begin{Verbatim}
|
||||
$ export X=42
|
||||
$ printf '%s\n' "$X"
|
||||
42
|
||||
$ unset X
|
||||
$ printenv X || echo "X is no longer in the environment"
|
||||
X is no longer in the environment
|
||||
\end{Verbatim}
|
||||
}
|
||||
\end{minipage}
|
||||
Reference in New Issue
Block a user