The Wayback Machine - https://web.archive.org/web/20241128045438/https://www.geeksforgeeks.org/alias-command-in-linux-with-examples/
Open In App

How to Create and Use Alias Command in Linux

Last Updated : 03 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Imagine you’re lost in a maze of complicated Linux commands. You stumble upon a secret doorway marked “Alias,” and inside you find shortcuts to all your favorite commands! That’s what creating aliases is like. You get to make your own mini-commands for the long ones you use all the time, making things simpler and faster. This article guides you through building these shortcuts, turning you into a master of the Linux command jungle! No need for fancy tech words, we’ll keep it clear and fun, like chatting with a friend.

What is an alias in Linux?

In Linux, an alias is a user-defined shorthand for a longer command or sequence of commands. These aliases can be created and customized according to user preferences, making the command-line interface more user-friendly. The alias command instructs the shell to replace one string with another string while executing the commands. 
When we often have to use a single big command multiple times, in those cases, we create something called an alias for that command. Alias is like a shortcut command, which will have the same functionality as if we were writing the whole command. 

How does alias Command in Linux Work?

The alias command in Linux allows users to create shortcuts for long or complex commands, simplifying command-line usage. By defining an alias, you can substitute a shorter or more intuitive name for a lengthy command sequence. For example, you can set alias ll='ls -la' to make ll a shortcut for ls -la, which lists directory contents in detail. Aliases are typically defined in shell configuration files like .bashrc or .zshrc, ensuring they persist across sessions. This command enhances productivity by reducing the amount of typing and minimizing the chances of errors in frequently used commands.

Linux alias Syntax

  • The Basic syntax of the alias command in Linux is as follows:
alias shortname='longer command'

Here,

  • shortname – represents to that we can give any name we want.
  • longer command – represents to where we type our command.

aliasing with shortname variable

Options Available for Alias Command

The following are the some of the options available for alias command:

Option Description
alias Displays all current aliases
alias name='value' Creates a new alias with the specified name and command
unalias name Removes a specific alias
unalias -a Removes all aliases
alias --help Displays help information about the alias command
alias -p Prints all defined aliases in a reusable format
alias -t Temporarily creates an alias that lasts only for the current shell session (if supported by the shell)

How to Create and Use Alias Command in Linux ?

  • To create an alias, open your terminal and use the following syntax:
alias shortname='longer command'
  • The following screenshot illustrates on successfully creation of alias for shortname variable.

aliasing shortname

For example

  • If we want to create an alias for the ‘cd Desktop’ command (which is to move into Desktop directory using cd command), you can use:
alias CD="cd Desktop"
  • Here we have give shortname= CD and our longer command = cd Desktop
  • This alias allows you to type ‘CD’ instead of ‘cd Desktop’ for the same result.

setting the alias

List All Aliases in Linux

To list all aliases in Linux, you can use the alias command without any arguments. This will display all the currently defined aliases in your shell session. Here’s how it works:

  1. Open Terminal: Launch your terminal.
  2. Run the Command: Type alias and press Enter.
alias

This will output a list of all aliases, showing their names and the commands they represent. For example:

alias ll='ls -la'
alias gs='git status'
alias ..='cd ..'

Each line shows an alias name followed by the command it represents, making it easy to see and manage your shortcuts.

How to Remove an Alias?

  • The removing an existing alias is known as unaliasing. It following is the syntax of it.

Syntax

unalias [alias name]
  • This accurately reflects the process of removing an alias using the ‘unalias’ command in Linux.

Unaliasing the CD command

How to create an Aliases Persistent?

While creating an alias in the terminal is useful for the current session, users may want to make aliases persistent across sessions. To do this, add the alias command to your shell configuration file (e.g., ‘.bashrc’ for Bash or ‘.zshrc’ for Zsh). This ensures that your aliases are loaded each time you start a new terminal session.

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

Using Alias Command Effectively

Once aliases are set up, incorporating them into your workflow can significantly enhance productivity. Users can create aliases for commonly used commands, complex sequences, or even personalized shortcuts. For example, an alias like ‘update’ for the system update command could be:

alias update='sudo apt update && sudo apt upgrade'

alias update variable

  • With this alias, typing ‘update’ in the terminal will execute both update and upgrade commands in sequence.

How to remove all aliases at once using unalias?

Step 1: Open terminal for accessing command line interface.

Step 2: Execute the following command to unlias all the aliases:

unlias - a

unaliasing all

Step 3: Verify Removal, to ensure all the aliases are been removed with the following command

alias

aliasing all

  • On displaying no output confirms that all aliases have been successfully removed.

Commonly used Aliases Command Examples with Options

The following are the some of the commonly used aliases command:

1. Directory Navigation

alias ..='cd ..'

aliasing cd directory

  • This allows users to move up one directory level by typing ‘..’ instead of ‘cd ..’.

2. List Files with Details

alias ll='ls -l'
  • Shortening the ‘ls -l’ command for a detailed file list.

3. Clearing the Screen

alias cls='clear'

alising the variable with clear option

  • Typing ‘cls’ clears the terminal screen.

Best practices of using Alias Command

The following are the best practices of using alias command:

  • Keep the aliases simple and easy to remember for frequent commands.
  • Document aliases for clarity and future reference.
  • Avoid overriding system commands to prevent confusion or errors.
  • Ensure aliases are defined persistently in shell startup files.
  • Use aliases sparingly to maintain a clean and manageable command-line environment.

Conclusion

In this article we discussed “Alias” command which transforming long, winding paths into nimble shortcuts. This article equips you with the tools to craft your own mini-commands, making you a master of the command jungle. Ditch the tech jargon, this guide is like chatting with a friend, making things clear and fun.

So grab your machete of aliases and hack through directories, update systems with ease, and navigate this digital wilderness like a pro! Remember, the FAQs are your compass, always ready to guide you deeper into the jungle of Linux mastery. Step out of the shadows, wield your newfound skills, and make the command-line your domain!

Linux Alias Command – FAQs

How do I create an alias?

To create a basic alias, just type `alias your_alias="actual_command"` in your terminal. 

For example:

alias ll="ls -l"

How do I use an alias?

Just type the alias name as if it were any other command. In the previous example, typing `ll` would be equivalent to typing `ls -l`.

How do I see all my current aliases?

Type `alias` in your terminal. This will list all the aliases currently defined for your session.

How do I make my aliases permanent?

Add your alias definitions to your shell configuration file, like `.bashrc` for Bash or `.zshrc` for Zsh. This way, they’ll be loaded every time you open a new terminal.

Can I use spaces or special characters in an alias name?

You can’t use spaces, but most other characters (including _ and -) are allowed. However, it’s best to stick with simple names for clarity and avoid conflict with existing commands.

Can I have an alias that runs multiple commands?

Yes! You can chain commands together with semicolons within the alias definition. 

For example:

alias update="git pull; npm install"
 


Previous Article
Next Article

Similar Reads

Bash Scripting - Working of Alias
BASH Alias is a shortcut to run commands using some mapping. It is a way to create some shortcut commands for repetitive and multiple tasks. We create an alias in a BASH environment in specified files. We can also incorporate BASH functions, variables, etc to make the Alias more programmatic and flexible. What is a BASH Alias? A BASH Alias is a map
7 min read
How to Use ln Command to Create Symbolic Links in Linux
A symbolic link (symlink) is like a shortcut that points to a file or folder on Linux and other similar operating systems. Symlinks can be useful for organizing files and folders, or for making it easier to access files from different locations. In this guide, you'll learn how to create symbolic links in Linux using the 'ln' command. Use ln Command
5 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a Linux and macOS cat command that improved its precursor: the syntax highlighted file contents can be displayed with its help. Therefore, in the case of source code and configuration files, it is extremely helpful. It can highlight over 10 languages, like JavaScript, Java, Ruby, Python, Go, C, and JSON. Syntaxccat [OPTIONS] [FILE...]Basic
3 min read
How to use aplay and spd-say command in Linux?
We can send the text to the terminal to play that as an audio clip. These are basically command-line audio player and works using Linux Sound Architecture. To send audio to the terminal to play, you can use aplay and spd-say command as shown below. 1. aplay: This command in linux is used to play the audio file. Syntax: aplay filename Example: Here,
1 min read
How to Install and Use Sysdig from Linux Command Line?
Sysdig is a tool that provides administrators and developers with unparalleled insight into their systems' behavior. The team behind it hopes to enhance system-level monitoring and troubleshooting by providing a centralized, coherent, and granular view of the storage, transmission, network, and memory subsystems, as well as the ability to generate
2 min read
How to use the whois command on Ubuntu Linux
Whois is a command-line utility used in Linux systems to retrieve information about domain names, IP addresses, and network devices registered with the Internet Corporation for Assigned Names and Numbers (ICANN). The data received by Whois consists of the name and contact information of the domain or IP address owner, the registration and expiratio
3 min read
How to Use Linux lscpu Command
In the Linux environment, the lscpu command is an important utility for getting detailed information about the CPU (Central Processing Unit) configuration of our system. When this command is been executed, it provides the overview of the CPU architecture such as the CPU family, model, number of cores, threads, and other characteristics. In this art
5 min read
Use the Lines of a File as Arguments of a Linux Command
Handling multiple arguments for a command can be difficult, especially when there are a lot of parameters. One way to make this easier is to put the arguments in a text file and use each line of the file as an argument for the command. This can simplify the process and save time. For example, let's say you have a text file with a list of 100 client
6 min read
How to use SSH to connect to a remote server in Linux | ssh Command
Secure Shell, commonly known as SSH, is like a super-secure way to talk to faraway computers, called servers. It's like a secret tunnel on the internet that keeps your conversations safe and private. Imagine you're sending a letter, and instead of sending it openly, you put it in a magic envelope that only you and the person you're sending it to ca
8 min read
How to Create Directory in Linux | mkdir Command
In Linux, the 'mkdir' command is like a magic wand for creating folders super easily. 'mkdir' stands for "make directory," and it helps you organize your computer stuff by creating folders with just one command. Whether you're making one folder or a bunch of them in a row, 'mkdir' is there to help you keep things neat and tidy on your computer. In
7 min read
How to Create an Empty File in Linux | Touch Command
The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows: touch command: It is used to create a file without any content. The file created using the touch c
8 min read
How to Create a Text File Using the Command Line in Linux
The Linux command line is a powerful tool that allows you to manage your system with precision and speed. One of the most common tasks is creating a text file, which can be achieved using several commands and methods. Here we will learn some quick ways to create a text file from the Linux Command Line or Terminal. Table of Content Using the 'touch'
6 min read
How to Create a new group in Linux | groupadd command
In the Linux operating system, user management is a crucial aspect of system administration. One of the fundamental tasks is creating and managing user groups. Groups in Linux allow administrators to organize and control user access to various resources and files. The groupadd command is a powerful tool that facilitates the creation of new groups e
7 min read
How to Display and Set Date and Time in Linux | date Command
date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.  Syntax of Date Command in Linux date [OPTION]... [+F
6 min read
enable and disable command in Linux
Enables and disables are the built-in shell commands. enable command is used to start the printers or classes whereas the disable command is used to stop the printers or classes. Syntax For enable Command: enable [-a] [-dnps] [-f filename][name ...] Syntax For disable Command: disable [-c] [-W] [ -r [ reason ] ] printer Options: -a : List all built
1 min read
Working with zcat, zmore and zless Command in Linux
Compression means to keep files together in an archive and to reduce the size of files. Gzip a.k.a. Gunzip is a command in Linux which is used to compress and decompress the files. So in case, you compressed a very large file with gzip and you just want to see what are the contents of the file. As in this case decompression of the file is going to
2 min read
Iperf Command to Test Speed, Performance and Bandwidth of Network in Linux
There is a great degree of flex in how the packets are delivered and overall bit rate and packet payload size can be controlled. iperf is a tool that is used to perform network performance measurement and tuning. iperf is an open-source software which is written in C language. Jperf is a GUI version for doing the same job. The core concept work beh
2 min read
Optimize and Compress JPEG or PNG Images in Linux Command line
Today there are many GUI tools that compress and optimize the images, which are very useful. But if you are terminal addicted and want to compress and optimize the images in the terminal before uploading to the cloud, then you can do it. There are two tools by using which we can compress and optimize images. These are - jpegoptimOptiPNGjpegoptim jp
4 min read
Top Linux Command Line Tips and Tricks
Linux is a great OS that offers multiple options for simplifying tasks. It has everything you need for working more efficiently, whether a command-line or a GUI. However, many people don't know the Linux command line's short tricks and end up being stuck in complex tasks. An interface (text-based) through which you communicate with your system's OS
5 min read
How to Start, Stop and Restart Services in Linux Using systemctl Command
System services play a crucial role in the functioning of a Linux system, handling various tasks and processes in the background. systemctl is a powerful command-line tool that allows users to manage these services effectively. In this article, we will explore the basics of using systemctl to start, stop, restart, enable, disable and display status
9 min read
Difference Between su and su - Command in Linux
As a new Linux user, you may always face confusion regarding the difference between `su` command and `su -` command. In Linux, the `su` command is used to switch to another user account. However, there are two variations of the `su` command: `su` and `su -` (su hyphen). Table of Content What is Linux User Environment?What is su Command?What is su -
6 min read
How to Copy Files and Directories in Linux | cp Command
In the world of Linux computers, a common and important job is copying files. The key tool for this task is the "cp" command. In this simple guide, we'll explore how to copy a file in Linux, looking at the basic steps, different choices you can make, and giving clear examples. We'll also take a peek behind the scenes to understand how the "cp" comm
8 min read
mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
How to limit search a specified directory in Linux? There is a command in Linux to search for files in a directory hierarchy known as 'find' . It searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left-hand side
5 min read
Command-Line Tools and Utilities For Network Management in Linux
If you are thinking of becoming a system administrator, or you are already a system admin, then this article is for you. As a system admin, your daily routine will include configuring, maintaining, troubleshooting, monitoring, securing networks, and managing servers within data centers. Network configuration and troubleshooting are some important t
8 min read
Nice and Renice Command in Linux with Examples
In Linux, managing process priority is crucial for optimizing system performance, especially in environments where multiple processes compete for CPU resources. The nice and renice commands allow users to adjust the scheduling priority of processes, influencing how the Linux kernel allocates CPU time among them. 'nice' Command: This command is used
3 min read
How to Compress and Extract Files Using the tar Command on Linux
Tar archives are special files that bundle multiple files and directories into a single file, making it easier to store, share, and manage. While tar archives themselves are not compressed, they can be combined with various compression utilities to reduce their size. The tar command is highly versatile, allowing users to create, modify, and extract
3 min read
Difference between locate, which and find Command in Linux
In Linux, there are multiple utilities available to locate files efficiently within a system. Three of the most commonly used commands for this purpose are: findlocatewhichEach command serves a similar function but differs in the way they search and the data they return. In this article, we'll explore the differences between these commands, how the
5 min read
How to Use Cryptmount Utility to Create Encrypted Filesystems in Linux?
Cryptmount a utility which allows user to access encrypted files, which is developed by Linux Operating Systems here they do not require root privileges. Cryptmount requires Linux Distributions with kernel 2.6 or later. Encrypted files and encrypted partitions, both are handled by Cryptmount. Simple administration and system administrator are offer
5 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
three90RightbarBannerImg