Linux users frequently find themselves repeating the same commands. Continuously typing or copying these commands can slow down your workflow and take your focus away from the actual tasks at hand.
To save time, you can create aliases for the commands you use most often. Aliases act as personalized shortcuts that stand in for a command (or group of commands), possibly including specific options. There's a good chance you're already using some aliases on your Linux system without even realizing it.
View Existing Aliases in Linux
You can view all aliases currently defined in your session by simply running the alias command.
<code>$ alias</code>
The output will show you the default aliases set up for your user in the Ubuntu system.
As shown, using the ll command is the same as executing ls -alF
.
<code>$ ll $ ls -alF</code>
You can define an alias with a short name that represents a longer command of your choice.
How to Set Up Aliases in Linux
Creating aliases is straightforward and fast. You can make two kinds of aliases – temporary and permanent, both of which we’ll explore below.
Creating Temporary Aliases in Linux
All you need to do is type the word alias, followed by the shortcut name you'd like to use, then an "="
sign, and finally enclose the full command in quotes.
The format is:
<code>$ alias shortName="your custom command here"</code>
Here’s a real example:
<code>$ alias wr=”cd /var/www/html”</code>
Once created, you can use the "wr"
shortcut to quickly navigate to the webroot directory. However, this alias will only last for the current terminal session.
If you open a new terminal window, this alias won’t be available anymore. If you want your alias to persist across sessions, you'll need to create a permanent alias.
Creating Permanent Aliases in Linux
To retain aliases between sessions, you should store them in your shell configuration file. This file depends on the shell you are using:
- Bash – ~/.bashrc
- ZSH – ~/.zshrc
- Fish – ~/.config/fish/config.fish
The syntax used is essentially the same as when creating a temporary alias. The key difference is that this time, you’re saving it into a file. For instance, in bash, you can edit the .bashrc file with your preferred editor:
<code>$ vim ~/.bashrc</code>
Locate where you’d like to insert your aliases. It’s common practice to add them at the end of the file. For clarity, you may also include a comment before listing your aliases, such as:
<code>#My custom aliases <strong>alias home=”ssh -i ~/.ssh/mykep.pem [email?protected]”</strong> <strong>alias ll="ls -alF"</strong></code>
Save the file after adding your aliases. These changes will automatically load in your next terminal session. To apply them immediately in your current session, run:
<code>$ source ~/.bashrc</code>
To remove an alias created in the terminal, use the unalias command.
<code>$ unalias alias_name $ unalias -a [removes all aliases]</code>
Conclusion
This guide has shown how you can create your own aliases to avoid retyping commonly used commands repeatedly. Now consider the commands you use most often and start creating shortcuts for them in your shell environment.
The above is the detailed content of How to Create and Use Alias Command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

While writing program files or normal text files, programmers and writers sometimes want to know the difference between two files or two versions of the same file. When you compare two computer files on Linux, the difference between their contents is

There are three ways to create empty files in the command line: First, the simplest and safest use of the touch command, which is suitable for debugging scripts or placeholder files; Second, it is quickly created through > redirection but will clear existing content, which is suitable for initializing log files; Third, use echo"> file name to create a file with an empty string, or use echo-n""> file name to avoid line breaks. These three methods have their own applicable scenarios, and choosing the right method can help you complete the task more efficiently.

Are you looking for good software to write mathematical equations? If so, this article provides the top 5 equation editors that you can easily install on your favorite Linux distribution.In addition to being compatible with different types of mathema

dutree is a free, open-source, fast command-line tool for analyzing disk usage, written in the Rust programming language. It was created by combining durep (disk usage reporter) and tree (list directory content in tree-like format) command-line tools

Eclipse is a free integrated development environment (IDE) that programmers around the world use to write software, primarily in Java, but also in other major programming languages using Eclipse plugins.The latest release of Eclipse IDE 2023?06 does

ifconfig in short “interface configuration” utility for system/network administration in Unix/Linux operating systems to configure, manage, and query network interface parameters via command-line interface or in a system configuration scripts

Linux administrators should be familiar with the command-line environment. Since GUI (Graphical User Interface) mode in Linux servers is not commonly installed.SSH may be the most popular protocol to enable Linux administrators to manage the servers

Linux has a rich collection of commands, and while many of them are powerful and useful for various tasks, there are also some funny and whimsical commands that you can try out for amusement. 1. sl Command (Steam Locomotive) You might be aware of the
