Open In App

How to Create and Use Alias Command in Linux

Improve
Improve
Like Article
Like
Save
Share
Report

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. 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 as alias for that command. Alias is like a shortcut command which will have same functionality as if we are writing the whole command. 

Syntax of alias Command in Linux

The Basic syntax of alias command in Linux is as follows:

alias shortname='longer command'

Here,

shortname = We can give any name we want.

longer command = it is where we type our command.

Options Available for Alias Command

1. -p option

This option prints all the defined aliases is reusable format. 

Syntax: 

alias -p

2. –help option :

It displays help Information. 

Syntax: 

alias --help

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'

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.

How to Remove an Alias

Removing an existing alias is known as unaliasing. 

Syntax: 

unalias [alias name]

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

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'

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

Commonly Used Aliases

1. Directory Navigation

alias ..='cd ..'

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'

Typing ‘cls’ clears the terminal screen.

Frequently Asked Question on Alias – 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"

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!

?t=291 
 



Last Updated : 10 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads