51

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?

1

8 Answers 8

61
alias new_name='old command'

To create a permanent alias you have to edit the .bashrc file in your home directory.

More info here

More .bashrc files here

2
  • 1
    There is no manual entry for alias. It is a built-in command. You must look at the man page for the shell.
    – dobey
    Jun 22, 2012 at 15:40
  • Changed it. ty.
    – OrangeTux
    Jun 23, 2012 at 7:04
17

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:

alias ll="ls -l"

The quotes are not required if you are not adding switches to the aliased command.

8

To make permanent changes you can put your aliases separetely in ~/.bash_aliases

1
  • 2
    Don't forget to run source ~/.bash_aliases for the change to take effect. Nov 30, 2018 at 10:40
2

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.

1

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"

https://github.com/isamert/addalias

1
  • I hate this and it shouldn't exist. Dec 28, 2023 at 2:14
0

To learn about aliasing: visit http://www.mediacollege.com/linux/command/alias.html

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.

1
  • 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?
    – fossfreedom
    Apr 5, 2022 at 10:40
0

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

source ~.bash_profile

Remember that every time you modify your document you have to run again source ~.bash_profile

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .