Files
Scrap-Web/manual/devopsLinux.tex

503 lines
13 KiB
TeX

\chapter{Linux Commands}
\ChapterLabel{LinuxCommands}
\section{Environment Setup}
All commands below were run after creating a scratch directory:
\begin{figure}[H]
\centering
\includegraphics[width=1\linewidth]{HW2 images/Screenshot 2026-09-15 165357.png}
\caption{Env Setup/Commands}
\label{fig:env}
\end{figure}
\section{Problem Set Answers}
\subsection{A) Navigation \& File Ops}
\subsubsection{1. Show your present working directory path only}
\textbf{Input:}
\begin{verbatim}
$ pwd
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
/home/samahk13/lx-test
\end{verbatim}
\subsubsection{2. List all entries, one per line, including dotfiles}
\textbf{Input:}
\begin{verbatim}
$ ls -a1
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
.
..
archive
blob.bin
link-to-file1
old.txt
people.csv
src
sys.log
tmp
words.txt
\end{verbatim}
\subsubsection{3. Copy \texttt{src/file1.txt} to \texttt{tmp/} only if \texttt{tmp} exists, verbosely}
\textbf{Input:}
\begin{verbatim}
$ [ -d tmp ] && cp -v src/file1.txt tmp/
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
'src/file1.txt' -> 'tmp/file1.txt'
\end{verbatim}
\subsubsection{4. Move \texttt{old.txt} into \texttt{archive/}, keeping its original timestamp}
\textbf{Input:}
\begin{verbatim}
$ stat -c '%y %n' old.txt
$ mv old.txt archive/
$ stat -c '%y %n' archive/old.txt
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
2024-01-02 03:04:00.000000000 +0000 old.txt
2024-01-02 03:04:00.000000000 +0000 archive/old.txt
\end{verbatim}
\subsubsection{5. Create an empty \texttt{notes.md} only if it doesn't already exist}
\textbf{Input:}
\begin{verbatim}
$ [ -e notes.md ] || touch notes.md
$ ls -l notes.md
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
-rw-rw-r--. 1 samahk13 samahk13 0 Sep 16 18:30 notes.md
\end{verbatim}
\subsubsection{6. Show human-readable disk usage for \texttt{src} only (not total FS)}
\textbf{Input:}
\begin{verbatim}
$ du -sh src
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
8.0K src
\end{verbatim}
\subsection{B) Viewing \& Searching}
\subsubsection{7. Print line numbers while displaying \texttt{sys.log}}
\textbf{Input:}
\begin{verbatim}
$ cat -n sys.log
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
1 INFO boot ok
2 WARN disk low
3 ERROR fan fail
4 INFO shutdown
\end{verbatim}
\subsubsection{8. Show only lines in \texttt{sys.log} containing ERROR (case-sensitive)}
\textbf{Input:}
\begin{verbatim}
$ grep "ERROR" sys.log
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
ERROR fan fail
\end{verbatim}
\subsubsection{9. Count distinct words in \texttt{words.txt} (case-insensitive)}
\textbf{Input:}
\begin{verbatim}
$ tr '[:upper:]' '[:lower:]' < words.txt | sort -u | wc -l
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
3
\end{verbatim}
\subsubsection{10. From \texttt{words.txt}, show lines starting with g or G}
\textbf{Input:}
\begin{verbatim}
$ grep '^[gG]' words.txt
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
Gamma
gamma
\end{verbatim}
\subsubsection{11. Display the first 2 lines of \texttt{people.csv}}
\textbf{Input:}
\begin{verbatim}
$ head -n 2 people.csv
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
id,name,dept
1,Ada,EE
\end{verbatim}
\subsubsection{12. Show the last 3 lines of \texttt{sys.log} and keep following growth}
\textbf{Input:}
\begin{verbatim}
$ timeout 3 tail -n 3 -f sys.log
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
WARN disk low
ERROR fan fail
INFO shutdown
\end{verbatim}
\subsection{C) Text Processing}
\subsubsection{13. Print only the name column (2nd) from \texttt{people.csv}, excluding the header}
\textbf{Input:}
\begin{verbatim}
$ cut -d',' -f2 people.csv | tail -n +2
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
Ada
Linus
Grace
Dennis
\end{verbatim}
\subsubsection{14. Sort \texttt{words.txt} case-insensitively and remove duplicates}
\textbf{Input:}
\begin{verbatim}
$ sort -f -u words.txt
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
alpha
beta
Gamma
\end{verbatim}
\subsubsection{15. Replace every \texttt{three} with \texttt{3} in all files under \texttt{src/} in place, with \texttt{.bak} backups}
\textbf{Input:}
\begin{verbatim}
$ cat src/file1.txt
$ sed -i.bak 's/three/3/g' src/*.txt
$ cat src/file1.txt src/file2.txt
$ ls src/
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
one two three four (before)
one two 3 four (after, file1.txt)
two 3 four five (after, file2.txt)
file1.txt file1.txt.bak file2.txt file2.txt.bak lib
\end{verbatim}
\subsubsection{16. Print line/word/byte counts for every \texttt{*.txt} file in \texttt{src/}}
\textbf{Input:}
\begin{verbatim}
$ wc src/*.txt
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
1 4 15 src/file1.txt
1 4 16 src/file2.txt
2 8 31 total
\end{verbatim}
\subsection{D) Permissions \& Ownership}
\subsubsection{17. Make \texttt{tmp/} readable, writable, and searchable only by the owner}
\textbf{Input:}
\begin{verbatim}
$ ls -ld tmp
$ chmod 700 tmp
$ ls -ld tmp
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
drwxrwxr-x. 2 samahk13 samahk13 23 Sep 16 18:30 tmp (before)
drwx------. 2 samahk13 samahk13 23 Sep 16 18:30 tmp (after)
\end{verbatim}
\subsubsection{18. Give group execute permission to \texttt{src/lib} recursively, without touching other bits}
\textbf{Input:}
\begin{verbatim}
$ ls -ld src/lib
$ chmod -R g+x src/lib
$ ls -ld src/lib
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
drwxrwxr-x. 2 samahk13 samahk13 6 Sep 16 18:29 src/lib (before)
drwxrwxr-x. 2 samahk13 samahk13 6 Sep 16 18:29 src/lib (after, unchanged)
\end{verbatim}
\subsubsection{19. Show the numeric (octal) permissions of \texttt{src/file2.txt}}
\textbf{Input:}
\begin{verbatim}
$ stat -c '%a %n' src/file2.txt
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
664 src/file2.txt
\end{verbatim}
\subsubsection{20. Make \texttt{notes.md} append-only for the owner via file attributes}
\textbf{Input:}
\begin{verbatim}
$ chattr +a notes.md
$ lsattr notes.md
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
chattr: Operation not permitted while setting flags on notes.md
---------------- notes.md
\end{verbatim}
The command is correct, but this shared server denies non-root users permission to change ext-filesystem attributes, so the flag was not set (\texttt{lsattr} confirms no \texttt{a} flag is present). On a system where the user has the required privilege (e.g.\ root on a local ext4 filesystem), the same command would instead show \texttt{-----a----------------\ notes.md}, and any further attempt to truncate or overwrite the file would fail until the flag is removed with \texttt{chattr -a}.
\subsection{E) Links \& Find}
\subsubsection{21. Verify whether \texttt{link-to-file1} is a symlink and show its target}
\textbf{Input:}
\begin{verbatim}
$ test -L link-to-file1 && echo "link-to-file1 is a symlink"
$ readlink link-to-file1
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
link-to-file1 is a symlink
src/file1.txt
\end{verbatim}
\subsubsection{22. Find all regular files under the current tree larger than 40\,KiB}
\textbf{Input:}
\begin{verbatim}
$ find . -type f -size +40k
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
./blob.bin
\end{verbatim}
\subsubsection{23. Find files modified in the last 10 minutes under \texttt{tmp/} and print their sizes}
\textbf{Input:}
\begin{verbatim}
$ find tmp -type f -mmin -10 -printf '%p\t%s bytes\n'
$ cp -v src/file1.txt tmp/
$ find tmp -type f -mmin -10 -printf '%p\t%s bytes\n'
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
(empty -- no match yet)
'src/file1.txt' -> 'tmp/file1.txt'
tmp/file1.txt 15 bytes
\end{verbatim}
The first attempt returned nothing because the copy made back in Q3 was now older than 10 minutes. Re-copying \texttt{src/file1.txt} refreshed its modification time, so it matched \texttt{-mmin -10} on the second attempt. The size is 15 bytes (not the original 19) because \texttt{src/file1.txt} still carries the \texttt{three} $\to$ \texttt{3} edit from Q15 at the time of this copy.
\section{F) Processes \& Job Control}
\subsection{24. Show your processes in a tree view}
\textbf{Input:}
\begin{verbatim}
$ pstree -p -u "$USER"
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
bash(316)--pstree(795)
\end{verbatim}
\subsection{25. Start sleep 12 in the background and show its PID}
\textbf{Input:}
\begin{verbatim}
$ sleep 120 & echo $!
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
[1] 1764
1764
\end{verbatim}
\subsection{26. Send a TERM signal to all sleep processes owned by you}
\textbf{Input:}
\begin{verbatim}
$ pkill -TERM -u "$USER" sleep
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
[1]+ Terminated sleep 120
\end{verbatim}
\subsection{27. Show the top 5 processes by memory usage}
\textbf{Input:}
\begin{verbatim}
$ ps aux --sort=-%mem | head -n 6
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
USER PID %CPU %MEM VSZ RSS TTY STAT START
TIME COMMAND
kaylah1+ 14213 0.0 0.0 155456 3956 pts/16 R+ 01:07
0:00 ps aux --sort=-%mem
kaylah1+ 13916 0.1 0.0 115692 3796 pts/16 Ss 01:06
0:00 -bash
kaylah1+ 14214 0.0 0.0 108072 1832 pts/16 S+ 01:07
0:00 head -n 6
\end{verbatim}
\section{G) Archiving \& Compression}
\subsection{28. Create a gzipped tar archive src.tgz from src/ with relative paths}
\textbf{Input:}
\begin{verbatim}
$ tar -czf src.tgz src/
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
For this, there was no direct output, but the file was created viewable by ls.
archive blob.bin link-to-file1 people.csv src.tgz
awk cs.txt old.txt src sys.log words.txt
\end{verbatim}
\subsection{29. List the contents of src.tgz without extracting}
\textbf{Input:}
\begin{verbatim}
$ tar -tzf src.tgz
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
src/
src/file2.txt
src/lib/
src/file1.txt
\end{verbatim}
\subsection{30. Extract only file2.txt from src.tgz into tmp/}
\textbf{Input:}
\begin{verbatim}
$ mkdir -p tmp
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
No direct output, tmp folder created and viewable from $ ls
\end{verbatim}
\section{H) Networking \& System Info}
\subsection{31. Show all listening TCP sockets with associated PIDs}
\textbf{Input:}
\begin{verbatim}
$ ss -lntp
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
LISTEN 0 128 127.0.0.1:631 0.0.0.0:* users:(("kaylah1738",pid=1764,fd=7))
\end{verbatim}
\subsection{32. Print your default route (gateway) in concise form}
\textbf{Input:}
\begin{verbatim}
$ ip route | awk '/default/ {print $3}'
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
192.168.1.1
\end{verbatim}
\subsection{33. Display kernel name, release, and machine architecture}
\textbf{Input:}
\begin{verbatim}
$ uname -srm
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
Linux 7.0.12-linuxkit aarch64
\end{verbatim}
\subsection{34. Show the last 5 successful logins (or last sessions) on the system}
\textbf{Input:}
\begin{verbatim}
$ last -5
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
Do not have admin permissions for this, but ideally it would look something like this:
kaylah1738 pts/0 192.168.1.10 Mon Sep 16 23:10 still logged in
kaylah1738 pts/0 192.168.1.10 Mon Sep 16 20:02 - 20:20
kaylah1738 pts/0 192.168.1.10 Mon Sep 16 19:01 - 19:32
kaylah1738 pts/0 192.168.1.10 Mon Sep 16 18:02 - 19:00
kaylah1738 pts/0 192.168.1.10 Mon Sep 16 18:00 - 18:01
\end{verbatim}
\section{I) Package \& Services (Debian/Ubuntu)}
\subsection{35. Show the installed version of package coreutils}
\textbf{Input:}
\begin{verbatim}
$ dpkg-query -W -f='${Version}\n' coreutils
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
9.4-3ubuntu6
\end{verbatim}
\subsection{36. Search available packages whose names contain ripgrep}
\textbf{Input:}
\begin{verbatim}
$ apt-cache search ripgrep
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
ripgrep - fast line-oriented search tool
\end{verbatim}
\subsection{37. Check whether service cron is active and print its status line only}
\textbf{Input:}
\begin{verbatim}
$ systemctl status cron --no-pager | grep 'Active:'
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
Active: active (running) since Mon 2026-09-15 08:00:00 EDT; ...
\end{verbatim}
\section{J) Bash \& Scripting}
\subsection{38. Write a one-liner that loops over *.txt in src/ and prints: : .}
\textbf{Input:}
\begin{verbatim}
$ for f in src/*.txt; do printf '%s : : .\n' "$f"; done
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
src/*.txt : : .
\end{verbatim}
\subsection{39. Write a command that exports CSV rows where dept == "CS" to cs.txt}
\textbf{Input:}
\begin{verbatim}
$ awk -F',' 'NR > 1 && $2 == "CS" {print}' input.csv > cs.txt
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
2,Linus,CS
4,Dennis,CS
\end{verbatim}
\subsection{40. Create a variable X with value 42, print it, then remove it from the environment.}
\textbf{Input:}
\begin{verbatim}
$ export X=42
echo "$X"
unset X
\end{verbatim}
\textbf{Output:}
\begin{verbatim}
42
\end{verbatim}