Linux alias List, Set, Create & Remove with alias Command

This tutorial explains linux alias command with syntax and options in details including how to list, set, add, remove, create and delete alias in Linux temporary and permanently step by step with practical examples. Learn how to customize the Linux environment with local and global alias in detail.

How to check default alias

To check default alias run following command

$alias

alias command

Some of the aliases listed are likely to be system-wide aliases that apply to all users and are created automatically for each new user for a particular shell. Aliases for any other shell can be seen by first switching to that shell and than using the alias command as above.

alias command allows you to launch any command or group of commands with simple names or abbreviations.

Syntax of alias is

alias name="value"
  • name is the name of the new alias
  • value is the command(s) which it initiates.
  • No spaces are permitted before or after the equals sign. Any number of aliases can be created simultaneously by enclosing the name in each name-value pair in quotes.
  • The alias name and the replacement text can contain any valid shell input except for the equals sign ( = ).
  • The commands, including any options, arguments and redirection operators, are all enclosed within a single pair of quotation marks, which can be single quotes or double quotes.

Take a simple example of ls command. ls command list the content of directory.

ls command

With -l switch it list content in long format with details

ls command with l switch

With the use of alias command we can create an alias for ls command with -l switch so when you run ls command it execute with -l switch

alias ls with l

You can use any simple easy to remember name instead of command and than use them in the same way that ordinary commands are used.

For example you can use list keyword

alias list

alias set in this way are known temporary alias. Temporary alias would not be available after logout.

How to make alias permanent

You can make alias permanent locally and globally.

To make an alias permanent on user level edit ~/.bashrc file.

In Linux world on command prompt any file or folder deleted once would be deleted forever. But we can make TRASH folder using alias command.

Make a trash folder. Keep it hidden. [Put a DOT in front of folder name ]

$mkdir .trash

mkdir trash

Open .bashrc file

user vi bashrc

In the end of file add following command

alias rm="mv -t ~/.trash"
  • rm command used to delete file or folder [with switch]
  • ~ In Linux ~ (tilde sign) represent users home directory

alias edit user bashrc

.bashrc file initialized when user login.

Logout from current session

user exit

login back

user login alias

Now when a user run rm command shell will actually execute mv command. Create a test file and delete it with rm command

create test file

you can restore deleted file from trash folder. Restore our deleted file

restore test file

To make an alias permanent on system level login form root and open /etc/bashrc

roo vi bashrc

add your custom alias at the bottom of file and save it

bashrc root edit

logout form current session login back and test alias

root test alias

How to unset alias

If you need new alias with same name than best way to remove an alias is by use the alias command to create a new alias with the same name. This overwrites the existing alias with that name.

If you only want to remove alias use unalias command

unalias

If you have created permanent alias than open that file again and remove alias entry from configuration file.

remove system level

Use of Linux alias command for system administrator

alias command can be used in several ways. Most popular use of alias command among the Linux system administrators are following

Use alias command to reduce the amount of typing

For example you frequently need to go in directory which have long path. You can create an alias for that directory and use it. Like we have a directory with the path

~/custom_script/linux/web_script/php/new_script

we can create an alias for it

$ alias new_php_script="cd ~/custom_script/linux/web_script/php/new_script"

Now whenever we need to go in that directory, we only need to type new_php_script on command prompt

$new_php_script

Use alias command to specify default options for command.

Like we have specified for ls command in above

Use alias command to create trash on command prompt

As we create in above example

Use alias command for safety of the system

A system administrator use alias to increase the safety of the system by making commands interactive. This forces the user to confirm that it is desired to perform a specific action and thereby reduces the risk from accidental or impulsive abuse of powerful commands.

For example cp command which is used to copy the contents of one file to another file, can also be reduced by making it interactive by default. If the name for the file to be written to does not exist in the specified directory (by default the current directory), it will be created, but if it already exists, its contents will be overwritten.

$alias cp="cp -i"

Above alias will reduce the chances of an unintended overwriting. Now if it detects any existing file with same name rather than overwriting that file shell would ask for confirmation.

Use alias to correct misspellings of commands.

For example a user which switched from window platform, he has a habit of typing dir instead of ls. We can create an alias for it in following way

$alias dir="ls"

Now user can use dir also to list the content.

ComputerNetworkingNotes Linux Tutorials Linux alias List, Set, Create & Remove with alias Command