alias Command in Linux Explained with Usage Examples

The alias Command in Linux enables the creation of shortcuts by substituting user-defined strings for longer commands. It enhances efficiency by simplifying frequent command execution and can be set temporarily or permanently in shell configuration files.

🤖AI Overview:

The alias Command in Linux is a built-in shell feature used to create convenient shortcuts for long or complex commands. It improves terminal efficiency and reduces errors by allowing users to define custom strings that execute full commands. Aliases can be temporary for the current session or permanent by adding them to shell configuration files, compatible with various Linux shells.

What is an alias Command in Linux?

An alias in Linux is a shortcut that points to a command. A user-defined string is substituted for a command-invoking string in the Linux shell using an alias. It is a built-in command in the shell. By substituting the less complex command for the more complex one, it turns the more complex command into a shortcut.

These commands are typically used in place of lengthy commands to increase efficiency and prevent potential spelling mistakes. Additionally, aliases can offer more choices in place of commands, making them simpler to use. A temporary “alias” is created when “alias” is used on the command line. Temporary aliases are only accessible while the shell is open. ‘alias’ should be kept in the bash startup files to make it permanent.

Prerequisites to Create Commands Shortcuts Using alias

To let this tutorial works correctly, provide the options below and move on.

  • A Server running Linux VPS.
  • A non-root user with sudo privileges.
  • Access to the terminal window/Command line.
  • A text editor. To choose between Vim and Nano, read about their differences.

alias Command Syntax

Linux Alias Syntax uses the below syntax:

alias [option] [name]='[value]'

Let’s review what any elements of the alias command mean:

alias: Activates the alias command.

[option]: Enables the command to list every active alias.

[name]: Establishes the new shortcut that makes use of a command. A name is a user-defined string that is free of special characters, aliases, and unaliases, which are not acceptable as names.

[value]: Names the command that the alias refers to. Options, arguments, and variables are additional components of commands. A path to a script you want to run can also be a value.

How to Create alias Command in Linux?

In Linux, you can create one of two types of aliases:

Temporary: Utilizing the alias command to add them.

Permanent: Editing system files is necessary for this.

Create alias Command in Linux Temporary

To make an alias that is only valid for the duration of the current terminal session, use the alias command. Creating c as an alias for the clear command, for instance:

alias c='clear'

Include them as a part of the value when establishing an alias if you want to use any additional command arguments. For instance, if you add the move command as an alias for the mv command and give it the choice to request confirmation before overwriting:

alias move='mv -i'

If you are interested in learning more about mv command, refer to our related article.

Create alias Command in Linux Permanently

You must include an alias in your shell configuration file in order to permanently change it. Utilize: Depending on the kind of shell you are using:

  • Bash shell: ~/.bashrc
  • Zsh shell: ~/.zshrc
  • Fish shell: ~/.config/fish/config.fish

Open the shell configuration file in a text editor to get started. The Bash shell and the Nano text editor are used in this example:

sudo nano ~/.bashrc

The section listing the standard system aliases can be found by scrolling down. Create a new section, add your aliases using the alias command syntax, and leave a brief description for ease of usage.

In this example:

#Custom aliases
alias c='clear'
alias move='mv -i'
alias frename='Example/Test/file_rename.sh'

To save the modifications to the configuration file after adding all the new aliases, hit Ctrl+X, type Y, and press Enter. When you open a new terminal window, the new aliases are loaded automatically. Load the configuration file by using the source command if you wish to utilize them in the current session:

Examples of Using alias Command in Linux

In this part, you will learn about some of the alias command usages in Linux.

List All aliases in Linux

The list of all presently set aliases is displayed by the alias command on its own:

alias

To list all alias in Linux, you can also add the -p flag. Running the following command, you can view the list in a format

that can be entered into the shell:

alias -p

Remove aliases in Linux

Use the command below to remove an alias:

unalias [name]

If you add the -a option, all aliases will be removed:

unalias -a

Create alias for ‘file’ command as ‘fi’

The main syntax is:

alias <newName>=<command>

Example:

alias fi=file

Create alias for ‘ls-l’ command as ‘ll’

The main syntax is:

alias <newName>=<'command'>

Example:

alias ll='ls -l'

Create alias with two Arguments

The main syntax is:

alias <newName>=<'command arg1 arg2'>

Example:

alias both='ls sample example'

Create alias for a Path

The main syntax is:

alias <newName>=<'/home/sssit/path/...'>

Example:

alias path='cd /home/sssit/Downloads/sample'

Make rm Command safer

The rm command is frequently used every day and, if misused, can be extremely disruptive and damaging to a system. So, you can use the alias as shown below to make the rm command safer:

#make rm command safer
alias rm="rm -i"

This makes the ‘rm’ command safer since every time you use it, it will always pause and ask you if you are certain you want to carry out the dreaded removal operation before moving on, giving you one final chance to avoid accidentally breaking things.

Easy & Fast ssh to another system

It can occasionally be simpler to create aliases with memorable names for each of them, especially if you connect in to other Linux systems using private keys rather than credentials.

#alias for servers SSH
alias sshserver1='ssh -i ~/Documents/IT/SSH-keys/server1.pem centos@server1.com'
alias sshserver2='ssh -i ~/Documents/SSH-keys/server2.pem [email protected]'
alias sshplexserverhome='ssh [email protected]'
alias sshclientserver='ssh -i ~/Documents/IT/SSH-keys/client.pem [email protected]'

Display Date & Time

Some users can take advantage of aliases by doing the following: Some people can take advantage of aliases either because they script things that require specific times or need to consult time and/or date and want to have them in specific formats.

alias nowtime='date +"%T"' #this will show the current time in 24hrs format as HH:MM:SS
alias nowdate='date +"%d-%m-%Y"' #this will show the current date in format dd-MM-YY

Managing iptables information

Using the alias command, you can manage your iptables information:

#Displaying iptables information the easy way :)
alias iptlist='sudo /sbin/iptables -L -n -v --line-numbers' #this will display all lines of your current iptables
alias iptlistin='sudo /sbin/iptables -L INPUT -n -v --line-numbers' #this will display all your INCOMING rules in iptables
alias iptlistout='sudo /sbin/iptables -L OUTPUT -n -v --line-numbers' #this will display all your OUTGOING rules in iptables

Update Debian-based servers

You can update a Debian server through an alias. This can be modified for any Red Hat, CentOS, and ‘yum’ command servers.

# update on one command
alias update='sudo apt-get update && sudo apt-get upgrade'

Run alias with sudo

By default, sudo does not allow the use of aliases. You must use sudo to create an alias like this in order to run an aliased command with root access:

sudo='sudo '

At this point, you have reviewed all alias commands in Linux. With the help of mentioned examples, you can understand how to use the alias command in Linux. You can now consider your most often-used commands and add shortcuts to them in your shell. There are more comprehensive Linux commands that you will not regret taking time and review them.

Conclusion

The alias Command in Linux is an essential tool for developers seeking to optimize their command-line efficiency and tailor their shell environments. By understanding how to define, manage, persist, and troubleshoot aliases, developers can significantly reduce repetitive typing, prevent errors, and craft a more personalized and productive workflow.

Proper use of the alias Command in Linux involves adherence to best practices such as clear naming conventions, use of configuration files for persistence, and awareness of alias scope in interactive versus scripting contexts. Incorporating these strategies will enable developers to harness the full potential of aliases and maintain consistent, efficient command-line operations.

Developers aiming to elevate their Linux terminal productivity should consider mastering the alias Command in Linux as an indispensable part of their toolkit.

FAQ

The basic syntax is: "alias name='command'"

Here, name is the shortcut string, and command is the full command the alias will execute. Additional command arguments can be included inside the single quotes.

Temporary aliases are created directly in the terminal using the alias command and last only for the session. To make an alias permanent, add the alias command line to the shell configuration file such as ~/.bashrc for Bash, ~/.zshrc for Zsh, or the appropriate config file for your shell.

Simply running the alias command without arguments displays all currently defined aliases. The command "alias -p" lists aliases in a format suitable for shell input.

Use the unalias command followed by the alias name to remove a specific alias. Using "unalias -a" removes all aliases in the current shell session.

For example, setting "alias rm='rm -i'" prompts for confirmation before deleting files, preventing accidental file removal. Aliases can include extra flags to add safety or convenience to commonly used commands.

Yes, alias is supported by most Unix-like shells including Bash, Zsh, and Fish, and works consistently across popular distributions such as Ubuntu, Debian, and CentOS.

By default, sudo does not recognize aliases. To run aliased commands with sudo, create an alias for sudo itself, e.g. "alias sudo='sudo '", to enable alias expansion with elevated privileges.

Common examples include creating shortcuts for clearing the screen, safer file operations, SSH connections with private key options, displaying formatted date and time, managing iptables commands, and system updates.

Permanent aliases should be added to your shells startup configuration file such as ~/.bashrc for Bash, ~/.zshrc for Zsh, or other shell-specific config files. After editing, running source on the file loads the aliases immediately.

Leave a Reply

Your email address will not be published. Required fields are marked.