Save Time with these 15 Hacks for SysAdmins

Here is something I’ve learned as a Linux administrator: when I go into work, everything is boring or on fire. There is no comfortable middle ground. I mean, I’ll either sit around tuning configs and running tests for eight hours, or suddenly it’s 10pm and please stop yelling at me. The last sentence is only a bit hyperbolic.
Regardless of whatever day I have, time is precious: I have a job to do that I want to finish as best I can. I’ve used the Bash and Z shells on *nix systems since 2000 to admin sites and services. While I’ve tempted to say I’m some sort of ninja shell hacker, the truth of it is, I’ve just used it a lot. By this point in time I’ve probably clocked up a contiguous year of my life spent staring at a blinking green cursor in a terminal. Reap ye the benefits of my labour: here are 15 tips that will hopefully save you time (and maybe the bacon too!).
1. Plan Ahead
A failure to plan is a plan for failure. Every administrator will someday have that dreaded 3am phone call: “the site is down.” What do you do? Who do you call? Do you have a fallback plan? Are there backups? How do you find the point of failure? Can you say, if the worst comes to pass, “our data is safe and we can recover it all.”
Sit down with your colleagues, client or boss and talk through scenarios. Wargame failures and use what you learn to lay out a strategy for the time when the worst happens.
Nothing else will save you as much time as always having a plan.
2. Master the Crontab
Cron is a scheduling utility that is integral to the function of all *nix distributions. Cron tasks are single commands that run on the set schedule. Cron tasks can execute at:
- …any given time every day.
- …day of the week, month or year.
- …any other time whatsoever, as often as needed.
Cron grants great flexibility: a cron task can, for example, run on the ninth day of every month at 1:57pm. What follows is an example of a typical cron task that runs at 12:01am every night. It first copies and gzips the Apache access.log file, then empties it.
1 0 * * * log="/var/log/apache2/access.log"; tar -czf ~/log-$(date --date "-1 day" +%Y-%m-%d).tar.gz $log && > $log
Type
crontab -e
to launch an interactive prompt that help you add entries to cron schedule.
3. Ctrl + Keybind Bindings
The humble Linux shell has a history that reaches back to the 1970’s, before desktop computers even came supplied with a mouse. Because of this legacy, you can use the shell with a keyboard alone. There is a rich variety of keyboard shortcuts that a power user can exploit to save a great deal of time. Some examples:
-
Ctrl + e:jump to the end of the line.
-
Ctrl + a:jump to the start of the line.
-
Ctrl + r:search the shell’s history.
-
Ctrl + l:clear the screen.
There are almost 40 total keyboard shortcuts you can use to save time every day!
4. Alias Commands
Every shell command has arguments, and some arguments and commands you will use over and over time and again. Why not save these so the command itself can run them for you? The ofalias command creates a shortcut to a command that executes when the supplied arguments.
Set up an alias named
lsdu
that will output sizes all files in the folder:
$ alias lsdu='ls -a1 | xargs -n1 du -hs'
$ lsdu
191M .
191M ..
36K .bash_history
4.0K .bash_logout
4.0K .bashrc
...
You can save aliases to your.bash_profileor.bashrcprofile.
5. Skip | Loops
One of the most common administrative tasks is to edit a list of files. The ordinary slow way is to iterate through this list one item at a time. With some foresight, you can either skip iteration or split a task into several parallel threads.
This example uses
find -exec
argument to delete junk files added by file managers:
find . -type f -name '.DS_Store' -o -name 'Thumbs.db' -exec rm {} \;
The GNU parallel utility will split input into the given number of threads. This Imagemagick script to generate cropped thumbnails will use employ eight parallel threads:
find . -iname '*.thumbnail.jpg' | parallel --pipe -j 8 mogrify -crop 150x150 -gravity center {}
6. Save Sessions with Screen
screen is a powerful, subtle utility that allows you to separate, store and recover shell sessions. screen will save your work folder and history buffer into a named session. You can store and exit this session to return to at a later time.
This example starts a screen session named ‘foobar’ and execute several commands:
$ screen -S foobar
$ ls .
foo.txt bar.txt
Now save and resume it:
[detached from 12379.foobar]
$
$ screen -R foobar
$ ls .
foo.txt bar.txt
screen is a useful tool for diagnostics and debugging. A typical use case would be to open a remote server via ssh, and open the top command inside screen. In this manner you can page between local and remote work from the one terminal.
7. [Customize your Prompt @ localhost] $_
The Bash prompt is customizable through the PS1 variable. You can change colours and add status messages through the PROMPT_COMMAND variable. Output can be piped into the prompt. You can, for example, change PS1 to red if the last command exits with a code other than 0:
export PROMPT_COMMAND=change_on_err
change_prompt_on_err() {
red="\[\e[0;31m\]"
white="\[\e[0;m\]"
if [ $? -ne 0 ]; then
colour=$red
else
colour=$white
fi
PS1="${color} $ \e[m "
}
The PROMPT_COMMAND variable represents a function which runs each time a terminal command executes. In this way you can change the shell based on any criteria.
8. {Brace, Expansion}
Save yourself time when creating an empty folder tree by using brace expansion to populate it. Curly braces contains a comma-separated list of words. The shell matches each word in turn to the strings on either side of the brace:
$ mkdir stri{f,k,d}e
$ ls .
stride strife strike
$ mkdir -p strife/{foo,bar,baz,fizz}/
$ ls strife
foo bar baz fizz
9. !! History Substitution
The Bash shell is full of special characters like
!, ?, @
and
~
A less well-known subset set of characters links to the history command and permits you to reuse old commands and arguments:
-
!<number>will execute the history item <number>
-
!!: re-execute the last command.
-
!$will copy reuse the arguments (
[email protected]) of the previous command.
For example:
$ echo "The quick brown fox jumped over the lazy dog."
The quick brown fox jumped over the lazy dog."
echo !$ | wc -c
echo "The quick brown fox jumped over the lazy dog" | wc -c
45
Have you ever forgotten to preface a command with sudo? Run sudo !!
10. Navigate Directories Faster
History isn’t the only command with substitution shortcuts: cd also has pithy aliases for navigating the filesystem. While administrator will already know
~.
and
..
they may not know
~-
which jump between the last two directories. As with the other operands, you can combine this. The example here will move you to the parent of the previous directory.
$ cd ~-/../
11. [ Inline ] && Evaluation
As well bypassing iterative loops, you can also perform inline boolean evaluations using the
test
and
[
commands:
$ cd ~
$ [ $? -ne 0 ] && echo "Last command completed"
Last command completed
This enables powerful inline operations without having to resort to a full script.
12. Search Man Pages
The man command contains an almost encyclopaedic amount of information on every Bash command. The man page for mplayer, a multi-codec video player and transcoder, runs to more than eight thousand lines of instruction. This is as long as some novels! The man page for the GNU c compiler, gcc, is even longer, clocking almost thirteen thousand lines of instructions.
This depth of help can be intimidating. To make it easier, the
man
command has several helpful operations to make finding the correct command easier.
man -K
<command> will launch an interactive prompt which will offer potential matches.
13. s/Regular/Expressions/g
Even the most novice of Linux users has used regular expressions in their commands: the * wildcard. Most will never venture deeper than this. The shell has excellent regular expression support through different commands; take, for example, a task to batch rename files from foo to bar:
for file in ./*_foo.jpg; do
mv $file $(echo $file | sed -e ‘s/_foo/_bar/’)
done
The powerful rename command can accomplish the same with built-in regex support:
rename ‘s/_foo/_bar/’ *_foo.jpg
The
[
command also supports regex matching:
foo=‘The quick brown fox jumped over the lazy dog’
if [ $foo =~ (^The quick|dog$) ]]; then
# will match true
fi
14. Process <(Substitution)
All shell processes communicate in a plain text stream:
1.
STDIN
governs input.
2.
STDOUT
governs output.
3.
STDERR
handles errors.
Although you can build chains and scripts from pipelines joined by the | character, a pipeline is isn’t always the best answer to a problem. As an alternative, process substitution allows you to generate a list in situ, without the use of a subshell:
$ cat <(date --date "-1 day") <(date --date "-2 days") <(date --date "-3 days")
Sun Jun 14 08:00:18 UTC 2015
Sat Jun 13 08:00:18 UTC 2015
Fri Jun 12 08:00:18 UTC 2015
Process substitution allows you to use the output of one command as input for a command that expects a file. The other advantage of process substitution is that multiple streams of input can be concatenated without iteration.
15. Job Control &
Why wait for a task to complete before you move onto the next one?
$ ping google.com > ping.txt &
[2] 27731
$ wc -l ping.txt
5 ping.txt
$ wc -l ping.txt
12 ping.txt
$ fg
ping google.com > ping.txt
^C $
The ampersand character (&) forks a task into the background. The forked task will continue to run in the background so long as this shell is open. The more powerful nohup command will detach a process from the shell and run it as a background daemon. The process will continue to run until terminated.