12 useful bash aliases that you can make you more productive in linux

Aliases is one of the most useful features of Linux command shells. You probably end up using the same commands over and over on a daily basis. This is true if you use the command line for almost everything. You can use bash aliases (if you use bash) or shell aliases to make these every day tasks easier to execute.

Most of the commonly used commands are usually short and easy to remember (cd, ls, cp, mv etc). If you have specific long and complex commands, then you want to use aliases to reduce the need to type these long commands and to minimize typing errors.

I usually prefer to create aliases only if it something that I forget easily or it is really long. Otherwise, I prefer to type it out most times because it helps me to remember/learn command formats and options (in the long run), and also change such options on the fly. Also, if and when you happen to work at another machine…none of these will be available and you will have to remember proper commands.

I consider aliases to be both a good thing as well a bad thing. In many ways, it makes it easier and efficient to execute many tasks. But it also makes me complacent and I end up not remembering the actual command and options anymore. A general set of rules to abide by (at least I do) when creating the aliases:

  • There should be some substantial shortening in terms of typing. Converting a four letter command to two letters is hardly worth it. eg. No need to create an alias for ls as l.
  • You should not create a separate alias for every possible command line option. Yeah…I have seen it done.
  • Do not just rename commands just because you can. Again, do not rename ls to l.
  • These should be commands that you (semi) regularly use, not something that you use very rarely. You will forget that you created an alias exist anyway.
  • Beware of one letter aliases, you can accidentally set it off. Using something like r for shutdown is a bad idea.
  • Preserve the name of the command if possible. you are just adding options that you might forget. eg. cp -iv can be aliased to cp (and not cpv or c).
  • Having said that, change or modify the command name slightly, if you also want to execute the original command with out the specified options occasionally.
  • If it is not an one liner, try a shell script rather than an alias.

Here is a list of generic aliases that I have…..which you might find useful. These are for the bash shell, but you should be able to translate them easily to the specific shell that you use.

alias lh='ls -lisAd .[^.]*'

This will print out just the hidden files in the current working directory.

alias la='ls -lisA'

This will print out the directory in a detailed format. It will also print out hidden files and folder as well.

alias rm='rm -iv'

Do you routinely delete files from command line and use regular expressions to do so? This acts as the last level of defense before you accidentally delete that file you did not want to. You use it just the way you use the rm command.

Yep, it can be a little tiresome when there are many files and you are really sure of what you are doing. You can override this by specifying the -f option at the command line with rm. You can also use a less intrusive version, the -I command line option.

alias rm='rm -Iv'

You can add similar safety options to other file manipulation commands as well, such as cp, mv etc. I am not going to elicit each one of them here, but cp will look like this.

alias cp='cp -iv'

The same goes for moving files as well. Do not accidentally overwrite the file that you did not want to.

alias rms='shred -uz'

Why remove when you can shred? Use this alias to securely delete files when you want to. rms is close enough to rm, and use rms as your delete command. It goes without saying that you should have shred installed on your machine for this to work.

alias mkdir='mkdir -pv'

This will automatically create parent directories if they don’t exist. You can now type in sub-directories without checking if it exist.

alias hs='history|grep -i '

A case insensitive search for your command line history for a command you executed in the past. Most modern shells already provide some kind of functionality to search and recall past commands, so this might not be needed in your case. Try Ctrl+R for example, which works just as well.

Do you use any programming tools that takes a long time to run, eats your memory and makes everything else sluggish? I use maven and emerge tool (for Gentoo Linux) quite often in the background compiling tons of stuff. This also means I routinely forget to do it “nicely“…..and then have to try to renice it (or kill it) as the machine gets progressively sluggish.

alias mvn='nice -5 mvn'
alias emerge='nice -10 emerge'

Adjust the niceness of the program using an alias so that you don’t have to remember it every time. This should work with any other resource intensive process that you work with.

alias rd='rdesktop -z -g 1600x1024 -d mydev -u tom'

Usage: $ rd remotemachine

Do you have to connect to other remote machines very often? Here is a quick alias that will make sure that it always open the same size window and will use the username…etc etc. You can create a similar alias for vnc if you use that.

alias tgz='tar -xvfz'

Do you create or extract a lot of tar files? The above one will extract tar files. You can create aliases for different compression algorithms and you can create another alias to create a archive file as well.

alias rs='rsync -avz --progress --delete-after'

Do you copy or transfer files between machines often? A commonly used set of options for rsync mapped to rs. Feel free to modify with your own set of options.

alias ff='find . -type f -iname'

Do you search for files by file names very often? The above alias will search the current directory for the specified filename or expression. You can create a similar alias for searching directory names by using -type d.

One way to explicitly bypass the alias,…that one time you have to….is to use a backslash (\) in front of the command. So, if you execute something like this…

$ \rm -r ~/*

…this will not execute the alias (with the -i safety), but will execute the command found in the path.

Did I miss any commonly used generic aliases?