Edited Chapter 3

This commit is contained in:
Kenneth John Odle 2021-11-06 10:13:55 -04:00
parent 479a14079f
commit 1cbd8546b3

View File

@ -178,7 +178,7 @@ Despite all my prattling on about the many advantages the command line has for y
I'm not going to get into the difference between the command line, the terminal, and bash (the Bourne Again Shell, if you are interested\footnote{You may not be, but after all, you are reading \textit{this} so you very well may be. I'll talk about that in a future issue.}) For now, let's just assume that you know about \texttt{ctrl alt t} opening a terminal window to get you access to the command line.
If you are used to using the command line, chances are that you have a certain set of commands that you use a lot. For example, if you're pushing to your Github repos all the time, you probably are typing \verb=git push origin main= quite often. You can create a bash alias that makes your life a lot easier.
If you are used to using the command line, chances are that you have a certain set of commands that you use a lot. For example, if you're pushing to your Github repos all the time, you probably are typing \texttt{git push origin main} quite often. You can create a bash alias that makes your life a lot easier.
Chances are, you will open the terminal in the root of your \texttt{Home} directory. You can tell by typing
@ -186,19 +186,19 @@ Chances are, you will open the terminal in the root of your \texttt{Home} direct
$ pwd
\end{verbatim}
\texttt{pwd} stands for ``Print Working Directory'' and shows you where you are. If you're in the home directory, you'll get something that looks like this:
\texttt{pwd} stands for ``\texttt{print working directory}'' and shows you where you are. If you're in the home directory, you'll get something that looks like this:
\begin{verbatim}
/home/username
\end{verbatim}
\noindent{}where ``\texttt{username}'' is your login name. If you're \textit{not} in your home directory, you can get there with this command:
\noindent{}where ``\texttt{username}'' is your login name. If you're \textit{not} in your home directory, you can get there with this command:
\begin{verbatim}
$ cd ~
\end{verbatim}
\noindent{}\verb=$ cd= $\sim$
\noindent{}The \verb|~| is shorthand for your home directory. We are looking for an invisible file, so execute this command:
\bigskip
\noindent{}The $\sim$~ is shorthand for your home directory. (If you are logged in to the terminal as your username, it takes to \texttt{/home/username}. If you are logged in to the terminal as \texttt{sudo}, it takes you to the \texttt{root} directory in the top level directory—where all those Linux directories are that we talked about in the last issue.) We are looking for an invisible file, so execute this command:
\begin{verbatim}
$ ls -a
@ -226,7 +226,7 @@ alias gpush="git push origin main"
You can also execute bash scripts as well. In addition to a local backup on an external drive, I also backup the directories in my home drive to a remote storage location. To make life easy, I created a script (called, naturally, \texttt{backup.sh}) in each of those directories to back them up. To execute those backup scripts, I just need to go to that directory, open the directory in a terminal and type \texttt{./backup.sh}.
The problem here is that a lot of times, I'm not even in those directories when I'm saving files to them. I'm somewhere else. And to open the directory in my GUI and then open it in a terminal, or to open a terminal and then navigate to that directory, is a little \textit{too} much when I want to run that backup script. Remember, you want to back up soon, and you want to back up often. Backing up on that basis is a good habit to have. So let's remove as many obstacles to that habit as possible. In this case, we'll add an alias to run those backup scripts.
The problem here is that a lot of times, I'm not even in those directories when I'm saving files to them. I'm somewhere else. And to open the directory in my GUI and then open it in a terminal, or to open a terminal and then navigate to that directory, is a little \textit{too} much when I want to run that backup script. Remember, you want to back up soon, and you want to back up often. Backing up on that basis is a good habit to have. So let's remove as many obstacles to that habit as possible.\footnote{I am often amazed by how often people (myself included) want to form a new good habit (eating more fruit, getting more exercise, etc.) and then put as many things as possible in the way of that habit. Again, it's because we're so used to the old, bad habit that we don't think. Sometimes, we just need to get out of our own way.} In this case, we'll add an alias to run those backup scripts.
This is what I have in my \texttt{.bashrc} aliases:
@ -239,7 +239,7 @@ alias ktem="bash $HOME/Templates/backup.sh"
alias kvid="bash $HOME/Videos/backup.sh"
\end{verbatim}
Let's look at the first one. \texttt{kdoc} is the name of the alias. Since my first name is Ken, I prefix these with the letter \texttt{k} so I don't get them mixed up with something else. \texttt{bash} means to run this as a bash script. \texttt{\$HOME/Documents/backup.sh} means ``go to the home directory, then go to the Documents directory, and run this script called \texttt{backup.sh}''.
Let's look at the first one. \texttt{kdoc} is the name of the alias. Since my first name is Ken, I prefix these with the letter \texttt{k} so I don't get them mixed up with something else. \texttt{bash} means to run this as a bash script. \texttt{\$HOME/Documents/backup.sh} means ``go to the \texttt{home} directory, then go to the \texttt{Documents} directory, and run this script called \texttt{backup.sh}''.
We can do other things as well. Log into your webhost via ssh a lot? Try this one:
@ -247,24 +247,32 @@ We can do other things as well. Log into your webhost via ssh a lot? Try this on
alias kssh="ssh username@webhost.com"
\end{verbatim}
\noindent{}Type this, and it will ask for your password. You've now typed four characters instead of 24.
\noindent{}Replace ``\texttt{username}'' with your actual username and ``\texttt{webhost.com}'' with the actual host that you log into.
This is probably my favorite, though:
The next time you want to log into your host, just type \texttt{kssh} (or whatever you choose to call your command; the choice is yours as long as it doesn't conflict with a built-in bash command), and it will automatically ask for your password, as it has already sent your username to your host. You've now typed four characters instead of 24. Nifty, huh?
\newpage
\noindent{}This is probably my favorite, though:
\begin{verbatim}
alias kls="ls -Ahl"
\end{verbatim}
\noindent{}This gives us a directory listing, but with this flags:
\noindent{}This gives us a directory listing, but with these options:
\begin{itemize}
\itemsep-0.4em
\item \texttt{A} lists all files and directories, including invisible ones (but excluding the . and .. directories).
\item \texttt{A} lists all files and directories, including invisible ones (but excluding the . and .. directories\footnotemark).
\item \texttt{h} gives us file sizes in human readable sizes (i.e., ``4.0K'') instead of bytes.
\item \texttt{l} gives us the listing as a list, because I find that's more readable, especially with a directory that contains a lot of stuff.
\end{itemize}
And that's it. Just about anything you type often on the command line can be turned into a bash alias to save you time.
Again, I'm typing three keystrokes instead of seven. When you spend eight or more hours a day on the computers, whatever keystrokes you can save really start to add up.
\footnotetext{If you've ever wondered about what these are, here's a simple explanation. The . (dot) represents the current working directory, i.e., the one that you are in. The .. (dot dot) represents the parent directory, i.e., the directory that contains the directory you are currently in. Whenever you create a directory in a Unix-based system, it is added as a new entry to its parent directory, and these two entries (hard links) are created in the new directory. \\ \tabto{1.9em}\texttt{ls} and \texttt{ls .} are the same command: they give you the contents of the directory you are in. \texttt{ls ..} gives you the contents of the parent directory to the one you are in. It's the same as going up into your parent directory, getting a content listing, and then moving back into the child directory you were just in. And for what it's worth, you can do \texttt{ls ../..} to get the content listing of the grandparent directory. Nifty? Depends on how lost you are. \\ \tabto{1.9em}\texttt{cd ..} will move you up into your parent directory, whereas \texttt{cd .} moves you nowhere, because you are literally telling the terminal to change the directory to the current directory. It's a bit like changing into the underpants you are currently wearing. \\ \tabto{1.9em}Anyway, enough of the stupid pet tricks.}
And that's it. Just about anything you type often on the command line can be turned into a bash alias to save you time. Go for it.
\chapter{What Have I Installed?}