#2 - Some Linux tips

I was glancing the book The Linux Command Line by William Shotts hoping to find some new tricks, and god I did find them! In honor to that, I am writing this blog post to share some of my findings, hopefully it will be useful for someone.

Keyboard navigation

These are some useful bindings to use Bash without lifting your hands from the keyboards, and hopefully without even reaching for the arrow keys.

Ctrl-a, Ctrl-e
Move cursor to the beginning/end of the line.
Ctrl-f, Ctrl-b
Move cursor forward/backward one position. Use Alt instead to move using one word.
Ctrl-d
Delete character at current position.
Ctrl-u, Ctrl-k
Kill text to the beginning/end of the line. Note: killed text is then yanked (copied) with Ctrl-y.

Prediction and history

It is fundamental to use autocomplete together with history search. Hopefully, we all know that Tab helps autocomplete, and thath Tab Tab shows all the possible ways to complete the current word. Of course, it is not always obvious that the command we are using somehow supports autocomplete. For this it can be useful to use Alt-?, which explicitly shows all of the autocomplete options.

History can be traversed using what's known as incremental search, initiated with Ctrl-r. From there you can use:

Ctrl-j
Paste current command to terminal.
Ctrl-o
Execute current command, and instead of leaving, move to the next one used. This can be fundamental when repeating a sequence of commands.

Finally, something that I always forget is that !! repeats the last command used.

Text and file manipulation

Here there are many details that get you running, where I will assume that you are rather comfortable using sed and grep with pipes (|). One small note is that I was so used to using grep with pipes (ls | grep foo) that I found out too late about its recursive text search (grep -r "string" directory) that allows you to search through all files in a folder.

locate
Ever struggled to find a file you know to exist using locate? This happens because the file database gets updated once per day (usually), but it can be manually done using updatedb.
find
I have started using find fairly recently, as you can usually handle without it (until you learn how good it is!). The arguments it accepts are quite arbitrary, are include: -type ([d]irectory, [f]ile, symbolic [l]ink), -name (supports wild card *), -size (+1M for files larger than 1 Mb, -1M for files smaller than 1M), -and, -or, -not.

After you find the files, you can append standard actions such as -delete, -ls, -print as well as arbitrary code with -exec. This can be done using {}, which represents the current pathname. With it, a complicated way of printing the names of all files larger than 1G would be:

$ find . -size +1G -exec ls '{}' ';'

Note that the '' are required to avoid issues with spaces or with bash substitutions.
Standard output and error
You might know that to redirect standard error to standard output, you must append the directive 2&>1. Well, this is actually and old style solution! Recent versions of bash support simple replacing > with &>. Much cleaner! This will change something ugly such as ./long_run.sh > $OUT 2&>1 into ./long_run.sh &> $OUT.

Process monitor

The last point regards monitoring your system processes. I have found top to be too raw, so I usually use instead htop (apt install htop in Debian based systems), and there is also a nice GPU variant nvtop. In addition, the raw output of all processes ps -aux can be much better appreciated with the native tool pstree! I'm quite sad I never heard of it before.



Hopefully at least one of the things here will be new for you to make your Bash life easier. Best!