- How to add an alias to a command in terminal?
- 8 Answers 8
- Aliases’ File
- Alias Help
- How to Create and Use Alias Command in Linux
- List Currently Defined Aliases in Linux
- How to Create Aliases in Linux
- Creating Temporary Aliases
- Creating Permanent Aliases
- Linux alias command: How to create and use Linux aliases
- Using Linux aliases
- Alias examples
- How to define a Linux alias
- Unix/Linux alias examples and syntax
- Have a favorite Linux alias?
How to add an alias to a command in terminal?
By typing a manually specified command in terminal I want to execute some other command. How could add an alias to a command? Can i do that with the help of the terminal or should I edit some kind of file?
8 Answers 8
alias new_name='old command'
To create a permanent alias you have to edit the .bashrc file in your home directory.
There is no manual entry for alias. It is a built-in command. You must look at the man page for the shell.
On the bash command line it is simply a case of typing:
alias my_command="Command to run"
For example to create a short command run a long listing you could do:
The quotes are not required if you are not adding switches to the aliased command.
To make permanent changes you can put your aliases separetely in ~/.bash_aliases
You can either use the alias built-in command in the shell you’re using, or you can write a script which does what you want. Assuming you are using bash as the shell (which is the default), you can type man bash and skip down to the ALIASES section, for documentation on aliases in bash.
I write a GUI for adding/editing alias commands. You can also use it from commandline like this:
addalias -add "sinstall" "sudo apt-get install"
To make the changes permanent (i.e. to be read everytime you start a shell) add the alias commands you typed in the terminal to the file ~/.bashrc file.
This has been flagged for deletion. Can I suggest you add the relevant bits of the link into the answer to make it self contained?
You can directly create a file in your home for collecting all the aliases .bash_profile by writing nano ~.bash_profile and simply write on the file the commands/shortcuts you want to create, for example:
alias edbp='nano ~/.bash_profile'
and then validate it sourcing the file, so running
Remember that every time you modify your document you have to run again source ~.bash_profile
Aliases’ File
Add aliases to the file ~/.bash_aliases and create it if it doesn’t exist. For example, I have:
$ more ~/.bash_aliases alias trop='tree --dirsfirst -L 1' (. )
Alias Help
$ alias --help alias: alias [-p] [name[=value] . ] Define or display aliases. Without arguments, `alias' prints the list of aliases in the reusable form `alias NAME=VALUE' on standard output. Otherwise, an alias is defined for each NAME whose VALUE is given. A trailing space in VALUE causes the next word to be checked for alias substitution when the alias is expanded. Options: -p print all defined aliases in a reusable format Exit Status: alias returns true unless a NAME is supplied for which no alias has been defined.
How to Create and Use Alias Command in Linux
Linux users often need to use one command over and over again. Typing or copying the same command again and again reduces your productivity and distracts you from what you are actually doing.
You can save yourself some time by creating aliases for your most used commands. Aliases are like custom shortcuts used to represent a command (or set of commands) executed with or without custom options. Chances are you are already using aliases on your Linux system.
List Currently Defined Aliases in Linux
You can see a list of defined aliases on your profile by simply executing alias command.
Here you can see the default aliases defined for your user in Ubuntu 18.04.
You can create an alias with a single character that will be equivalent to a command of your choice.
How to Create Aliases in Linux
Creating aliases is relatively easy and quick process. You can create two types of aliases – temporary ones and permanent. We will review both types.
Creating Temporary Aliases
What you need to do is type the word alias then use the name you wish to use to execute a command followed by » your custom command here»
Here is an actual example:
You can then use «wr» shortcut to go to the webroot directory. The problem with that alias is that it will only be available for your current terminal session.
If you open new terminal session, the alias will no longer be available. If you wish to save your aliases across sessions you will need a permanent alias.
Creating Permanent Aliases
To keep aliases between sessions, you can save them in your user’s shell configuration profile file. This can be:
The syntax you should use is practically the same as creating a temporary alias. The only difference comes from the fact that you will be saving it in a file this time. So for example, in bash, you can open .bashrc file with your favorite editor like this:
Find a place in the file, where you want to keep the aliases. For example, you can add them in the end of the file. For organizations purposes you can leave a comment before your aliases something like this:
#My custom aliases alias home=”ssh -i ~/.ssh/mykep.pem [email protected]” alias ll="ls -alF"
Save the file. The file will be automatically loaded in your next session. If you want to use the newly defined alias in the current session, issue the following command:
To remove an alias added via the command line can be unaliased using unalias command.
$ unalias alias_name $ unalias -a [remove all alias]
Conclusion
This was a short example on how to create your own alias and execute frequently used commands without having to type each command again and again. Now you can think about the commands you use the most and create shortcuts for them in your shell.
Linux alias command: How to create and use Linux aliases
Unix/Linux aliases FAQ: Can you share some examples of the Linux alias command?
Using Linux aliases
Aliases in Unix and Linux operating systems are cool. They let you define your own commands, or command shortcuts, so you can customize the command line, and make it work the way you want it to work. In this tutorial I’ll share several Linux aliases that I use on a daily basis.
Alias examples
I’ve found it very helpful to create aliases to make my command line life easier. For instance, a lot of people don’t like the name of the grep command, and wish they could change it to search . With the alias command you can do exactly that:
Now you can just type search instead of grep at your Linux command line:
search 'Flinstone' StoryOfBedrock.txt
In another simple alias example, instead of always typing this ls command to get a directory listing:
I’ve created an alias so I only have to type the lowercase letter «L» like this:
Whenever I use this alias, it’s exactly the same as if I had typed out the longer ls -al Linux command.
Using aliases like this you can create anything from simple shortcuts like this to powerful custom commands.
How to define a Linux alias
Creating a Linux alias is very easy. You can either enter them at the command line as you’re working, or more likely, you’ll put them in one of your startup files, like your .bash_profile or .bashrc files, so they will be available every time you log in.
I created the l alias above by entering the following command into my .bash_profile file:
As you can see, the Linux alias syntax is very easy:
- Start with the alias command
- Then type the name of the alias you want to create
- Then an = sign, with no spaces on either side of the =
- Then type the command (or commands) you want your alias to execute when it is run. This can be a simple command, or can be a powerful combination of commands.
Unix/Linux alias examples and syntax
To get you going, here is a list of sample Linux aliases I use all the time. I’ve pretty much just copied them here from my .bashrc file:
alias dirs="ls -al | grep '^d'" alias l="ls -al" alias lf="ls -aFG" alias lm="ls -al|more" alias h="history" alias html="cd /web/apache/htdocs/devdaily/html" alias logs="cd /web/apache/htdocs/devdaily/logs" alias qp="ps auxwww|more" alias nu="who|wc -l" # GIT alias gpom="git push origin master" alias gs="git status" alias gb="git branch" alias gco="git checkout"
As you can see, you can get as creative as you want, and pipe commands together to do just about anything. In the last alias shown I’ve actually combined three Linux commands in a row into one alias to get the output I want.
Because the Unix shell is very programmable and because the output of commands is very consistent and reliable, you can create your own aliases (or macros if you prefer) to do just about anything.
Have a favorite Linux alias?
If you have a favorite Linux alias command/example you’d like to share, feel free to leave it in the comments section below.