Boost Up Productivity in Bash - Tips and Tricks
Introduction
When spending most of your day around bash shell, it is not uncommon to waste time typing the same commands over and over again. This is pretty close to the definition of insanity.
Luckily, bash gives us several ways to avoid repetition and increase productivity.
Today, we will explore the tools we can leverage to optimize what I love to call “shell time”.
Aliases
Bash aliases are one of the methods to define custom or override default commands.
You can consider an alias as a “shortcut” to your desired command with options included.
Many popular Linux distributions come with a set of predefined aliases.
Let’s see the default aliases of Ubuntu 20.04, to do so simply type “alias” and press [ENTER].
By simply issuing the command “l”, behind the scenes, bash will execute “ls -CF”.
It's as simple as that.
This is definitely nice, but what if we could specify our own aliases for the most used commands?! The answer is, of course we can!
One of the commands I use extremely often is “cd ..” to change the working directory to the parent folder. I have spent so much time hitting the same keys…
One day I decided it was enough and I set up an alias!
To create a new alias type “alias ” the alias name, in my case I have chosen “..” followed by “=” and finally the command we want an alias for enclosed in single quotes.
Here is an example below.
Functions
Sometimes you will have the need to automate a complex command, perhaps accept arguments as input. Under these constraints, aliases will not be enough to accomplish your goal, but no worries. There is always a way out!
Functions give you the ability to create complex custom commands which can be called directly from the terminal like any other command.
For instance, there are two consecutive actions I do all the time, creating a folder and then cd into it. To avoid the hassle of typing “mkdir newfolder” and then “cd newfolder” i have create a bash function called “mkcd” which takes the name of the folder to be created as argument, create the folder and cd into it.
To declare a new function, we need to type the function name “mkcd ” follower by “()” and our complex command enclosed in curly brackets “{ mkdir -vp "$@" && cd "$@"; }”
.bashrc file
Aliases and functions are fantastic tools, but if every time we open a new shell we will need to set them up, it becomes a waste of time by itself.
Fortunately, the solution is just behind the corner. We have the .bashrc file.
Usually located in the user’s home directory, this file will be silently executed every time a new bash process is started.
This is the perfect place to define aliases and functions which will be automatically loaded when a new bash shell is started.
Using your favorite text editor, append at the end of the file all the aliases and functions you find helpful.
Shortcuts and default variables
Like any modern program, bash supports keyboard shortcuts. Like in any other program keyboard shortcuts are a great way to boost up productivity.
Moreover, the bash shell comes with a set of predefined variables which come quite handy.
Let’s have a look at a few of them!
Type |
Description |
|
Shortcut |
CTRL + a |
Move the cursor at the beginning of the line |
Shortcut |
CTRL + e |
Move the cursor at the end of the line |
Shortcut |
ALT + . |
Use the last word of the previous command |
Shortcut |
CTRL + l |
Clear the screen |
Shortcut |
CTRL + r |
Search thought the commands’ history |
Default Variable |
$HOME |
Translates to the user’s home directory |
Default Variable |
$HOSTNAME |
Translates to the computer hostname |
Default Variable |
$BASHPID |
Translates to the process ID of the current bash shell |
The above table show a few examples, if this cached your mind and want to know more you can look at:
- “env” command - Will print all defined environment variables
- “declare” command - Will print all defined functions (including the built in)
- “set” command - Will print all defined and available variables and functions
- The bash manual - It contains all details about the bash builtins
Tricks
I want to conclude this article by sharing with you a list of my favorite aliases and functions.
Feel free to cherry pick your preferred ones and add then to your own .bashrc!
Aliases
...
alias ..='cd ..'
Use .. to move to the parent folder
chx
alias chx='chmod +x'
Use “chx filename” to grant executable rights to a file called filename
ddu
alias ddu='du -sh * | sort -hr | head -20'
Use “ddu” to get a list of the top 20 files/folders sizes
Functions
ftext
ftext () { grep -iIHrn --color=always "$1" . | less -R -r }
Use “ftext text” to get a list of files in the current folder and subfolders which contains the word “text”, the line number, and the line contact inside “less”.
For example I have used “ftext kernel” inside /etc/sysctl.d folder.
duplicatefind
duplicatefind () { find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate }
Use “duplicatefind” to create a list of duplicated files. Note that files are considered duplicated based on their content, not their name.
Example
generateqr
generateqr () { printf "$@" | curl -F-=\<- qrenco.de }
Use “generateqr string” to generate a QR code for the word string.
It is very handy sometimes to have the ability of quickly sharing text and link between your terminal and your phone.
Example