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.
These are some useful bindings to use Bash without lifting your hands from the keyboards, and hopefully without even reaching for the arrow keys.
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:
Finally, something that I always forget is that !!
repeats the last command used.
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
locate
? This happens because the file database gets updated once per day (usually), but it can be manually done using updatedb
.find
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
.
-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.
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
.
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!