alias Command in Linux with Examples

Alias Command in Linux: Create Shortcuts to Save Time and Be More Efficient

An alias command is a simple string substitution of one text for another, when it is used as the first word of a simple command. In other words, you can create a shortcut for any Linux command by giving it a shorter and easier-to-remember name.

Aliases persist for the current session. They can be loaded at login time by modifying the shell’s .rc file. The invocation and usage of alias differs depending on the shell. They are typically placed in the ~/.bashrc (bash) or ~/.tcshrc (tcsh) startup files so that they are available to interactive subshells.

Syntax:

alias [name=['command']]
  • name: The alias name, which is typically a shorter version of the original command.
  • command: The actual command or set of commands you want to use with the alias, enclosed in quotation marks if needed.

Option:

-p: This option is used to list all the currently defined aliases.

advertisement
advertisement

alias Command Examples:

Example 1: Create an Alias

For example, let’s say you want to create an alias for the ls command to always show file sizes in a human-readable format and include hidden files. You can create an alias like this:

alias l='ls -lhA'

Now, when you enter l in your terminal, it’s equivalent to running ls -lhA, which lists files and directories in a human-readable format and shows hidden files.

Example 2: Viewing Aliases for a Specific Command

To view the alias for a specific command, use the alias command followed by the command’s name. For example, to check the alias for ls, you would enter:

$ alias ls

You will get something like:

advertisement
alias ls='ls --color=auto'

Example 3: Listing All Your Aliases

To see a list of all the aliases currently in effect, simply enter the alias command without any arguments:

$ alias

This will display a list of currently defined aliases. Here’s an example of what you might see:

advertisement
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

Note: Make sure there are no spaces around the equal sign, and if the aliased command consists of multiple words, it should be enclosed in quotes.

Example 4: Creating Directory Navigation Aliases

You can create aliases to quickly navigate up to one, two, three, or four higher-level directories. For example:

$ alias ..='cd ..'
$ alias ...='cd ../../../'
$ alias ....='cd ../../../../'
$ alias .....='cd ../../../../'

These aliases make it easier to navigate directories by allowing you to use .. to move up one level, … to go up two levels, and so on.

Example 5: Creating the ‘port’ Alias for Network Port Information

You can create aliases to make complex commands shorter. In this example, we create an alias ‘port’ to display all currently open network ports using the netstat command:

$ alias port='netstat -tulanp'

Now, you can simply use port to check open network ports.

Example 6: Temporarily Turning Off a Shortcut

If you want to use the original command instead of a shortcut for a moment, you can escape the shortcut. For example, if you have an alias called cp but want to use the regular cp command, type:

$ cp * /backup/files/dir1/

Example 7: Removing an Alias in Linux

To remove an alias in Linux, you can use the unalias command. Here’s how:

Syntax:

unalias alias_name

For example, if you have an alias named “myalias” that you want to remove, you can do this:

unalias myalias

This will delete the “myalias” alias.

Note: Please note that removing an alias is a permanent action, and the alias will no longer be available in your current shell session. If the alias was defined in a startup configuration file, you may need to edit that file to remove the alias permanently.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.