JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

How to Create Your Own Commands in Linux

Author:JIYIK Last Updated:2025/04/06 Views:

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?

aliasCommand provides a string value that replaces the command name when it is encountered.

aliasThe 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 aliasthe command we will be able to create our own commands. Creating your own commands is very simple.

Following is aliasthe 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 cdvcommand called and entering that command in the terminal should take us to the Videos directory.

Normally, to go into a directory, we use cdthe command. To go into the Videos directory, we need to use cd Videos, as shown in the following screenshot:

Create your own command in Linux to enter the Videos directory

Let's create cdva 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"

Terminal (alias) commands to create our own commands

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 cdvand see what happens:

Run the created cdv command


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 -pview all aliased commands by appending the -a flag to the aliased command, like this:

$ alias -p

Terminal command to view all created alias commands

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 unaliasthe command to remove the alias.

$ unalias alias_name

Terminal command unalias delete alias command


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 cdvthe command created above). Open another terminal window and type the cdv command in it.

Terminal command shows output of non-existent alias command

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 aliasthe 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.

Terminal command to view all alias commands

We can open multiple windows and alias -pcheck 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 gohomealias 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"

Run multiple commands using a single alias command - Method 1

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
}

:wqSave and exit Vim by pressing the Esc key and typing in command mode .

source ~/.bashrcWe can now verify the gohome command by reloading the terminal by running .

注意: Creating a function does not alias -plist 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.

Article URL:

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 –

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial