Examples of alias Command in Linux

alias Command in Linux to Create Commands Shortcut

When running commands, the alias command asks the shell for replacing one word or string with another. Running long and complicated commands multiple times is time assuming and boring. alias command in Linux enables you to create a shortcut to replace those commands and save your time. Alias functions similarly to a shortcut command and has the same functionality as the full command that helps you use and remember commands.

The alias is available in Unix shells, AmigaDOS, 4DOS/4NT, KolibriOS, Windows PowerShell, ReactOS, and the EFI shell. Most Linux distributions support the alias command. Regardless if you are using what distribution of Linux, this tutorial will be useful for you since the alias command is used for Ubuntu, Debian, and CentOS.  Join us with this article to learn all about alias Linux to create command shortcuts.

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 [email protected]'
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.

FAQ

To begin, use the alias command.
Write the name of the alias you want to create after that.
Following that, a = sign with no spaces after it
Write the command (or commands) that you wish your alias to run when it is invoked. A straightforward command or a potent string of commands could be used in this.

Yes, case matters when entering commands into any of the command windows or shells in Linux or Unix.

To find an alias in Linux, type alias at the prompt to see a list of aliases.

 

To edit the file in the terminal, use the vim command. This will open your. bashrc file right away, and you must start adding aliases at the very end of the file, right after the last line that has been written. You should then source the bashrc file.

First, run bash and then try the command. If you still receive the error, put it in ~/.bash_profile which should be loaded automatically.

Conclusion

In this article, you learned What an alias command is and how to use an alias command in Linux. From now on, you can create and manage aliases using the alias command and carry out frequently used commands without having to repeatedly type every command. Also, your task will be more efficient as a result, and using terminal commands will be simpler. Using the alias command in Linux helps you to improve your efficiency. Creating aliases for your daily tasks enable you to make them easier. By the way, knowing Dangerous and Destructive Linux Commands is essential since it is destructive for your system if not properly used commands.

Leave a Reply

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