I want to create a shortcut for a command in terminal. Like for a command "ssh user@123.45.7.123", I just want a command "user" and the above command will run. Is this possible, and if so how can it be done?
Thank you!
I want to create a shortcut for a command in terminal. Like for a command "ssh user@123.45.7.123", I just want a command "user" and the above command will run. Is this possible, and if so how can it be done?
Thank you!
The shortcuts for commands are known as aliases.
The syntax to create an alias is:
alias custom_command='original_command'
Example:
For creating an alias for update and upgrade we should type the following in terminal,
alias update='sudo apt-get update'
alias upgrade='sudo apt-get upgrade'
So to create an alias for your command open the termianl and type:
alias user='ssh user@123.45.7.123'
If you want to not load the alias every time, to permanently store a alias command, do this.
Go to your home directory and press Ctrl+H to view hidden files, one of these files would be .bashrc
. Open it.
Now write the following command anywhere in the mainspace:
alias custom_command='original_command'
Your shortcut command will be stored permanently.
source .bashrc
after closing .bashrc
with ctrl-x and saying yes to save changes
– mLstudent33
Apr 27 '20 at 01:13
Aliases can take parameters. For example:
$ alias 777='sudo chmod -R 777 '
$ 777 MyFolder
will perform chmod recursively on MyFolder
ffmpeg -ss 00:00:00 -i input.mp4 -to 00:00:15 -c copy output.mp4
So that I can just type in the values that change, like the time and the filenames
– Harsha Mar 16 '20 at 08:42Generally the answer is to alias your command, as mentioned by M.Tarun. For your example with ssh you might want to add it to your .ssh/config
:
Host someName
HostName 123.45.7.123
User user
Then call ssh with the name:
$ ssh someName
Your shell probably also has tab-completion for ssh. So you can just type ssh s
and then hit Tab.
This also has the advantage that it works with other commands like scp
:
$ scp some-file someName:a/path/
Whereas the alias approach would not work with this.
~/.bash_aliases
. Then it will be automatically loaded. – panmari Aug 03 '13 at 09:47~/.bashrc
. Does it source~/.bash_aliases
? If yes, then just create it. – ahilsend Aug 03 '13 at 09:58.bash_aliases
file is not a standard bash feature (I think it is an Ubuntu thing), the standard way is to add alias definitions to your.bashrc
file. – terdon Aug 03 '13 at 15:17~/.bash_aliases
or~/.bashrc
don't forget to activate it bysource ~/.bash_aliases
orsource ~/.bashrc
– Mahdi mehrabi Aug 16 '20 at 05:59