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?
To create an alias permanently add the alias to your
.bashrc
fileAnd then add your alias at the bottom.
Now execute
. ~/.bashrc
in your terminal (there should be a space between the.
and~/.bashrc
.Now you can check your alias.
There are a lot of ways to create an alias. The most used ways are:
Add aliases directly in your
~/.bashrc
fileFor example: append these line to
~/.bashrc
fileNext time (after you have logged out/in, or done
. ~/.bashrc
) when you typerm
therm -i
command will be executed.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 the following lines if they don't already exist, or uncomment them if they do: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: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:
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:
This says that if the file ~/.bash_aliases exists, then run it.
The problem is that you are trying to execute a non executable file: You can check this with:
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:
or
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.
I wrote this helpful function to quickly create a new alias, and then write the alias definition to
~/.bash_aliases
(if it exists) or~/.bashrc
.TIP: Ensure
~/.bash_aliases
exists & is executed in~/.bashrc
.if you are using ruby, you can install aka using rubygem.
gem install aka2
usage
the rubygem will auto-source your dot file so that you don't need to. Check it out.
I'd love to expand on this idea!
You want to
alias
a command according to your question:Now you can type
wolfr
to move to wolf's home directory.This is very similar and very cool, the
export
command:Now you can type
cp $ngse/my_file /destination_directory/destination_filename
to copy a file from the sites-enabled directory to a destination.None of this will work until you do something like this:
Alternatively, you can re-log or you can reboot.
I would suggest using
/etc/bash.bashrc
You can add line at the end of that file.
After putting the aliases per line you have to reboot or relogin.
As I recall,
bashrc
has, or had, a line suggesting to not use it for aliases directly. The solution is to use an external file(s). Thefoo
andbar
aliases have been added, but to addbaz
thebashrc
file must be "sourced" (or, just open a new terminal). Example as:now the
baz
alias works. I only just now realized that a previous answer had mentioned this technique, but they had buried the lede.