How can I make -f override -i with cp?

K

KenJackson

Guest
I have a number of aliases in my ~/.bashrc file, including this one:
Code:
alias cp="/bin/cp -i"
The -i switch make cp prompt me if it's going to overwrite an existing file. That's normally what I want.

But if I use the -f switch, I want it to take precedence and force the copy, regardless of whether it overwrites a file. But it doesn't work. Actually, it doesn't matter if I use the alias or not--when you use -i and -f in the same command, the -i overrides the -f.

Code:
$ touch test1 test2
$ /bin/cp -i -f test1 test2
/bin/cp: overwrite `test2'?

I guess I could come up with some elaborate function to inspect all the arguments, but I'm hoping for a simpler solution. Is there one?
 


OP
G

grim76

Guest
I think if you refer to cp as /bin/cp in place of just cp it should allow the override.
 
OP
R

Rob

Guest
Ok, then how about:

Code:
alias cpi="/bin/cp -i"

The fact that you're using -i means it'll be interactive no matter what.. (as far as I know)
 
OP
K

KenJackson

Guest
I think this function instead of the alias would do what I want.
But I haven't tested it and I'm not sure I'm going to bother with it.

Code:
function cp()
{
    ARGS=() IF=-i
    while test $# -gt 0; do
        case "$1" in
            -i|-f) IF=$1                 ;;
            *)     ARGS[${ARGS[*]}]="$1" ;;
        esac
        shift
    done
    /bin/cp $IF "${ARGS[@]}"
}
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Members online


Top