Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a good way to alias the command cp file1 file2 to cp -i file1 file2?

Apologies if this is a dumb question, I'm a Unix novice.

share|improve this question
2  
It'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. –  Baard Kopperud Jun 7 '13 at 13:41

3 Answers 3

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.

share|improve this answer

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.

share|improve this answer
1  
Someone who is a Unix novice is likely to be using a bash-like shell, but +1 for completeness' sake :) –  Michael Kjörling Jun 7 '13 at 17:38
    
Thanks 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. –  unxnut Jun 7 '13 at 17:51
 alias cp="cp -i"

Put this line in your shell startup script. (probably ~/.bashrc)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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