Tech Couch

A Comfy Corner for Software, Security, Cloud, and AI

Why bash shell aliases deserve your attention

Table of contents


Often overlooked as a simple customization feature, the alias command of bash shells is a powerful tool to increase your productivity, enable backward-compatibility and save customized default behaviour for commands. Bash aliases can save you significant time on recurring tasks and streamline your environment - let's see how!

Creating, listing and removing aliases

At it's core, an alias is just a named shortcut for a command. Creating an alias is a simple one-liner:

alias greet='echo "Hello!"'

You now have a command called greet that will execute the echo "Hello!" command when it is run. If you want to edit this alias, simply use this command again - creating an alias with the name of an already existing one will simply replace the old one.

You can get a list of all currently available aliases with the -p flag:

alias -p

This will print all known aliases and the commands they will execute, one per line.

If you want to remove an alias, use unalias:

unalias greet

Now the greet command is gone.

Making an alias persistent

By default, your alias is temporary: it exists only in the terminal you created it in and vanishes when you close it. This may be good if you want to temporarily shorten a command, but often you will want an alias to be permanent.

To make an alias persistent, you will need to execute it before the bash session starts. Bash brings two default files to do that:

  • ~/.bashrc: This file will only be executed for the owner of the home directory it is in. Use this to create an alias exclusive to a specific user.
  • /etc/bash.bashrc: This file is executed for all users on the machine. If you want to create a global alias for all users, put it in here.

Actually creating the alias is done by simple appending the alias command you want to the end of one of these files.

Changing default behaviour of commands with aliases

Most commands will include flags to alter their behaviour. The ls command for example, simply outputs the names of the directory contents by default. If you wanted to include hidden files (names starting with . ), human-readable file sizes and color the output on supported terminals, you can pass a few flags to the command:

ls -lAh --color=auto

This is cumbersome to type every time in place of ls, so it is a perfect candidate for a bash alias:

alias ls='ls -lAh --color=auto'

Now ls will always run with your newly set default settings without you having to type them every time.

Another common use case of this approach is to enable safeguarding features, for example adding the -i flag to cp, mv and rm commands to prompt for confirmation before the command would override or remove an existing file:

alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'

Creating compound alias commands

Aliases can be used to create entirely new commands out of one or more old ones. Especially users that have been on linux systems for a while may remember the commands egrep and fgrep, that were deprecatd in 2007 and replaced by flags of the grep command. Getting those words out of muscle memory can be a long process, so many users have opted to alias the commands back into existence:

alias egrep='grep -e'
alias fgrep='grep -f'

Now they can continue to use the old syntax, but call the newer command version under the hood.

Even creating entirely new commands are possible, for example to list all images in a directory:

alias findimg='find . -regextype sed -regex ".*\.\(png\|jpg\|gif\|webp\|svg\|bmp\)"'

Or to quickly list the 10 most recently accessed files:

alias latest='ls -ltu | head -n 10'

Aliases will pass their command line arguments to their underlying command, so you can even create aliases taking arguments:

alias findext='find . -name "*.$1"'

This will find files having the given file extension, for example text files using findext txt.


Knowing how to use aliases and how much time and effort they can save you, it is your turn now to find long commands you frequently use and shorten them down to simple names. Have a look at your bash history!

More articles

Automating code quality in go

Exploring what code quality is, tools to maintain it in go and how to automate them

Managing packages with apt

How to find, install, update and remove packages - and everything inbetween

Enabling automatic background updates with unattended-upgrades

Keep your debian-based servers up to date automatically

Checking disk health in Linux

Identify bad drives before they fail

Handling file downloads with PHP

Meeting user expectations with every download

Streamlined error handling in PHP

Handling all kinds of errors with a single function