Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd please sign up again. Thanks!

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

Discussion in 'Command Line' started by KenJackson, Feb 22, 2012.

  1. KenJackson

    KenJackson Guest

    I have a number of aliases in my ~/.bashrc file, including this one:
    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.

    $ 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?
  2. Rob

    Rob Guest

    I would just get rid of the alias..
  3. KenJackson

    KenJackson Guest

    But that would violate the "that's normally what I want" clause.
  4. grim76

    grim76 Guest

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

    Rob Guest

    Ok, then how about:

    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)
  6. KenJackson

    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.

    function cp()
        ARGS=() IF=-i
        while test $# -gt 0; do
            case "$1" in
                -i|-f) IF=$1                 ;;
                *)     ARGS[${ARGS[*]}]="$1" ;;
        /bin/cp $IF "${ARGS[@]}"

Share This Page