alias Command in Linux with Examples

«
»

This tutorial explains Linux “alias” command, options and its usage with examples.

alias – Create an alias.

DESCRIPTION

An alias command is simple string substitution of one text for another, when it is used as the first word of a simple command.

Simply put, An alias is a (usually short) name that the shell translates into another (usually longer) name or command. Aliases allow you to define new commands by substituting a string for the first token of a simple command.

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.

SYNOPSIS

advertisement
advertisement

alias [name=[‘command’]]

OPTION :

-p Print the current values

EXAMPLES

1. To view the alias for a particular name, enter the command alias followed by the name of the alias.

$ alias ls
alias ls='ls --color=auto'

2. Enter an alias command to see which aliases are in effect.

$ alias
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: There are no spaces on either side of the equal sign. Quotes around command are necessary if the string being aliased consists of more than one word.

3. Create aliases for quickly navigating to one, two, three, or four higher-level directories.

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

4. Create the alias ‘port’ which will use the netstat command to display all currently open network ports.

$ alias port='netstat -tulanp'

5. How to temporarily stop using aliases

When you want to call the command instead of the alias, then you have to escape it and call.

advertisement
$ aliasname

For Example, alias cp=”cp -iv”, will ask you confirmation if you are about to overwrite a file. This can be annoying when you are copying lot of files that you already know you are going to overwrite. Probably you might want to temporarily use the regular cp command instead of the cp-alias.

So, if an alias cp exists, but you want to use the cp-command instead, escape the alias temporarily as shown below:

$ cp * /backup/files/dir1/

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 & technical discussions at Telegram SanfoundryClasses.