Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to create an alias to rm command in order to have a confirmation message after executing this command. So I am creating an alias like this alias rm='rm -i'. But as far as I know this is a temporary alias and it lives until you close the terminal.

As it is explained here to save alias permanently I need to execute ~/.bash_aliases or ~/.bashrc commands in terminal and add my alias there. But when I execute ~/.bashrc I get following error message :

bash: /home/bakhtiyor/.bashrc: Permission denied

When I run ~/.bash_aliases I get another error message like this:

bash: /home/bakhtiyor/.bash_aliases: File or directory doesn't exist.

What is the actual problem and how can I solve it?

share|improve this question
How did you create an alias?? – karthick87 Dec 15 '10 at 8:05
@karthick87. I have updated my question. – Bakhtiyor Dec 15 '10 at 8:17
ANSWER -- – neaumusic Sep 10 '15 at 18:18
up vote 169 down vote accepted

To create an alias permanently add the alias to your .bashrc file

gedit ~/.bashrc

And then add your alias at the bottom.

alt text

Now execute . ~/.bashrc in your terminal (there should be a gap between the . and ~/.bashrc.

Now you can check your alias.

alt text

share|improve this answer
Thank you very much. – Bakhtiyor Dec 15 '10 at 11:05
@ButtleButkus - sadly, some here don't have any concept of other operating systems... – Wilf Jan 14 '14 at 17:40
@karthick87 you wrote "Now execute . ~/.bashrc in your terminal (there should be a gap between the . and ~/.bashrc.". Why is this step needed? – Geek Feb 11 '14 at 15:21
what does the first '.' do in . ~/.bashrc ? – Zen Jul 16 '14 at 4:13
@Geek @Zen "Execute" was not the correct term. The dot is equivalent to source. With . ~/.bashrc, you source your bash. Executing the file would start a child process, execute the commands in this process, then return. All that is done in the child process has no effect on the parent process (the bash from which you executed). Instead, sourcing (with the dot) acts exactly as if you wrote the content of the file in the terminal. This is what you want. .bashrc is sourced everytime you start a bash. If you make changes, they won't apply until you start a new bash or source manually. – Gauthier Oct 3 '14 at 9:19

There are lot of ways to create alias. The most used ways are :

  1. Add aliases directly in your ~/.bashrc file

    For example : append these line to ~/.bashrc file

    alias ll='ls -l'
    alias rm='rm -i'

    Next time (after you have logged out/in, or done . ~/.bashrc) when you type rm the rm -i command will be executed.

  2. The second method lets you make a separate aliases file, so you won't have to put them in .bashrc, but to a file of your choice. First, edit your ~/.bashrc file and add or uncomment the following lines, if it is not already

    if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases

    Save it and close the file. After that, all you have to do is create a ~/.bash_aliases file and add your aliases there, with the same format specified in the first method.

    Contents of my ~/.bash_aliases file:

    alias cs='cd;ls'
share|improve this answer
+1 for using ~/.bash_aliases. – ændrük Jun 10 '11 at 5:48
could you please remove the cat .bash_aliases as it is misleading :) – numediaweb Dec 2 '15 at 8:25

The problem is that you are trying to execute a non executable file: You can check this with:

ls -la ~/.bashrc
-rw-r--r-- 1 username username 3596 2010-08-05 17:17 /home/pt001424/.bashrc

Note there is no "x - executable" letter on the first column (file permissions).

Profile files are not executable files, instead of executing them you load them with:

source /home/bakhtiyor/.bashrc


. /home/bakhtiyor/.bashrc
share|improve this answer

It sounds to me like your only problem is simply trying to execute .bashrc when it is not executable. But this isn't the correct way to do it; whenever you make a change to this file, you should "execute" it by the command:

source ~/.bashrc

Otherwise, it will simply create a new shell, execute the file in the new shell's environment, then discard that environment when it exits, thereby losing your change. By sourcing the script, it executes within the current shell, so it will remain in effect.

I'm assuming the second error was because bash_aliases does not exist. It is not required, just recommended to keep your changes separate and organized. It is only used if it exists, and you can see the test for it in .bashrc:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases

This says that if the file ~/.bash_aliases exists, then run it.

share|improve this answer
~/.bash_aliases is better.… – tomByrer Jul 8 '14 at 2:39
using the source command made my aliases work. +1 – mightyspaj Dec 2 '15 at 10:00
echo "alias vps='ssh -X'" >> ~/.bashrc

This is an example I was looking for, a way to type a few letters at the terminal ("vps") to remotely log in to a server and enable X11 forwarding so I can run gui apps like "gedit" over the network.

Whatever the command / aliased command, this way with the echo statement, quotation marks, and the symbol for appending the output of a command to a file (>>) works for me. Just replace my command for the alias command you need and enter it into your terminal.

share|improve this answer
The quoting here is slightly tricky. In this example, using double quotes is unproblematic, but if the text within the quotes contains dollar signs, backslashes, etc, you will need to understand how the shell processes them inside double quotes. You can switch to single quotes on the outside and double quotes on the inside, but you then still need to understand how Bash processes the double quotes in the alias definition. – tripleee Nov 30 '15 at 6:04

I've made this little function for quickly writing a new-alias to .bashrc

##------------------------------------ ##
 #           -- new-alias --           #
 # creates new alias & writes to file  #
 #          $1 = alias new             #
 #          $2 = alias definition      #
##------------------------------------ ##
new-alias () { 
  if [ -z "$1" ]; then
    echo "alias name:"
    read NAME

  if [ -z "$2" ]; then
    echo "alias definition:"
    read DEFINTION
    if [ "$2" = "-cd" ]; then
      DEFINTION='cd '

  echo "alias $NAME='$DEFINTION'" >> ~/.bashrc
  . ~/.bashrc
share|improve this answer
Good idea, but careless using of this function may lead to trashing .bashrc with multiple instances of alias command. Your function definitely needs to implement some checkups to avoid such cluttering. – Troublemaker-DV Mar 31 at 1:04

I would suggest using /etc/bash.bashrc

You can add line at the end of that file.

alias ok="ping"

After putting the aliases per line you have to reboot or relogin.

share|improve this answer
"I would suggest .." and why would you suggest that? – muru Aug 9 '15 at 4:17
You should not be messing with the system file unless you specifically want to install a system-wide setting for all users. On a personal system, the difference is marginal, but then messing with system files is more complicated down the road, so you should probably still prefer your own personal dot files for personal preferences (and that makes it easier to copy the settings somewhere else in the future, too). – tripleee Nov 30 '15 at 6:06

if you are using ruby, you can install aka using rubygem.

gem install aka2


aka generate hello="echo helloworld" #add an alias
aka g hello="echo helloworld"  #add alias for lazy people

aka destroy hello #remove alias
aka d hello #remove alias for lazy people

the rubygem will auto-source your dot file so that you don't need to. Check it out.

share|improve this answer

protected by heemayl Aug 14 '15 at 18:22

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.