From 90c5a9521fc87ebd05e53a6aa2f3d6589b424d59 Mon Sep 17 00:00:00 2001 From: Kenneth Odle Date: Wed, 4 Feb 2026 22:04:06 -0500 Subject: [PATCH] Updates to basic terminal commands --- 006/chapters/basic_terminal_commands.tex | 48 +++++++++++++++++++----- 006/include/ls_redirect.tex | 3 ++ 2 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 006/include/ls_redirect.tex diff --git a/006/chapters/basic_terminal_commands.tex b/006/chapters/basic_terminal_commands.tex index 6c88667..5bb82dc 100644 --- a/006/chapters/basic_terminal_commands.tex +++ b/006/chapters/basic_terminal_commands.tex @@ -35,7 +35,21 @@ Over to the right, we see a DEC terminal, which is similar to the terminal I use \section{Basic Commands} -Enough about me. Let's talk about some basic terminal commands you can start using right now. +Enough about me. Let's talk about some basic terminal commands you can start using right now. But first, let's talk about information, and which way it flows… + +\section{Input, Output, Errors, Redirects, and Piping} + +Every command has three things you need to consider: what input you provide the command, what output the command gives you, and what kinds of errors (hopefully none) you encounter. + +We have standard names for these things: \texttt{stdin}, \texttt{stdout}, and \texttt{stderr}, because these things are, well…\textit{standard}. We refer to these things as ``standard streams'' because again, they are standard with every command you give, and they are also a stream—meaning a flow of information.\footnote{See \kref{https://en.wikipedia.org/wiki/Standard_streams}{https://en.wikipedia.org/wiki/Standard\_streams} for a bit more information.} It's important to understand what these things are, because you are going to encounter them in online discussions about terminal commands, and as will all things, understanding these things makes your life easier. + +For example, let's say that you want to see the contents of a particular directory. You would use the command \texttt{ls} (which we will talk about in a bit), and the \texttt{stdin} you would provide would be the name of the directoy. The \texttt{stdout} would be the contents of that directory. If you misspelled the name of the directory or the directory doesn't exist, then the \texttt{ls} command would give you an error like ``cannot access : No such file or directory'', which would be the \texttt{stderr}. + +It's important to understand standard streams because you need to understand exactly what a command needs to get from you in order to operate correctly, and you also need to understand where it is sending its \texttt{stdout} and \texttt{stderr} in case you get something you weren't expecting. For the most part, simple commands send their \texttt{stdout} and \texttt{stderr} to your monitor. (If this were 1972, those would probably go to that line printer attached to your terminal.) But it's also possible to \textit{redirect} those streams to a file, via a redirect symbol (which is typically >). This is sometimes handy if you want a record of that information, or you need to use that data in a different program like a spreadsheet. + +It's also possible to take the \texttt{stdout} of one command and use it as the \texttt{stdin} of another command. This is called ``piping'' and makes use of the pipe symbol (that is, |) that is generally located on the key just above the \kkey{enter} key on your keyboard. This is a good technique when you know the command you are using will produce a lot of output, but you are only concerned with a small subset of it, so you send that output through a command (usually \texttt{grep}, which we will talk about) to filter out just the bits that you are interested in. + +We'll look at specifics about redirects and piping when we discuss actual commands. \subsection{Accessing the Terminal} @@ -131,6 +145,12 @@ That seems like a lot of information, but it's highly useful information. To sum \item The sixth and final column is the name. Again, notice that Linux puts single quotation marks around any names that contain a space. \end{itemize} +As I mentioned earlier, you can also redirect the \texttt{stdout} of this command to a file. For example, this command: + +\input{include/ls_redirect} + +will create a file called ``documents.txt'' that contains a list of everything (except for invisible files and directories) in your ``Documents'' folder. + \subsection{Finding Things} The Linux filesystem is large, and chances are that your home directory is also large and complex. It's easy to lose things. Fortunately, Linux has a number of commands to help you find things. @@ -147,9 +167,9 @@ A command like \texttt{find } will search your entire filesystem for direc A \textit{binary} is just another name for an application file that you can run. Often you need to find where a binary is located so that you can add it as a command to another application. You have a couple of options here. -\texttt{which} will return the name of all binaries that are located in your \texttt{PATH} variable, which is a list of locations that your system will search when attempting to execute that command. For example, if I run \texttt{which latex} in a terminal, I get this output \texttt{/usr/bin/latex}. This tells me a couple of things. First, it means that LaTeX is installed on my system. Second, it tells me that its location is contained in my \texttt{PATH} variable, which means that I can use it by simply typing \texttt{latex} in a terminal. +\paragraph{which} This command will return the name of all binaries that are located in your \texttt{PATH} variable, which is a list of locations that your system will search when attempting to execute that command. For example, if I run \texttt{which latex} in a terminal, I get this output \texttt{/usr/bin/latex}. This tells me a couple of things. First, it means that LaTeX is installed on my system. Second, it tells me that its location is contained in my \texttt{PATH} variable, which means that I can use it by simply typing \texttt{latex} in a terminal. -Sometimes binaries get installed without getting added to the \texttt{PATH}, however. To find all binaries, regardless of whether or not they are in the \texttt{PATH}, use \texttt{wheris}. For example, typing \texttt{whereis latex} in my terminal gives me this output: +\paragraph{whereis} Sometimes binaries get installed without getting added to the \texttt{PATH}, however. To find all binaries, regardless of whether or not they are in the \texttt{PATH}, use \texttt{wheris}. For example, typing \texttt{whereis latex} in my terminal gives me this output: \begin{Verbatim}[] latex: /usr/bin/latex /usr/share/man/man1/latex.1.gz @@ -163,7 +183,7 @@ The \texttt{find} command is useful because it can take wildcards. A full discus The first is \texttt{?} which takes the place of a single character. Thus \texttt{find ?at} would match ``cat'' and ''hat'' but it wouldn't match ``what''. -There is also \texttt{*} which matches any character of any length. The command \texttt{find *at} would match ``cat'', ``hat'', ``what'', and also ``casey\_at\_the\_bat''. +There is also \texttt{*} which matches any character of any length. The command \texttt{find *at} would match ``cat'', ``hat'', ``what'', and also ``casey\_at\_the\_\-bat''. You can also use square brackets to provide a comma-delimited list of characters to match. The command \texttt{find [b,c]at} would match both ``bat'' and ``cat'' but it wouldn't match ``hat''. @@ -171,21 +191,29 @@ Of course, you can combine these. \texttt{find *[b,c]at} would match ``bat'' and These are the most basic examples of wildcard usage, but they have sufficed for 99\% of the searches I've done over the years, if not more. I will talk about regex in a later issue, though, because it is one of the most useful skills to have. +\subsection{grep} + \subsection{Working with Files and Directories} Bash has a number of commands for working with files and directories. The first is \texttt{touch} which will create a file if it doesn't exist, or will update the date and time it was modified if it does exist.\footnote{I admit, I'm a little confused as to the purpose of this command, and have never found a use for it. It is probably a legacy from the early days of Linux or possbily Unix, and I have never had the time—or the amount of curiosity required—to investigate it.} -A command I do find a lot of use for however is \texttt{cat}, which is short for ``concatenate''. This command can also create files and add content to them, but it can also add content to existing files. But the thing I find it most useful for is simply displaying the contents of a file, which I use a lot when examining log files. +\paragraph{cat} A command I do find a lot of use for however is \texttt{cat}, which is short for ``concatenate''. This command can also create files and add content to them, but it can also add content to existing files. But the thing I find it most useful for is simply displaying the contents of a file, which I use a lot when examining log files. However, \texttt{cat} can also be used to create files. If you type \texttt{cat > } in the terminal, it will create a file named ``'' and then go into \textit{interactive mode}, meaning it looks like it's not doing anything. In reality, it's waiting for you to add input. Type what you want the contents of the file to be and then press \kkey{ctrl+d} to save the file and exit interactive mode. -Notice that we are using a redirect symbol here (\texttt{>}) to push your content to that file. If you repeat this command with different text, \texttt{cat} will just overwrite what you initially wrote. To append text to a file, rather than overwrite it, use \texttt{>>}. +Notice that we are using a redirect symbol here (\texttt{>}) to push your content to that file. If you repeat this command with different text, \texttt{cat} will just overwrite what you initially wrote. To append text to a file, rather than overwrite it, use \texttt{>}\texttt{>}. -To make a directory you can use the \texttt{mkdir} command, which is pretty straightforward. You can also create subdirectories by specifying the path to the subdirectory you want to create. For example, \texttt{mkdir 1/2/3} will create a directory named ``3'' in that path, provided that directories 1 and 2 exist in the first place. If they don't, \texttt{mkdir} will return an error. +\paragraph{head} -If you want to make a copy of a file, use the \texttt{cp} command. Its syntax is pretty straightfoward: just specify the file you want to copy and then specify the name of the copied file. You can also use paths as well, so \texttt{cp 1/2/a.txt 3/4/b.txt} will go to directory ``1'' and then to directory ``2'' and copy the file ``a.txt'' to directory ``4'' located in directory ``3'' and rename it as ``b.txt''. Again, all those directories must exist in the first place, or \texttt{cp} will return an error. +\paragraph{tail} -To move a file, use the \texttt{mv} command. Just specify the name of the file you want to move, and then the path you want to move it to. Interestingly, Linux does not have a command for renaming a file, but you can use the \texttt{mv} command to rename a file by not specifying a new path. Take a look at the following examples: +\paragraph{more} + +\paragraph{mkdir} To make a directory you can use the \texttt{mkdir} command, which is pretty straightforward. You can also create subdirectories by specifying the path to the subdirectory you want to create. For example, \texttt{mkdir 1/2/3} will create a directory named ``3'' in that path, provided that directories 1 and 2 exist in the first place. If they don't, \texttt{mkdir} will return an error. + +\paragraph{cp} If you want to make a copy of a file, use the \texttt{cp} command. Its syntax is pretty straightfoward: just specify the file you want to copy and then specify the name of the copied file. You can also use paths as well, so \texttt{cp 1/2/a.txt 3/4/b.txt} will go to directory ``1'' and then to directory ``2'' and copy the file ``a.txt'' to directory ``4'' located in directory ``3'' and rename it as ``b.txt''. Again, all those directories must exist in the first place, or \texttt{cp} will return an error. + +\paragraph{mv} To move a file, use the \texttt{mv} command. Just specify the name of the file you want to move, and then the path you want to move it to. Interestingly, Linux does not have a command for renaming a file, but you can use the \texttt{mv} command to rename a file by not specifying a new path. Take a look at the following examples: \begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{Examples of the mv Command}] mv cat.txt bat.txt @@ -195,6 +223,8 @@ mv cat.txt animals/mammals/bat.txt The command in line 1 simply renames the file ``cat.txt'' to ``bat.txt'' and leaves it in the same directory. The command in line 2 moves the file ``cat.txt'' to the subdirectory ``mammals'' which is located in the subdirectory ``animals'' which itself is a subdirectory of the current directoy. The third command is a combination of the first two: it both moves the ``cat.txt'' file and also renames it as ``bat.txt''. +\paragraph{rmdir} + \section{Two More Important Things} If your terminal window gets filled with text and you find that distracting (which my ADHD brain often does), just use the command \texttt{clear} to get an empty window. diff --git a/006/include/ls_redirect.tex b/006/include/ls_redirect.tex new file mode 100644 index 0000000..54e9e9c --- /dev/null +++ b/006/include/ls_redirect.tex @@ -0,0 +1,3 @@ +\begin{Verbatim}[] +$ ls Documents > documents.txt +\end{Verbatim}