Added section on bash aliases

This commit is contained in:
Kenneth John Odle 2021-11-05 12:24:22 -04:00
parent a22d7a084e
commit e0c2199aea

View File

@ -1,7 +1,6 @@
\documentclass[twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
%\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{makeidx}
%\makeindex
@ -63,6 +62,12 @@
% Use strikethrough
\usepackage{ulem}
% Style a blockquote
% See https://tex.stackexchange.com/questions/325695/how-to-style-blockquote
\usepackage{etoolbox}
\usepackage{setspace} % for \onehalfspacing and \singlespacing macros
% See also https://www.overleaf.com/learn/latex/Typesetting_quotations
% Make things neater. Thanks /u/-LeopardShark-
%\usepackage{microtype}
@ -153,9 +158,123 @@ And let's face it: the menu at McDonald's has not really changed in years. Yes,
Of course, if you are thinking about outcomes, chances are you don't eat fast food very often anyway, because the long term outcomes are obesity, heart disease, and hypertension. But damn, those fries are good!
\chapter{What's to Like About Linux?}
\section{A GUI Solution}
For what it's worth, there is a GUI for \texttt{pdftk}. It's called PDF Chain and you can find it at \href{https://pdfchain.sourceforge.io/}{\texttt{https://pdfchain.sourceforge.io/}}.
Despite all my prattling on about the many advantages the command line has for your brain, I'm not opposed to using a GUI, actually. (I mean, I have Ubuntu installed on two machines and Kubuntu on a third.) A GUI does make life easier in many ways, and what I like about one in a case like this is that if you're someone who has to manipulate pdf files rarely or only once, it's probably easier to just use a GUI than it is to learn the command line. Efficiency plays a role here, as well. If I'm going to use this all the time, it's definitely more efficient for me to learn the command line approach. But once or twice a year? Or only once? A GUI is much more efficient.
\begin{center}
\includegraphics[scale=0.4]{pdfchain_-_title}
\end{center}
\chapter{Make Life Easier with bash Aliases}
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.
Chances are, you will open the terminal in the root of your \texttt{Home} directory. You can tell by typing
\begin{verbatim}
$ 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:
\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:
\begin{verbatim}
$ cd ~
\end{verbatim}
\noindent{}The \verb|~| is shorthand for your home directory. We are looking for an invisible file, so execute this command:
\begin{verbatim}
$ ls -a
\end{verbatim}
\noindent{}The \texttt{ls} command will list visible files, the \texttt{-a} option shows all files, including the invisible ones.
We are looking for a file called \texttt{.bashrc}. If you have a lot of stuff installed, you may have to scroll around to find it. Once you find it, open it in a text editor. This is in your home directory, so you can use whatever text editor you want and no \texttt{sudo} privileges should be required.
If you want, scroll through it. There is some interesting stuff there. But we want to create aliases, so go to the end, and type
\begin{verbatim}
# my aliases
\end{verbatim}
\noindent{}The \verb=#= means that this line is a comment. We'll add our aliases beneath this line.\footnote{Let's stop and reflect for a moment on how important it is to add comments to whatever we work on. When you come back to this file in six months or two years and ask yourself ``Why is this here? Who wrote this?'' your comment will tell you. I often find it handy to include the date as well, and any url where I found something useful. Check the source code of the preamble to this document for examples.}
Let's use our Github example. Add the following line:
\begin{verbatim}
alias gpush="git push origin main"
\end{verbatim}
\noindent{}Save the file, and then go to one of your git repos, make some changes, and commit them. Then, when you want to push the changes to the remote repository, instead of typing \texttt{git push origin main} just type \texttt{gpush}. Your terminal will do the rest for you.
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.
This is what I have in my \texttt{.bashrc} aliases:
\begin{verbatim}
alias kdoc="bash $HOME/Documents/backup.sh"
alias kdow="bash $HOME/Downloads/backup.sh"
alias kpic="bash $HOME/Pictures/backup.sh"
alias krec="bash $HOME/Recordings/backup.sh"
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}''.
We can do other things as well. Log into your webhost via ssh a lot? Try this one:
\begin{verbatim}
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.
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:
\begin{itemize}
\itemsep-0.4em
\item \texttt{A} lists all files and directories, including invisible ones (but excluding the . and .. directories).
\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.
\chapter{What Have I Installed?}
\chapter{What's to Like About Linux?}
I have an app on my phone called ``The Stoic'' that shows quotations from various Stoic philosophers. As I was working on this issue, this popped up:
\AtBeginEnvironment{quote}{\singlespacing\small}
\begin{quote}
Because a thing seems difficult for you, do not think it impossible for anyone to accomplish.
\begin{flushright}
---\textbf{Marcus Aurelius}
\end{flushright}
\end{quote}
\end{document}