Under Linux, aliases are your best friends when you are in the command line. It’s such a handy feature it’d be a crime not to use it. Here I’ll give you a short overview of how you can use them, and also share a tip that will make managing them easier.
First off, here’s how to create an alias:
$ alias ls='ls --color=auto'
Now, when you type
ls, it would be treated the same as if you typed
Aliases cannot be used as command arguments, although some tools seem to support it. But you can still use backtick-quoting to execute an alias as part of command argument.
$ echo `ls`
To see the command that created some alias, you can do this:
$ alias ls alias ls='ls --color=auto'
If you call the
alias command without arguments, it will show you the defined aliases:
$ alias alias ls='ls --color=auto'
It’s important to note that the output of the above command is a list of alias commands used to set up our aliases. We will use this fact to make managing them easier (later).
To remove an alias, use the
$ unalias ls $ alias ls bash: alias: ls: not found
You can also use aliases as part of alias definition. For example:
$ alias ls='ls --color=auto' $ alias la='ls -a'
When you type
la, it will be treated as
ls --color=auto -a and not just
ls -a, because
ls in the
la alias is properly expanded to
Aliases can contain all kinds of characters, including a dot, underscore, and dash. For example:
$ alias ..a='source ../bin/activate'
You can use this fact to make aliases easier to remember. You cannot use characters like
\ because they have special meanings.
As you grow fond of aliases (as I’m sure you will), you will no doubt want to make them persistent. Once defined, an alias lasts only as long as your shell session. As soon as you log out of your shell, the alias is gone.
Usually, people place all their aliases in
~/.bashrc so that they are loaded as your shell is initialized. There are a few drawbacks using this method. First, you have to actually edit
~/.bashrc when you need to define an alias. Second, alias definitions are not ordered alphabetically. Finally, when you have multiple terminals open, you would have to source
~/.bashrc, which not only defines all new aliases, but also runs anything else that you have there.
A better way to do it is to separate aliases into a different file. First create two management aliases:
$ alias sa='alias > ~/.alias' $ alias la='source ~/.alias'
NOTE: If you are using zsh (Z shell), you must add the
% alias sa='alias -L > ~/.alias'
The first alias,
sa, saves all alias commands to a file named
~/.alias. Second one,
la, loads the saved aliases into the current session. Loading is useful when you have multiple terminals open at once, define an alias in one of them, and want to have another terminal load the new alias.
Finally, load the saved aliases in
$ echo "source ~/.alias" >> ~/.bashrc
Now instead of editing
~/.bashrc every time you need to save an alias, you can simply call
$ alias ll='ls -l' $ sa
Bonus track: some useful aliases
Preserve current environment when using sudo (thanks Rhoit for posting this in the comments):
alias sudo='sudo -E'
Show disk usage of individual directories within current directory:
alias dudir='du -khd 1 --exclude=./.*'
Get a quick glance at currently mounted devices:
alias mtab='cat /etc/mtab | grep /dev/sd'
Record a full desktop screencast with sound recording using ffmpeg (replace
wxga with your screen resolution):
alias scast='ffmpeg -f alsa -ac 2 -ab 192k -i pulse -f x11grab \ -s wxga -r 25 -i :0.0 -sameq -strict experimental'
Quickly clear the terminal:
Post your favourite aliases in the comments, and I will include them in the bonus track.