How to save keystrokes at the command line with alias

Save some hassle by setting up aliases for the commands you often use.
486 readers like this.
An introduction to GNU Screen

The alias command-line tool is both useful and relatively simple. Its purpose is to simplify a single-line command by creating a custom name for it. There is a good chance that you already have some aliases even if you've never used the tool. In Bash, aliases can be created with a simple text editor and are stored in your $HOME/.bashrc file. If you want to see what aliases have been set up, take look at that file, or simply type alias on a command line and press Enter/Return.

Here are some simple aliases, several of which were already in the file when I installed Fedora:

alias cp='cp -v'
alias rm='rm -v'
alias mv='mv -v'
alias ls='ls --color'
alias ll='ls -l --color'
alias lh='ll -h'

The first three aliases above ensure that whenever I use cp, rm, or mv I get verbose output. The others slightly modify the ls command's output.

All six also show you how to write an alias: First is the alias command, followed by the name you give the alias, then the equal sign, and finally the complete command between single quotes. You can use an existing alias name or create a unique one.

These are obviously simple, but let's look at some more complex aliases I have set up.

Opening Emacs files

alias emacs='emacs %f --no-splash --maximize -fg "white" -bg "dark slate gray" -cr "yellow"'

I use Emacs quite a bit, and the command above ensures its size and appearance are the way I like them. Notice the %f, which is a placeholder for the filename I type after the emacs command. If I want to create a new file in Emacs, I just type a filename that doesn't exist.

Using a temperature-conversion script

alias temp='/home/gregp/perl/'

Here is the alias I created to use a temperature-conversion Perl script I wrote about last year. It's not shown, but you have to follow temp with a number for the script to work.

Keeping track of people

alias census2file='pg_dump -vt census gregp > census.out'

alias fromfile='psql -e gregp < census.out'

When I was still practicing medicine, I used to keep track of my hospital patient list and individual visits with PostgreSQL, so I could slice and dice the information as I wanted. I used both a laptop and desktop and to keep these up to date on both systems, I made the two aliases above.

With the first alias, I could dump the contents of the table "census" in one system, then use the second alias to import it with psql on the other system. I also used these whenever I upgraded Fedora to a newer version.

Running multiple commands together

Sometimes you may want to use an alias to run more than one command. You can do this with the alias tool, just as you can on the command line.

For example, I downloaded and compiled a command-line tool, weather, that worked well enough, although it took a while for me to find the precise name for the weather station I wanted, and it also only worked from inside its directory.

alias weather='cd /home/gregp/Downloads/weather-2.3;./weather "Louisville East CCD";cd /home/gregp'

In the example above, three commands are separated by semicolons (just like they would be on the command line). The alias takes me to the correct directory, runs the weather command, then goes back to /home.

alias forecast='cd /home/gregp/Downloads/weather-2.3;./weather -f -n "Louisville East CCD";cd /home/gregp'

Getting a forecast requires some switches to be set, and this forecast alias keeps me from having to repeatedly look them up.

Updating software

Finally, here's the most complicated alias I've created so far.

alias cmake15x='cmake . -DCMAKE_INSTALL_PREFIX:PATH=/home/gregp/scribus15x -DWANT_DEBUG=1 -DWANT_HUNSPELL=1 -DWANT_GRAPHICSMAGICK=1'

I wrote this because I am constantly keeping up to date with the latest versions of and compiling Scribus. It's easy enough to remember svn up to update Scribus from the repository, but then I have to run cmake with a number of settings. With this script, I can just type cmake15x, which is so much easier than trying to remember all the steps.

The alias command-line tool saves me a lot of time and keystrokes, especially for frequently used or complex commands. I would, however, recommend keeping some notes about how to use the more complex commands, especially in cases when more than just the command is required.

Greg Pittman
Greg is a retired neurologist in Louisville, Kentucky, with a long-standing interest in computers and programming, beginning with Fortran IV in the 1960s. When Linux and open source software came along, it kindled a commitment to learning more, and eventually contributing. He is a member of the Scribus Team.


Great article. It's probably worth mentioning that an alias overrides things in your PATH. So if I alias foo='bar', then the command located at /usr/bin/foo doesn't get run. To circumvent the alias and run the actual command, one can use the \ character; so '\foo' ignores the alias and runs whatever foo is found in the PATH. Alternately, one could just type the full path to foo.

Thanks for the article. I have an irrational fear that if I 'alias' any of my common commands I'll forget them entirely and be unable to do anything useful on other machines because I'll have completely forgotten anything but the alias. Probably irrational. Anyone have any stories there?

I have this problem with any commands I haven't used in a while, not specific to alias. Sometimes running alias and showing all your aliases will be a minirefresher course, and trigger memories about what you need even if it's not part of the aliased version. Then there's always the man pages.

In reply to by K_REY_C

Your weather example could be improved by using pushd/popd instead of cd.

Currently, whatever your working directory at the beginning, you always end up in /home/grep.

Replace the "cd /home/gregp/Downloads/weather-2.3" with

"pushd /home/gregp/Downloads/weather-2.3"

and the final "cd /home/gregp" with


Excellent information, but I can't seem to find the definitions for the 14 aliases that already exist by default in my Fedora 25 system. They're not in ~/.bashrc or /etc/bashrc

Check your .bash_profile file. If you're still stuck then look at each of these files to see if they're calling up some other file -- it's .bash_profile that actually calls .bashrc. It sounds like whoever set those up was intentionally hiding them.

In reply to by dnovak

I had this Idea to add 'tar' and 'untar' as two aliases. but never got around to it.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.