Files
smartpark-app/latex-manual/devopsLinuxCommands.tex
T

652 lines
24 KiB
TeX

\chapter{Linux Commands}
\ChapterLabel{LinuxCommands}
\index{Linux}
\index{command line}
\index{file operations}
\index{processes}
\index{networking}
\index{archiving}
\index{shell scripting}
\section{Activity Brief}
\subsection{Scenario}
This activity develops proficiency with common Linux command-line tools. The exercises were completed in an Ubuntu Linux environment using Windows Subsystem for Linux (WSL). The problem set covers navigation and file operations, viewing and searching, text processing, permissions, symbolic links, process control, archiving, networking, package management, services, and Bash scripting.
\subsection{Activities}
The Linux Command Proficiency problem set consists of ten sections, labeled A through J. The work was divided among the three group members as follows:
\begin{itemize}
\item \textbf{Jacob Radzieta}: Problems A, B, C, and D
\item \textbf{Nica Saoi}: Problems E, F, and G
\item \textbf{Vincent Penalosa}: Problems H, I, and J
\end{itemize}
Each group member was responsible for executing the commands in their assigned sections and documenting the corresponding terminal input and output for inclusion in the final report.
\subsection{Success Criteria}
The activity is complete when each required Linux command has been executed successfully and the corresponding terminal input and output have been documented.
%--------------------------------------------
%Jacob
\section{A) Navigation \& File Ops}
\subsection{Question 1: Show your present working directory path only}
This task displays the absolute path of the current working directory using the
\texttt{pwd} command.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q1.png}
\caption{Terminal input and output for Question 1.}
\end{figure}
The output displays the current working directory and confirms that the Linux
Commands activity was performed from the designated \texttt{lx-test} working
directory.
\subsection{Question 2: List all entries in the current directory, one per line, including dotfiles}
This task lists all entries in the current working directory, including hidden
entries, with one entry displayed per line.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q2.png}
\caption{Terminal input and output for Question 2.}
\end{figure}
The output lists the contents of the working directory one entry per line.
The use of the option that includes hidden files also displays the special
dot entries along with the files and directories created for the activity.
\subsection{Question 3: Copy src/file1.txt to tmp/ only if tmp exists; do it verbosely}
This task first verifies that the \texttt{tmp/} directory exists and then
copies \texttt{src/file1.txt} into it using verbose output.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q3.png}
\caption{Terminal input and output for Question 3.}
\end{figure}
The output confirms that \texttt{tmp/} existed and that
\texttt{src/file1.txt} was successfully copied into the directory. The
verbose copy output identifies both the source and destination paths.
\subsection{Question 4: Move old.txt into archive/ and keep its original timestamp}
This task moves \texttt{old.txt} into the \texttt{archive/} directory while
preserving the file's existing timestamp.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q4.png}
\caption{Terminal input and output for Question 4.}
\end{figure}
The output confirms that \texttt{old.txt} was moved into
\texttt{archive/}. The file information verifies that the timestamp assigned
during the initial environment setup was retained after the move.
\subsection{Question 5: Create a new empty file notes.md only if it doesn't already exist}
This task conditionally creates an empty file named \texttt{notes.md} only
when a file with that name does not already exist.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q5.png}
\caption{Terminal input and output for Question 5.}
\end{figure}
The resulting directory information confirms that \texttt{notes.md} exists.
The conditional command prevents an existing copy of the file from being
unnecessarily modified.
\subsection{Question 6: Show disk usage (human-readable) for the src directory only}
This task displays the disk usage of the \texttt{src/} directory in a
human-readable format without displaying usage information for the entire
filesystem.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q6.png}
\caption{Terminal input and output for Question 6.}
\end{figure}
The output reports the amount of disk space used by the \texttt{src/}
directory in a human-readable unit, providing a concise summary for that
directory only.
\section{B) Viewing \& Searching}
\subsection{Question 7: Print line numbers while displaying sys.log}
This task displays the contents of \texttt{sys.log} while adding a line
number to each displayed record.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q7.png}
\caption{Terminal input and output for Question 7.}
\end{figure}
The output displays all four entries in \texttt{sys.log} with corresponding
line numbers, making the location of each log entry easy to identify.
\subsection{Question 8: Show only the lines in sys.log that contain ERROR}
This task searches \texttt{sys.log} for the case-sensitive text
\texttt{ERROR} and displays only matching lines.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q8.png}
\caption{Terminal input and output for Question 8.}
\end{figure}
The output displays the log entry containing \texttt{ERROR} and excludes the
other informational and warning entries. This confirms that the search was
performed using the required case-sensitive match.
\subsection{Question 9: Count how many distinct words appear in words.txt}
This task converts the contents of \texttt{words.txt} to a common case,
removes duplicate entries, and counts the number of distinct words.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q9.png}
\caption{Terminal input and output for Question 9.}
\end{figure}
The output reports three distinct case-insensitive words. The values
\texttt{Gamma} and \texttt{gamma} are treated as the same word, and the
duplicate occurrences of \texttt{beta} are counted only once.
\subsection{Question 10: From words.txt, show lines that start with g or G}
This task searches \texttt{words.txt} for lines beginning with the letter
\texttt{g}, using a case-insensitive match so that both uppercase and
lowercase forms are included.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q10.png}
\caption{Terminal input and output for Question 10.}
\end{figure}
The output displays the lines containing \texttt{Gamma} and
\texttt{gamma}, confirming that both uppercase and lowercase matches at the
beginning of a line were selected.
\subsection{Question 11: Display the first 2 lines of people.csv without using an editor}
This task displays only the first two lines of \texttt{people.csv} directly
from the terminal.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q11.png}
\caption{Terminal input and output for Question 11.}
\end{figure}
The output displays the CSV header followed by the first data record. This
confirms that only the requested first two lines were read without opening
the file in an editor.
\subsection{Question 12: Show the last 3 lines of sys.log and keep following if the file grows}
This task displays the final three lines of \texttt{sys.log} and keeps the
command active so that newly appended log entries would be displayed as they
are written.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q12.png}
\caption{Terminal input and output for Question 12.}
\end{figure}
The initial output displays the final three log entries. The follow option
keeps the command attached to the file so that additional lines would appear
automatically if the log were updated.
\section{C) Text Processing}
\subsection{Question 13: From people.csv, print only the name column, excluding the header}
This task processes \texttt{people.csv} and displays only the second column,
which contains each person's name, while excluding the CSV header.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q13.png}
\caption{Terminal input and output for Question 13.}
\end{figure}
The output lists the four names from the CSV data without displaying the
header or the ID and department columns.
\subsection{Question 14: Sort words.txt case-insensitively and remove duplicates}
This task sorts the contents of \texttt{words.txt} without regard to letter
case and removes duplicate values from the resulting output.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q14.png}
\caption{Terminal input and output for Question 14.}
\end{figure}
The output contains one entry for each distinct case-insensitive word. The
duplicate occurrences of \texttt{beta} and the case variants of
\texttt{gamma} are reduced to single entries.
\subsection{Question 15: Replace every three with 3 in all files under src/ in-place, creating .bak backups}
This task replaces every occurrence of the word \texttt{three} with the
number \texttt{3} in the text files under \texttt{src/}. The files are
modified in place while backup copies with the \texttt{.bak} extension are
created.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q15.png}
\caption{Terminal input and output for Question 15.}
\end{figure}
The output verifies that the active text files now contain \texttt{3} in
place of \texttt{three}. The directory listing also confirms that backup
copies of the original files were retained using the \texttt{.bak}
extension.
\subsection{Question 16: Print the number of lines, words, and bytes for every *.txt file in src/}
This task uses a word-count operation to report the number of lines, words,
and bytes for every file matching \texttt{*.txt} in the \texttt{src/}
directory.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q16.png}
\caption{Terminal input and output for Question 16.}
\end{figure}
The output reports line, word, and byte counts for each matching text file
and includes a combined total. This provides a concise summary of the
contents of the text files in \texttt{src/}.
\section{D) Permissions \& Ownership}
\subsection{Question 17: Make tmp/ readable, writable, and searchable only by the owner}
This task changes the permissions of \texttt{tmp/} so that only the owner
has read, write, and execute permissions.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q17.png}
\caption{Terminal input and output for Question 17.}
\end{figure}
The resulting permissions show that the owner has full access to the
directory while group members and other users have no permissions. This
corresponds to an octal permission value of \texttt{700}.
\subsection{Question 18: Give group execute permission to src/lib recursively without touching others/owner bits}
This task recursively adds execute permission for the group to
\texttt{src/lib} without replacing or otherwise modifying the existing owner
or other permission bits.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q18.png}
\caption{Terminal input and output for Question 18.}
\end{figure}
The resulting permission information confirms that group execute permission
was added to \texttt{src/lib}. Using a symbolic permission change preserves
the existing owner and other permission settings.
\subsection{Question 19: Show the numeric permissions of src/file2.txt}
This task displays the permissions of \texttt{src/file2.txt} using their
numeric, or octal, representation.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q19.png}
\caption{Terminal input and output for Question 19.}
\end{figure}
The output provides the numeric permission value currently assigned to
\texttt{src/file2.txt}, allowing the owner, group, and other permission bits
to be interpreted directly.
\subsection{Question 20: Make notes.md append-only via file attributes}
This task applies the append-only file attribute to \texttt{notes.md} and
then displays the file's attributes to verify the change.
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{png/Q20.png}
\caption{Terminal input and output for Question 20.}
\end{figure}
The attribute information shows that the append-only attribute is enabled
for \texttt{notes.md}. With this attribute active, existing contents cannot
be overwritten through normal file operations and new data may only be
appended to the file.
% ----------------------------------------------------------------------- Jacob
%--------------------------------------------
% Nica
%E---------------------
\section{E) Links \& Find}
\subsection{Question 21: Verify a symbolic link}
The purpose of this task was to verify that \texttt{link-to-file1} is a symbolic link and display its target path.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q21.png}
\caption{Terminal input and output for Question 21.}
\label{fig:q21}
\end{figure}
The output confirms that \texttt{link-to-file1} is a symbolic link and shows that it points to \texttt{src/file1.txt}.
\subsection{Question 22: Find files larger than 40 KiB}
This task used the \texttt{find} command to locate all regular files under the current directory tree that were larger than 40 KiB.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q22.png}
\caption{Terminal input and output for Question 22.}
\label{fig:q22}
\end{figure}
The output shows that \texttt{blob.bin} is the only regular file in the current directory tree larger than 40 KiB.
\subsection{Question 23: Find recently modified files}
This task searched the \texttt{tmp/} directory for files modified within the last 10 minutes and displayed their sizes.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q23.png}
\caption{Terminal input and output for Question 23.}
\label{fig:q23}
\end{figure}
No output was produced because there were no files in \texttt{tmp/} that matched the search conditions at the time the command was run.
%F---------------------
\section{F) Processes \& Job Control}
\subsection{Question 24: Show processes in a tree view}
This task displays the processes owned by the current user in a tree structure so that parent-child process relationships can be observed.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q24.png}
\caption{Terminal input and output for Question 24.}
\label{fig:q24}
\end{figure}
The output displays the processes owned by the current user in a tree structure. The indentation shows the parent-child relationships between the running processes.
\subsection{Question 25: Start a background process and show its PID}
This task starts \texttt{sleep 120} as a background process and displays the process ID assigned to that background job.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q25.png}
\caption{Terminal input and output for Question 25.}
\label{fig:q25}
\end{figure}
The output shows that \texttt{sleep 120} was started as a background process. The displayed process ID identifies the newly created background job.
\subsection{Question 26: Send a TERM signal to sleep processes}
This task sends a TERM signal to all \texttt{sleep} processes owned by the current user without using the forceful \texttt{kill -9} signal.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q26.png}
\caption{Terminal input and output for Question 26.}
\label{fig:q26}
\end{figure}
The output confirms that a TERM signal was sent to the current user's \texttt{sleep} process, causing the background process to terminate.
\subsection{Question 27: Show the top five processes by memory usage}
This task displays the five processes owned by the current user that are using the highest percentage of system memory.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q27.png}
\caption{Terminal input and output for Question 27.}
\label{fig:q27}
\end{figure}
The output lists the five processes owned by the current user with the highest memory usage. The processes are sorted in descending order by their percentage of memory usage.
%G---------------------
\section{G) Archiving \& Compression}
\subsection{Question 28: Create a gzipped tar archive}
This task creates a gzip-compressed tar archive named \texttt{src.tgz} from the \texttt{src/} directory while preserving relative paths.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q28.png}
\caption{Terminal input and output for Question 28.}
\label{fig:q28}
\end{figure}
The command creates the archive \texttt{src.tgz} containing the \texttt{src/} directory. There is no output when the archive is created successfully.
\subsection{Question 29: List the contents of the archive}
This task lists the contents of \texttt{src.tgz} without extracting any of the files stored inside the archive.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q29.png}
\caption{Terminal input and output for Question 29.}
\label{fig:q29}
\end{figure}
The output lists the files and directories stored inside \texttt{src.tgz} without extracting them. This confirms that the \texttt{src/} directory and its contents were successfully archived.
\subsection{Question 30: Extract file2.txt into tmp}
This task extracts only \texttt{file2.txt} from the compressed archive and places it into the \texttt{tmp/} directory.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q30.png}
\caption{Terminal input and output for Question 30.}
\label{fig:q30}
\end{figure}
The output confirms that only \texttt{file2.txt} was extracted from the archive into the \texttt{tmp/} directory. The extracted file can then be checked to verify that the correct file was recovered.
%--------------------------------------------
%Vincent
\section{H) Networking \& System Info}
\subsection{Question 31: Show all listening TCP sockets with associated PIDs (no root assumptions).}
This task uses \texttt{sudo ss -ltnp} to display listening TCP sockets with numeric addresses and ports, along with the associated processes.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q31.png}
\caption{Terminal input and output for Question 31.}
\label{fig:q31}
\end{figure}
The output shows listening TCP sockets on ports 53 and 6379. Process information identifies \texttt{systemd-resolve} and \texttt{redis-server} for the sockets where process details are available.
\subsection{Question 32: Print your default route (gateway) in a concise form.}
This task uses \texttt{ip route} with \texttt{grep default} to display the system's default network route.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q32.png}
\caption{Terminal input and output for Question 32.}
\label{fig:q32}
\end{figure}
The output shows that the default route uses gateway \texttt{172.31.48.1} through the \texttt{eth0} network interface.
\subsection{Question 33: Display kernel name, release, and machine architecture.}
This task uses \texttt{uname -srm} to display the kernel name, kernel release, and machine architecture.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q33.png}
\caption{Terminal input and output for Question 33.}
\label{fig:q33}
\end{figure}
The output identifies the kernel as Linux, the kernel release as \texttt{6.18.33.2-microsoft-standard-WSL2}, and the machine architecture as \texttt{x86\_64}.
\subsection{Question 34: Show the last 5 successful logins (or last sessions) on the system.}
This task uses \texttt{last -n 5} to display the five most recent entries in the system's login history.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q34.png}
\caption{Terminal input and output for Question 34.}
\label{fig:q34}
\end{figure}
The five entries shown are system boot records labeled \texttt{reboot}, rather than individual user login sessions. The most recent entry is marked as still running.
\section{I) Package \& Services (Debian/Ubuntu)}
\subsection{Question 35: Show the installed version of package coreutils.}
This task queries the installed \texttt{coreutils} package using \texttt{dpkg -s} and filters the result to display its version.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q35.png}
\caption{Terminal input and output for Question 35.}
\label{fig:q35}
\end{figure}
The output shows that the installed version of \texttt{coreutils} is \texttt{9.4-3ubuntu6}.
\subsection{Question 36: Search available packages whose names contain ripgrep.}
This task uses \texttt{apt-cache search ripgrep} to search the local package cache for packages matching \texttt{ripgrep}.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q36.png}
\caption{Terminal input and output for Question 36.}
\label{fig:q36}
\end{figure}
The output includes \texttt{ripgrep}, described as a tool that recursively searches directories for a regular expression pattern, along with other matching packages. This command searches package information without installing a package.
\subsection{Question 37: Check whether service cron is active and print its status line only.}
This task uses \texttt{systemctl is-active cron} to check the current activity state of the \texttt{cron} service.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q37.png}
\caption{Terminal input and output for Question 37.}
\label{fig:q37}
\end{figure}
The output returns \texttt{active}, confirming that the \texttt{cron} service was running when the command was executed.
\section{J) Bash \& Scripting}
\subsection{Question 38: Write a one-liner that loops over *.txt in src/ and prints: : .}
This task uses a Bash \texttt{for} loop to process each \texttt{.txt} file in \texttt{src/} and display its filename and line count using \texttt{wc -l}.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q38.png}
\caption{Terminal input and output for Question 38.}
\label{fig:q38}
\end{figure}
The output shows that \texttt{src/file1.txt} and \texttt{src/file2.txt} each contain one line.
\subsection{Question 39: Write a command that exports CSV rows where dept == "CS" to cs.txt (exclude header).}
This task uses \texttt{awk} to locate the \texttt{dept} column in the header of \texttt{people.csv}, select records whose department is \texttt{CS}, and save the matching records to \texttt{cs.txt} without the header.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q39.png}
\caption{Terminal input and output for Question 39.}
\label{fig:q39}
\end{figure}
Displaying \texttt{cs.txt} with \texttt{cat} shows the two matching records: \texttt{2,Linus,CS} and \texttt{4,Dennis,CS}.
\subsection{Question 40: Create a variable X with value 42, print it, then remove it from the environment.}
This task exports the variable \texttt{X} with the value \texttt{42}, displays its value, and then removes the variable using \texttt{unset}.
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{png/Q40.png}
\caption{Terminal input and output for Question 40.}
\label{fig:q40}
\end{figure}
The first \texttt{echo} command displays \texttt{42}. After \texttt{unset X}, the second \texttt{echo} command produces a blank line because the variable is no longer set.