An alias is nothing but the shortcut to commands. The alias command allows the user to launch any command or group of commands (including options and filenames) by entering a single word. Use alias command to display a list of all defined aliases. You can add user-defined aliases to ~/.bashrcfile. You can cut down typing time with these aliases, work smartly, and increase productivity at the command prompt.
More about aliases
The general syntax for the alias command for the bash shell is as follows:
Task: List aliases
Type the following command:
alias ..='cd ..' alias amazonbackup='s3backup' alias apt-get='sudo apt-get' ...
By default alias command shows a list of aliases that are defined for the current user.
Task: Define / create an alias (bash syntax)
To create the alias use the following syntax:
In this example, create the alias c for the commonly used clear command, which clears the screen, by typing the following command and then pressing the ENTER key:
Then, to clear the screen, instead of typing clear, you would only have to type the letter ‘c’ and press the [ENTER] key:
Task: Disable an alias temporarily (bash syntax)
An alias can be disabled temporarily using the following syntax:
Task: Remove an alias (bash syntax)
You need to use the command called unalias to remove aliases. Its syntax is as follows:
In this example, remove the alias c which was created in an earlier example:
You also need to delete the alias from the ~/.bashrc file using a text editor (see next section).
Task: Make aliases permanent (bash syntax)
The alias c remains in effect only during the current login session. Once you logs out or reboot the system the alias c will be gone. To avoid this problem, add alias to your ~/.bashrc file, enter:
The alias c for the current user can be made permanent by entering the following line:
Save and close the file. System-wide aliases (i.e. aliases for all users) can be put in the /etc/bashrc file. Please note that the alias command is built into a various shells including ksh, tcsh/csh, ash, bash and others.
A note about privileged access
You can add code as follows in ~/.bashrc:
A note about os specific aliases
You can add code as follows in ~/.bashrc using the case statement:
30 uses for aliases
You can define various types aliases as follows to save time and increase productivity.
#1: Control ls command output
The ls command lists directory contents and you can colorize the output:
#2: Control cd command behavior
#3: Control grep command output
grep command is a command-line utility for searching plain-text files for lines matching a regular expression:
#4: Start calculator with math support
#4: Generate sha1 digest
#5: Create parent directories on demand
mkdir command is used to create a directory:
#6: Colorize diff output
You can compare files line by line using diff and use a tool called colordiff to colorize diff output:
#7: Make mount command output pretty and human readable format
#8: Command short cuts to save time
#9: Create a new set of commands
#10: Set vim as default
#11: Control output of networking tool called ping
#12: Show open ports
Use netstat command to quickly list all TCP/UDP port on the server:
#13: Wakeup sleeping servers
#14: Control firewall (iptables) output
Netfilter is a host-based firewall for Linux operating systems. It is included as part of the Linux distribution and it is activated by default. This post list most common iptables solutions required by a new Linux user to secure his or her Linux operating system from intruders.
#15: Debug web server / cdn problems with curl
#16: Add safety nets
#17: Update Debian Linux server
apt-get command is used for installing packages over the internet (ftp or http). You can also upgrade all packages in a single operations:
#18: Update RHEL / CentOS / Fedora Linux server
yum command is a package management tool for RHEL / CentOS / Fedora Linux and friends:
#19: Tune sudo and su
#20: Pass halt/reboot via sudo
shutdown command bring the Linux / Unix system down:
#21: Control web servers
#22: Alias into our backup stuff
#23: Desktop specific – play avi/mp3 files on demand
#24: Set default interfaces for sys admin related commands
vnstat is console-based network traffic monitor. dnstop is console tool to analyze DNS traffic. tcptrack and iftop commands displays information about TCP/UDP connections it sees on a network interface and display bandwidth usage on an interface by host respectively.
#25: Get system memory, cpu usage, and gpu memory info quickly
#26: Control Home Router
The curl command can be used to reboot Linksys routers.
#27 Resume wget by default
The GNU Wget is a free utility for non-interactive download of files from the Web. It supports HTTP, HTTPS, and FTP protocols, and it can resume downloads too:
#28 Use different browser for testing website
#29: A note about ssh alias
Do not create ssh alias, instead use ~/.ssh/config OpenSSH SSH client configuration files. It offers more option. An example:
You can now connect to peer1 using the following syntax:
$ ssh server10
#30: It’s your turn to share…
This post summarizes several types of uses for *nix bash aliases:
- Setting default options for a command (e.g. set eth0 as default option for ethtool command via alias ethtool='ethtool eth0' ).
- Correcting typos (cd.. will act as cd .. via alias cd..='cd ..').
- Reducing the amount of typing.
- Setting the default path of a command that exists in several versions on a system (e.g. GNU/grep is located at /usr/local/bin/grep and Unix grep is located at /bin/grep. To use GNU grep use alias grep='/usr/local/bin/grep' ).
- Adding the safety nets to Unix by making commands interactive by setting default options. (e.g. rm, mv, and other commands).
- Compatibility by creating commands for older operating systems such as MS-DOS or other Unix like operating systems (e.g. alias del=rm ).
I’ve shared my aliases that I used over the years to reduce the need for repetitive command line typing. If you know and use any other bash/ksh/csh aliases that can reduce typing, share below in the comments.