How to alias cp with cp -i by default
Solution 1
You should put an alias in your start up script:
alias cp='cp -i'
You can put this directly in ~/.bashrc
, but I have in my ~/.bashrc
:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
and in ~/.bash_aliases
I have:
alias realias='source ~/.bash_aliases'
alias cp='cp -i'
alias rm='rm -i'
and when I have added/changed things to that file I do realias
(that does not remove aliases from the running shell you have taken out, for that use unalias
).
If you do man bash
and search for aliases you will not find examples but:
For almost every purpose, aliases are superseded by shell functions
The (`bash`) shell function alternative for the above alias is:
cp () { command cp -i "$@" ; }
shell functions are more powerful, but for simple things where aliases suffice.
I still tend to use them.
Solution 2
If you are using bash, the answers by Anthon and michas will work fine. However, if you are using csh or tcsh, the command to add will be
alias cp "cp -i"
and you will add it in your .cshrc
file.
Solution 3
alias cp="cp -i"
Put this line in your shell startup script. (probably ~/.bashrc)
Jordi Knol
Updated on September 18, 2022Comments
-
Jordi Knol almost 2 years
Is there a good way to alias the command
cp file1 file2
tocp -i file1 file2
?-
Baard Kopperud about 11 yearsIt's a great idea to alias destructive commands like cp, mv and rm - but don't depend on it... especially not as root! Because one day you'll be working at a computer without the alias you expect, and if you've then become used to the alias catching your mistakes, you'll be in for a rude awakening.
-
-
user about 11 yearsSomeone who is a Unix novice is likely to be using a bash-like shell, but +1 for completeness' sake :)
-
unxnut about 11 yearsThanks Michael. However, the novice works in whatever is given. And that depends mostly on the sys admins. Interestingly, on our campus, students get csh (not even tcsh) as their default shell working on Solaris. And since the original posting said Unix novice, I figured I should at least add my two cents worth.