Final updates to «terminal commands»
This commit is contained in:
parent
5e96b11816
commit
9ecbab55d2
@ -57,3 +57,5 @@ Date,Topic,Minutes
|
||||
2026.01.31, proofreading, 60
|
||||
2026.02.01, terminal commands, 405
|
||||
2026.02.04, terminal commands, 35
|
||||
2026.02.07, terminal commands, 120
|
||||
2026.02.09, terminal commands, 100
|
||||
|
||||
|
@ -71,13 +71,19 @@ This may not seem like a useful command, but if you start wandering around in th
|
||||
|
||||
If you forget who you are, you can also use the \texttt{whoami} command, which simply returns your user name. Again, this may not seem very useful, but sometimes you have to log in as the root user, and if you forget whether you're logged in as the root user or a regular user, this becomes not handy but absolutely necessary. (You can wreak havoc on your system quite easily when logged in as root, so this is a good thing to check.) If you are logged in as the root user, \texttt{whoami} should return ``root'' rather than your regular user name.
|
||||
|
||||
\subsection{A Note on Conventions}
|
||||
|
||||
In the code examples that follow, I prefix any commands with a dollar sign (\$) which represents your terminal cursor. Output from any command will not have that. (See the code examples in section \ref{conventions} for examples.)
|
||||
|
||||
Arguments (see the next section) are typically shown between angle brackets, so if you see something like this \texttt{rm <file>}, you would replace ``<file>'' with the actual name of your file. If your file is named ``\texttt{cookie\_list.txt}'', the actual command would be ``\texttt{rm cookie\_list.txt}''.
|
||||
|
||||
\subsection{Arguments and Switches}
|
||||
|
||||
Bash commands follow the Unix principle (``Do one thing and do it well'') but that doesn't mean that they are simple. I mean, they are simple, but they can accomplish complex things.\footnote{Which I guess means that ``simple'' is a matter of perspective. When I was first starting with Linux, I thought these commands were terribly complicated. But as I've gained experience, I went from ``complex input $\rightarrow$ simple intput'' to ``simple output $\rightarrow$ complex output''. Experience and knowledge change your perspective.}
|
||||
|
||||
Most but not all commands have both \textit{arguments} and \textit{switches}, which are sometimes called \textit{options}. (I like the term ``switch'' because I like to think that a command can switch between doing things in different ways.) For example, if I want to copy a file on the command line, I need to tell the terminal which file I'd like to copy. That file's name is the \textit{argument}—in other words, it's the thing I want the command to act on.
|
||||
|
||||
Sometimes an argument is obvious and can be omitted. For example, if I want to list the contents of a directry, the \texttt{ls} command (which we will talk about in a bit) assumes that the argument is the current directory. If I want to see the contents of a different directory, I either need to move to that directory, or specify that directory's path as an argument.
|
||||
Sometimes an argument is obvious and can be omitted. For example, if I want to list the contents of a directory, the \texttt{ls} command (which we will talk about in a bit) assumes that the argument is the current directory. If I want to see the contents of a different directory, I either need to move to that directory, or specify that directory's path as an argument.
|
||||
|
||||
Switches (i.e., options) affect \textit{how} a command carries out its work. Suppose I want to see the contents of the directory I'm currently in. I would use the \texttt{ls} command for that. But do I want to see any invisible files or subdirectories? By default, the \texttt{ls} command omits those. If I want to see those (which is useful if I'm looking at configuration files, which are typically created and modified by your software and are thus hidden), I need to use the \texttt{-a} switch, which switches from hiding invisible files and subdirectories to showing invisible files and subdirectories.
|
||||
|
||||
@ -88,11 +94,11 @@ As it turns out, most commands have two options for switches. For example, the m
|
||||
do not ignore entries starting with .
|
||||
\end{Verbatim}
|
||||
|
||||
Both \texttt{-a} (one hyphen) and \texttt{--all} (two hyphens) have the same effect: they both show invisible files and subdirectories. So why are there two versions of this switch? I'm not 100\% sure as to the reason\footnote{Because the amount of research I have done here is exactly equal to zero.} but I suspect it has to do with how the human mind remembers information. For example, if I want to see \textit{all} the files in a directory, including the invisible ones, it's pretty easy to remember the two-hyphen version (\texttt{ls --all}). But in this case, ``\texttt{-a}'' is also short for ``\texttt{--all}'' All I need to do is remember that ``a'' is a shortcut for ``all''.
|
||||
Both \texttt{-a} (one hyphen) and \texttt{-}\texttt{-all} (two hyphens) have the same effect: they both show invisible files and subdirectories. So why are there two versions of this switch? I'm not 100\% sure as to the reason\footnote{Because the amount of research I have done here is exactly equal to zero.} but I suspect it has to do with how the human mind remembers information. For example, if I want to see \textit{all} the files in a directory, including the invisible ones, it's pretty easy to remember the two-hyphen version (\texttt{ls --all}). But in this case, ``\texttt{-a}'' is also short for ``\texttt{--all}'' All I need to do is remember that ``a'' is a shortcut for ``all''.
|
||||
|
||||
Most command switches work this way. Keep in mind, there are no hard and fast rules for this. The single-hyphen switch is not always lowercase (for example, the \texttt{cp} command has both \texttt{-l} which means ``hard link files instead of copying'' and \texttt{-L} which means ``always follow symbolic links in SOURCE''; the lower-case and upper-case options have nothing to do with each other), and the single-hypen single letter option doesn't always have a double-hypehn full word option.
|
||||
|
||||
\subsection{Moving Around}
|
||||
\subsection{Moving Around}\label{conventions}
|
||||
|
||||
Now that you know who and where you are, you probably want to move around a bit. The command to change directories is \texttt{cd} (which stands for ``change directory''—sometimes these things make sense). If you are in your home directory and you want to change to your Documents directory, you would use:
|
||||
|
||||
@ -132,6 +138,8 @@ For example, if I run \texttt{ls -ahl} in my ``Documents'' directory, I get this
|
||||
|
||||
\begin{small}
|
||||
\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{Output of 'ls -ahl' in Documents directory}]
|
||||
drwxr-xr-x 45 kjodle kjodle 96K Jan 26 15:35 .
|
||||
drwxr-xr-x 74 kjodle kjodle 4.0K Feb 9 19:48 ..
|
||||
drwxrwxr-x 2 kjodle kjodle 4.0K Mar 6 2021 'About Me'
|
||||
drwxrwxrwx 5 kjodle kjodle 4.0K Aug 5 2023 'Audio Recordings'
|
||||
-rwxrwxr-x 1 kjodle kjodle 1.5K Jan 3 15:08 backup.sh
|
||||
@ -141,8 +149,6 @@ drwxrwxrwx 15 kjodle kjodle 4.0K Oct 6 07:51 Business
|
||||
drwxrwxrwx 3 kjodle kjodle 4.0K Mar 30 2019 'Business Cards'
|
||||
drwxrwxr-x 4 kjodle kjodle 4.0K Jul 29 2022 calibre
|
||||
drwxrwxrwx 8 kjodle kjodle 4.0K Mar 6 2018 'Calibre Projects'
|
||||
drwxrwxrwx 3 kjodle kjodle 4.0K Jul 5 2020 Collections
|
||||
drwxrwxr-x 16 kjodle kjodle 4.0K Aug 13 2023 Correspondence
|
||||
*snip*
|
||||
\end{Verbatim}
|
||||
\end{small}
|
||||
@ -168,11 +174,11 @@ will create a file called ``documents.txt'' that contains a list of everything (
|
||||
|
||||
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.
|
||||
|
||||
The simplest of these is \texttt{locate}. All you have to do is provide a search term and \texttt{locate} will search your entire filesystem for any item that contains that search term.
|
||||
\paragraph{locate} The simplest of these is \texttt{locate}. All you have to do is provide a search term and \texttt{locate} will search your entire filesystem for any item that contains that search term.
|
||||
|
||||
\texttt{locate} is a fairly blunt instrument, however. Unless the search term you provide to it is extremely specific, it will return a \textit{very} long list of results. To get some idea of how many results you will get, you can pass the count option (\texttt{-c}) to it. Using ``latex'' as a search term with the count option returns ``122132'' which means that there are just around an eighth of a million items on my filesystem with ``latex'' in the name.
|
||||
|
||||
A more useful command is \texttt{find}, which has a \textit{ton} of options. (The \texttt{man} page runs to over 1,600 lines, so yep, a lot of options.)
|
||||
\paragraph{find} A more useful command is \texttt{find}, which has a \textit{ton} of options. (The \texttt{man} page runs to over 1,600 lines, so yep, a lot of options.)
|
||||
|
||||
A command like \texttt{find <term>} will search your entire filesystem for directories and files containing \texttt{<term>} in their name. To search just your home directory, you can use \texttt{find \$HOME <term>}.
|
||||
|
||||
@ -204,7 +210,7 @@ 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{grep} This utility was originally written back in 1973 by Ken Thompson (one of the original designers of Unix) as way to search text for lines that match a regular expression (i.e., regex). If you are having trouble finding what you are looking for using wildcards, you'll want to use \texttt{grep}, but in order to use \texttt{grep} effectively, you really need to understand regex. That means, unfortunately, that any discussion of \texttt{grep} is really outside the bounds of this little tutorial.\footnote{I do intend to eventually get around to it, though.}
|
||||
|
||||
\subsection{Working with Files and Directories}
|
||||
|
||||
@ -218,11 +224,25 @@ However, \texttt{cat} can also be used to create files. If you type \texttt{cat
|
||||
|
||||
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{>}.
|
||||
|
||||
\paragraph{head}
|
||||
\paragraph{head} If you're looking at log files, the \texttt{head} command can be very useful, because it shows the first ten lines of a file and then exits. You can use the ``-n'' option to specify a different number of lines, like this:
|
||||
|
||||
\paragraph{tail}
|
||||
\input{include/head_n20}
|
||||
|
||||
\paragraph{more}
|
||||
\paragraph{tail} Of course, if new information is appended to the end of the log file, then you can use the \texttt{tail} command, which works pretty much the same way the \texttt{head} command does.
|
||||
|
||||
\paragraph{more} If \texttt{head} and \texttt{tail} don't get you what you need, you can always use the \texttt{more} command, which allows you to page through text one screenful at a time. You can see the next line by pressing the space bar, or see the next screen by pressing either \kkey{z} or \kkey{enter}. I particularly ``\texttt{-c}'' option, which does scroll, but clears the screen before displaying the next screenful of text. I find that it's easier to keep track of what I'm looking at.
|
||||
|
||||
The \texttt{more} command will also tell you how much of the file you've seen in the bottom left corecer of the screen. For example, if I use this command to look at the main \LaTeX{} file for this issue:
|
||||
|
||||
\input{include/more_codex_command}
|
||||
|
||||
I then get this output:
|
||||
|
||||
\input{include/more_codex_output}
|
||||
|
||||
You'll also notice that the \texttt{more} command can use the ``\texttt{-n}'' option just like \texttt{head} and \texttt{tail} can.
|
||||
|
||||
\paragraph{less} The \texttt{less} command is often described as ``\texttt{more} on steroids'' which is a pretty apt description, as it was designed to overcome the limitations of \texttt{more} by having tons of options. It has so many options that it's really beyond the scope of this guide. Keep in mind that if you ever start bumping up against the limitations of \texttt{more}, then it's time to get familiar with \texttt{less}.
|
||||
|
||||
\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 dir1/dir2/dir3} will create a directory named ``dir3'' in that path, provided that directories ``dir1'' and ``dir2'' exist in the first place. If they don't, \texttt{mkdir} will return an error.
|
||||
|
||||
@ -234,6 +254,14 @@ Notice that we are using a redirect symbol here (\texttt{>}) to push your conten
|
||||
|
||||
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''.
|
||||
|
||||
If you want to move a file that is in a parent directory to the current directory, you can use:
|
||||
|
||||
\input{include/mv_file_down}
|
||||
|
||||
and if you want to move that file back, you can use:
|
||||
|
||||
\input{include/mv_file_up}
|
||||
|
||||
\paragraph{rmdir} To remove a directory, just use the \texttt{rmdir} command. There is one caveat with this command, however: the directory must be empty, because this command only works on empty directories. To remove a directory that \textit{isn't} empty (which means first removing all its contents and then finally removing the directory), you'll need to use the \texttt{rm} command.
|
||||
|
||||
\paragraph{rm} To remove a file, use the \texttt{rm} command with the name of the file as an argument. The \texttt{rm} does take wildcards by the way, which means that you can do a lot of work (or a lot of damage) with very little effort. For example, this command:
|
||||
|
||||
3
006/include/head_n20.tex
Normal file
3
006/include/head_n20.tex
Normal file
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[]
|
||||
$ head -n20 file.txt
|
||||
\end{Verbatim}
|
||||
3
006/include/more_codex_command.tex
Normal file
3
006/include/more_codex_command.tex
Normal file
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[]
|
||||
$ more -c -n10 codex-006.tex
|
||||
\end{Verbatim}
|
||||
13
006/include/more_codex_output.tex
Normal file
13
006/include/more_codex_output.tex
Normal file
@ -0,0 +1,13 @@
|
||||
\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{Output of the more command}]
|
||||
\documentclass[twoside]{report}
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% Packages %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage[utf8]{inputenc}
|
||||
\usepackage[T1]{fontenc}
|
||||
\usepackage{amsmath}
|
||||
\usepackage{amssymb}
|
||||
\usepackage{makeidx}
|
||||
\usepackage{graphicx}
|
||||
--More--(5%)
|
||||
\end{Verbatim}
|
||||
3
006/include/mv_file_down.tex
Normal file
3
006/include/mv_file_down.tex
Normal file
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[]
|
||||
$ mv ../file.txt .
|
||||
\end{Verbatim}
|
||||
3
006/include/mv_file_up.tex
Normal file
3
006/include/mv_file_up.tex
Normal file
@ -0,0 +1,3 @@
|
||||
\begin{Verbatim}[]
|
||||
$ mv file.txt ../
|
||||
\end{Verbatim}
|
||||
Loading…
Reference in New Issue
Block a user