BASH: persistent aliases

A very nice feature of BASH is defining aliases. An alias is like an alternate name or a nickname for a command. Instead of typing a long comand with many arguments every time you use it, you can just type the (short) alias you chose for it. For example, if you want to see file details when using the ls command, you need to write ls -l. If you use the long listing format a lot, it will be faster and easier to define an alias for it. Let’s call it ll (list long):
alias ll='ls -l'
From now on, when you write ll it is replaced with ls -l and you see the file details in the output. By defining aliases in this manner you can only use them until you reboot.

Making them persistent

In order to have persistent aliases, you need to append them to the .bashrc file. On my Debian Linux this file is located in /home/myUserName/.bashrc. Note that this is a hidden file (its name starts with .(dot)), which means it might not appear in the list of files you see when browsing through folders from the graphical interface (e.g. Gnome, KDE) or when you issue the ls command from a console. However, if the file is present there you should be able to see it with the ls -a command (the -a flag means show all files).

Open the .bashrc file in your user profile with a text editor and append your aliases to its end. They will be available for use after you start a new BASH (in the graphical environment you can use Konsole, Terminal etc.) and will be persistent after a reboot.

Note: You should find some (commented) aliases at the end of the .bashrc file. I think the following ls ones are particularly useful:
alias ls='ls --color=auto' – always use colors
alias ll='ls -l' – list long (with details)
alias la='ls -A' – list all (without implicit . and ..)
Just uncomment them (delete the # character from the beginning of the line in the file) to make them active. Read more about the ls command in the manual pages: man ls

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s