The Linux Philosophy for SysAdmins, Tenet 06 — Be the Lazy SysAdmin
Image by: VM (Vicky) Brasseur. CC BY-SA 4.0
Author’s note: This article is excerpted in part from chapter 8 of my book, The Linux Philosophy for SysAdmins, with some changes.
Despite everything we were told by our parents, teachers, bosses, well-meaning authority figures, and hundreds of quotes about hard work that I found with an on-line search, getting your work done well and on time is not the same as working hard. One does not necessarily imply the other.
I am a lazy SysAdmin. I am also a very productive SysAdmin. Those two seemingly contradictory statements are not mutually exclusive, rather they are complementary in a very positive way. This article is about working hard at the right tasks to optimize our own efficiency and finding a few of the myriad ways to use the short cuts already built into Linux.
True Productivity
Typing away at the keyboard all day long every day to perform the tasks required by the job is probably the least productive that any SysAdmin can be. A SysAdmin is most productive when thinking – thinking about how to solve existing problems and about how to avoid future problems; thinking about how to monitor Linux computers in order to find clues that anticipate and foreshadow those future problems; thinking about how to make their work more efficient; thinking about how to automate all of those tasks that need to be performed whether every day or once a year.
This contemplative aspect of the SysAdmin job is not well known or understood by those who are not SysAdmins – including many of those who manage the SysAdmins, the Pointy Haired Bosses (PHBs). Many PHBs, being completely ignorant of how to measure SysAdmin productivity – or anyone else’s productivity for that matter – like to hear those keys being struck. Lots of keyboard noise is music to their ears. That is the worst possible measurement of a SysAdmin’s productivity.
Some PHBs go so far as to install keystroke and mouse movement monitoring software on their employees’ computers as a measurement of how productive they are. Google it and see for yourself the large number of programs that perform this type of keystroke counting. The more keystrokes and mouse clicks, the more productive the user must be, right? Nope! Perhaps that’s exciting stuff for bean-counters but it’s a horrible way to measure the productivity of a SysAdmin – or anyone else for that matter.
Minimize typing
I am a horrible typist. I went to high school at a time when boys did not take typing. That was for the women who were going to be secretaries. When I finally did start using a computer through a real keyboard instead of punched cards, I managed to teach myself enough to type at a fair speed with a couple fingers on each hand. It works for me but I have to make a lot of corrections. And making errors when typing a command to a CLI program is a bad thing. So reducing the amount of typing that needs to be done is important.
Aliases is a tool for substituting a long command for a shorter one that is easier to type because it has fewer characters. They’re a common way to reduce typing by making it unnecessary to type in long options that we use constantly by including them in the alias.
Note: Perform all commands in this article as a non-root user unless otherwise stated.
Enter the alias command to view the current list of aliases.
$ alias
<SNIP<
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mc='. /usr/libexec/mc/mc-wrapper.sh'
alias vi='vim'
alias vim='vim -c "colorscheme desert" '
<SNIP>
Your results should look similar to mine but I have added some additional aliases. One is for vim to use the “desert” color scheme.
I particularly like the ll
alias because I like the long listing of directory contents and instead of typing ls -l
, I can just type ll
. I use the ll
command a lot and it saves typing three characters every time I use it. For slow typists like me that can amount to a lot of time.
I strongly recommend that you do not use aliases to alias Linux commands to those you used in another operating system like some people have done. You will never learn Linux that way.
You can use the alias command to add your own new aliases to the ~/.bashrc file to make them permanent between reboots and logout/in. To make the aliases available to all users on a host, add them to the /etc/bashrc file. The syntax in either case is the same as from the command line.
File naming
In general, short names are good, but meaningful names that are easy to see in a list are even better. My naming strategy for files that have similar names but that were created on different dates is in the form YYYYMMDD-filename.pdf for example. I download many financial files from the Internet and they have names like statement.pdf and when downloaded into a directory I rename them with my own format so they are more easily discernible in a directory, such as 20170617-CC-statement.pdf. Placing the date first in YYYYMMDD or YYYY-MM-DD format makes them automatically sort in the correct date sequence in directory listings which makes it easy to find a particular file.
This type of naming does require some additional typing up front but it can save a lot of time looking for specific files later.
BASH Efficiency
Bash is only one of many shells available for Linux. Like all shells, Bash has many ways to help you become more efficient. We have already seen the aliases that can be configured in the .bashrc file. Let’s look at some command line features provided by the Bash shell.
Completion facility
Bash provides a facility for completing partially typed program and host names, file names, and directory names. Type the partial command or a file name as an argument to a command and press the Tab key. If the host, file, directory, or program exists and the remainder of the name is unique, Bash will complete entry of the name. Because the Tab key is used to initiate the completion, this feature is sometimes referred to as “Tab completion.”
Tab completion is programmable and can be configured to meet many different needs. However unless you have specific needs that are not met by the standard configurations provided by Linux, the Core Utilities, and other CLI applications, there should never be a reason to change the defaults.
Note: The Bash man page has a detailed and mostly unintelligible explanation of “programmable completion.” The book, “Beginning the Linux Command Line” has a short and more readable description1 and Wikipedia has more information, examples, and an animated GIF to aid in understanding this feature.
Your home directory should have a subdirectory named Documents for this experiment. Most Linux distributions create a Documents subdirectory for each user.
We can use completion to change into the ~/Documents directory. Be sure that your home directory is the PWD. Type the following partial command into the terminal.
$ cd D<Tab>
<Tab> means to press the Tab key once. Nothing happens because there are three directories that start with “D”. You can see that by pressing the Tab key twice in rapid succession which lists all of the directories that match what you have already typed.
$ cd D<tab><Tab>
Desktop/ Documents/ Downloads/
Now add the “o” to the command and press Tab twice more.
$ cd Do<tab><Tab>
Documents/ Downloads/
You should see a list of both directories that start with “Do”. Now add the “c” to the command and press the Tab key once the rest of the directory name is completed in the command.
$ cd Doc<Tab>
cd Documents/
The completion facility only completes the command, directory, or file name when the remaining text string needed is unequivocally unique.
Tab completion works for commands, some subcommands, file names, and directory names. I find that completion is most useful for completing directory and file names, which tend to be longer, and a few of the longer commands and some sub-commands.
Most Linux commands are so short already that using the completion facility can actually be less efficient than typing the command. The short Linux command names is quite in keeping with being a lazy SysAdmin. So it just depends on whether you find it more efficient or consistent for you to use completion on short commands. Once you learn which commands are worthwhile for tab completion and how much you need to type you can use those that you find helpful.
Command line recall and editing
Command line recall and editing is another method to reduce the total amount of typing we do. These two features, command line recall and command line editing, work together to enhance productivity. I use these features frequently and cannot imagine using a shell that does not have them. These features would not be possible without the Bash history feature so we will start there.
History
Command line recall uses the Bash history feature to maintain a list of previously entered shell commands. This feature allows us to use the command history to recall previous commands for re-use. Prior to pressing the Enter key the recalled commands may be edited. Let’s start by looking at the history for our hosts so we can see what that looks like.
Enter the history command in your terminal and look at the results.
$ history
1 poweroff
2 w
3 who
<SNIP>
26 history
Your results will be different from mine, but you should see at least some of the commands that you entered for previous experiments.
The Bash command history is maintained in the ~/.bash_history file. Other shells keep their histories in different files. The Korn shell stores its history in .sh_history, for example. For Bash, at least, the history in the buffer is not written to the .bash_history file until you exit from the shell.
Each open terminal has its own history so you may not see the command you want in the listing. If you do not, try another terminal session. The shell histories are maintained for a specified number of lines, the Fedora default being 1,000.
Using the history
Now lets discover how we can use this history. There are two ways to access the contents of the history in order to reuse them. We can use the line number or we can use scroll back.
First clear the existing history, then run a couple commands to add some new data to the history file and look at it again. By clearing the history file you should have the same entries and results as I do in this experiment.
$ history -c
$ history
1 history
$ echo "This is a way to create a new file using the echo command and redirection. It can also be used to append text to a file" >> newfile1.tx
Note that I made this command a little long on purpose. Now look at the result. Just type the first part of the file name and press Tab to do the completion.
$ cat new<Tab>file1.txt
This is a way to create a new file using the echo command and redirection. It can also be used to append text to a file
Now press the Up arrow (↑) key once. You should see the command you just entered. Press the Up arrow key one more time to see the previous command. You should now be looking at the echo command. Press the Enter key to repeat this command and then look at the result using the Up arrow key to return to the cat command.
In addition to using the arrow keys to scroll through the Bash history, we can simply use the number of the entry we want to re-use. Let’s add another line to the existing file using the command on line 4 of the history file.
$ !4
echo "This is a way to create a new file using the echo command and redirection. It can also be used to append text to a file" >> newfile1.txt
$
Notice that the line number is preceded by the bang (exclamation point) which reruns the command from line 4 in the history. After you press the Enter key, Bash also displays the command that it is being executed. After you press the Enter key, though, there is no way to take it back.
Success as a lazy SysAdmin
By now you have figured out that this article is not really about being lazy in the usual sense of the term. The successful lazy SysAdmin is not lazy – just efficient. Using the tools available efficiently pays dividends in the long run.
The strategies I have discussed here are not the only ones that can be used to enhance our own efficiency. I am certain that you already have found many of your own ways to work smarter.
There are many tools and shortcuts that the lazy SysAdmin can use to work smarter. We’ve only covered a few here, mostly related to the command line. If you have some favorite tools and strategies to work smarter and more efficiently, send us an email to open@both.org, and we’ll collect them into another article. Or we would welcome your submissions of your own article on this subject, also to open@both.org.