alias Command in Linux



The alias command is a powerful feature in Linux that allows you to create shortcuts for frequently used commands, making your work more efficient and less prone to errors. You can create a temporary alias using the alias command and it will last until you close the current terminal session.

This guide will help you understand how to use the alias command, its different options, and practical examples to get the most out of it.

Table of Contents

Syntax for alias Command

The basic syntax for the alias command in Linux is provided below −

alias [alias_name]='[command]' 

Where alias_name is the name you want to give to your shortcut, and [command] is the actual command you want to create an alias for.

Different Options Available for the alias Command

The alias command itself is quite simple, but you can create complex and powerful shortcuts by combining different commands and options. Here are some commonly used options and their descriptions −

Options Description
-p Print all defined aliases to the terminal.
unalias Remove an alias. Use unalias [alias_name] to delete a specific alias.
alias Without arguments, it prints the list of all current aliases.

Examples for the alias Command

Now, let’s go through some examples to better understand the working of the alias command in Linux.

  • Creating a Simple Alias
  • Viewing All Aliases
  • Removing an Alias
  • Creating Aliases with Multiple Commands
  • Making Aliases Permanent

Creating a Simple Alias

To create a simple alias, use the following syntax. This example creates an alias ll for the command ls -la −

alias ll='ls -la'  
alias Command Linux 1

After running this command, typing ll will execute ls -la.

Explanation

alias ll='ls -la' sets up ll as a shortcut for ls -la, which lists all files and directories with detailed information including hidden files.

Viewing All Aliases

To view all currently defined aliases, simply type −

alias 
alias Command Linux 2

This command will print a list of all aliases available in the current shell session.

Explanation

alias without any arguments displays all the aliases currently set in the shell, allowing you to see what shortcuts are available.

Removing an Alias

To remove a specific alias, use the unalias command. For example, to remove the ll alias created earlier, you would use −

unalias ll 
alias Command Linux 3

Checking the deleted alias −

alias Command Linux 4

This command will delete the alias ll.

Explanation

unalias ll removes the ll alias, meaning ll will no longer work as a shortcut for ls -la.

Creating Aliases with Multiple Commands

You can create an alias that runs multiple commands by using a semicolon to separate the commands.

For example, to create an alias update that updates your package list and upgrades all packages, use −

alias update='sudo apt update; sudo apt upgrade' 
alias Command Linux 5

Running update will now execute both commands in sequence.

Explanation

alias update='sudo apt update; sudo apt upgrade' sets up update as a shortcut for running both sudo apt update and sudo apt upgrade in sequence, which refreshes the package list and then upgrades all installed packages.

Making Aliases Permanent

Aliases created in the terminal are temporary and will be lost when you close the terminal. To make an alias permanent, add it to your ~/.bashrc or ~/.bash_aliases file. Open the file with a text editor, add your alias, and then source the file −

echo "alias ll='ls -la'" >> ~/.bashrc source ~/.bashrc 

First check whether ~./bash_aliases file exists or not. If not then you can create it by directly opening it in the nano text editor −

alias Command Linux 6

Create alias of your own choice −

alias Command Linux 7

Save the changes −

alias Command Linux 8

Check the alias −

alias Command Linux 9

This will ensure the alias is available in every new terminal session.

Explanation

echo "alias ll='ls -la'" >> ~/.bashrc adds the alias definition to the ~/.bashrc file, which is executed every time a new terminal session is started.

source ~/.bashrc reloads the ~/.bashrc file, applying the changes immediately without needing to restart the terminal.

Conclusion

The alias command is a versatile tool for creating shortcuts and improving productivity in Linux. By understanding its syntax and options, and using practical examples, you can create powerful aliases that simplify your workflow.

Whether you're creating simple shortcuts or combining multiple commands, the alias command can save you time and effort.

Advertisements