How to Create Your Own Commands in Linux
In this article, let us learn how to create our own commands in Linux. Yes – we will talk about creating alias commands.
What is Alias Command in Linux?
alias
Command provides a string value that replaces the command name when it is encountered.
alias
The command allows us to create shortcuts for long commands, making them easier to remember and use. It will have the same functionality as running the entire command.
How to create your own Linux commands
Using alias
the command we will be able to create our own commands. Creating your own commands is very simple.
Following is alias
the syntax of the command:
$ alias [alias-name[=string]...]
Let's look at an example of creating your own command.
Let's say we want to create a cdv
command called and entering that command in the terminal should take us to the Videos directory.
Normally, to go into a directory, we use cd
the command. To go into the Videos directory, we need to use cd Videos
, as shown in the following screenshot:
Let's create cdv
a command called to go into the Videos directory. To do this, we have to type the following command in the terminal:
$ alias cdv="cd Videos"
We have created our command. From the screenshot above, you can see that it does not return anything.
But how do we verify that the command was created and is running?
There is only one way to verify that a command worked: that is to execute the command you created.
Run the command in your terminal cdv
and see what happens:
How to view the created alias commands
After creating a few commands, we may have the following questions:
Suppose I have created multiple alias commands. How can I view all of them together? How can I view the equivalent command of the alias?
We can -p
view all aliased commands by appending the -a flag to the aliased command, like this:
$ alias -p
From the screenshot above, you can see that I have created only one alias command.
How to Delete Alias Commands in Linux
Pass our alias as an argument to unalias
the command to remove the alias.
$ unalias alias_name
How to Delete All Aliases in Linux
Let's assume that we have added about 20 alias commands. After a while, we realize that using alias commands makes us forget other commands for a long time. Because of this concern, we want to delete all alias commands.
We have a command to do this:
$ unalias -a
You may want to know one thing.
"After a while, you realize that using aliased commands makes you forget other commands in the long run"
Is this something you should be worried about? Is this going to happen?
The answer to your first question is, yes. You will definitely feel that way when you learn and try out alias commands. Because I feel the same way.
The answer to your second question is, absolutely not. It will increase productivity. You will most likely forget the commands you created, but you will never forget the original command. So I always recommend revisiting our aliases frequently and making sure that all the aliases we created are being used.
I have a shocking surprise for you. Open a terminal window and create an alias command (we will use cdv
the command created above). Open another terminal window and type the cdv command in it.
Yes. If we create an alias command, it will be valid only for that specific instance of the terminal. It will not be created permanently, so we will not be able to access it in two different terminal windows unless we run alias
the command in both terminals.
How to create a permanent alias command
To create a permanent alias command, we have to add the alias command to the shell configuration file. There are many shell configurations available. Some of the famous shells are:
- Bash - ~/.bashrc
- Zsh - ~/.zshrc
- Fish - ~/.config/fish/config.fish
Most Linux distributions use bash, so let's look at creating a permanent alias in the bash shell. Other shells work pretty much the same.
Let's open the .bashrc file using Vim.
$ sudo vim ~/.bashrc
Go to the bottom of the file and press i to enter insert mode. Add the alias command you want to make permanent.
alias cdv="cd Videos"
Press the Esc key and enter :wq to save and exit Vim.
Each time you change a shell configuration file, you must reload the file for the changes to take effect immediately.
By default, all terminal windows we open from now on will contain our aliased command.
We can open multiple windows and alias -p
check by entering the command.
How to run multiple commands in a single alias command
We can achieve this in two ways. Let me explain them here.
Let us learn this through an example.
Assume that we have to create an gohome
alias command called . Running this command should take you to your home directory and display the "Navigated to home directory" message.
Method #1:
This is the common way to add alias commands. We have to add ;
two commands separated by a semicolon.
$ alias gohome="cd ~/;echo Navigated to home directory"
Method #2
This is a different way. To do this, we have to change the .bashrc file. A function must be defined in the .bashrc file and all the commands must be nested inside it.
Open the .bashrc file using Vim .
Press the i key to enter insert mode.
Use the above 2 commands to create a function called gohome.
function gohome() {
cd ~/
echo Navigated to home directory
}
:wq
Save and exit Vim
by pressing the Esc key and typing in command mode .
source ~/.bashrc
We can now verify the gohome command by reloading the terminal
by running .
注意
: Creating a function does notalias -p
list it as an alias command when running the command.
Summarize
In this article, you learned how to create your own commands in Linux.
Using aliases will definitely increase our productivity. After seeing many people using aliases, I have witnessed their exponential growth. I recommend everyone to set up their own aliases.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Restart PostgreSQL in Ubuntu 18.04
Publish Date:2025/04/09 Views:72 Category:PostgreSQL
-
This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n
Issues to note when installing Apache on Linux
Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM
-
As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s