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 deniedWhen 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?
310 Answers
To create an alias permanently add the alias to your .bashrc file
gedit ~/.bashrcAnd 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
~/.bashrcfileFor example: append these line to
~/.bashrcfilealias ll='ls -l' alias rm='rm -i'Next time (after you have logged out/in, or done
. ~/.bashrc) when you typermtherm -icommand 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~/.bashrcfile and add the following lines if they don't already exist, or uncomment them if they do:if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fiSave it and close the file. After that, all you have to do is create a
~/.bash_aliasesfile and add your aliases there, with the same format specified in the first method.Contents of my
~/.bash_aliasesfile:alias cs='cd;ls'
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 ~/.bashrcOtherwise, 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
fiThis says that if the file ~/.bash_aliases exists, then run it.
3The 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/.bashrcNote 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/.bashrcor
. /home/bakhtiyor/.bashrc echo "alias vps='ssh -X '" >> ~/.bashrcThis 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.
1I 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.
# -----------------------------------
# Create a new permanent bash alias
#
# @param $1 - name
# @param $2 - definition
# -----------------------------------
new-alias () { if [ -z "$1" ]; then echo "alias name:" && read NAME else NAME=$1 fi if alias $NAME 2 > /dev/null > /dev/null; then echo "alias $NAME already exists - continue [y/n]?" && read YN case $YN in [Yy]* ) echo "okay, let's proceed.";; [Nn]* ) return;; * ) echo "invalid response." && return;; esac fi if [ -z "$2" ]; then echo "alias definition:" && read DEFINTION else DEFINTION="$2" fi if [ -f ~/.bash_aliases ]; then echo "alias $NAME=\"$DEFINTION\"" >> ~/.bash_aliases else echo "alias $NAME=\"$DEFINTION\"" >> ~/.bashrc fi alias $NAME="$DEFINTION"
} 4 if you are using ruby, you can install aka using rubygem.
gem install aka2
usage
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 peoplethe 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:
echo "alias wolfr='cd /home/wolf'">>./~bashrcNow you can type wolfr to move to wolf's home directory.
This is very similar and very cool, the export command:
echo "export ngse=/etc/nginx/sites-enabled"./~bashrcNow 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:
exec bashAlternatively, 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.
alias ok="ping google.com"After putting the aliases per line you have to reboot or relogin.
5As 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). The foo and bar aliases have been added, but to add baz the bashrc file must be "sourced" (or, just open a new terminal). Example as:
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias bar='echo foo'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ cat .bash_aliases
alias foo='echo foo'
alias bar='echo foo'
alias baz='echo baz'
thufir@dur:~$
thufir@dur:~$ source .bashrc
thufir@dur:~$
thufir@dur:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias bar='echo foo'
alias baz='echo baz'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias foo='echo foo'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
thufir@dur:~$
thufir@dur:~$ baz
baz
thufir@dur:~$ now the baz alias works. I only just now realized that a previous answer had mentioned this technique, but they had buried the lede.