This tutorial explains Linux “alias” command, options and its usage with examples.
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
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.
$ 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.
$ 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.
- Buy Linux Books
- Apply for Linux Internship
- Buy Information Technology Books
- Apply for Programming Internship
- Practice Programming MCQs