8

How the Alias Command Works on Linux

 2 years ago
source link: https://dzone.com/articles/how-the-alias-command-works-on-linux
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

We've covered a lot of Linux commands, and it's sometimes necessary to write the same command repeatedly. We can use the alias command on Linux and other Unix-like systems to avoid the drudgery of typing the same thing often. It lets us make custom commands that will run a specific command of our choosing.

The alias command has no options, so it can be written as shown:

alias NAME="STRING"

How the Alias Command Works on Linux

We are constantly using the cd command to move into our documents folder. Instead of writing cd ~/Documents every time, we can define a new command which runs cd ~/Documents. I'm going to call it gtd (go to documents):

alias gtd="cd ~/Documents"

Now, all we have to type into our terminal to go to our documents folder is gtdAnd it'll run cd ~/Documents.

You can create any custom command with the alias. Here is another example where we create a command mtf, which moves my-file.txt in the current folder to ./test/my-file.txt:

alias mtf="mv my-file.txt ./test/my-new-file.txt"

Alias Only Lasts for the Session

The only thing to note about the alias is that the commands created are not permanent. If you close the terminal window, you'll lose them - so they provide a nice efficiency boost for a session but need to be redefined if you want to create them again. This is so you don't end up with many new commands sitting around and taking up namespaces.

Unalias Command

If you think you've made a mistake and want to remove an alias command, use unalias. For example, to remove our mtf command, we can run the following:

unalias mtf

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK