Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just read the Linux scp command issue question and it reminded me that I regularily forget to specify the colon in the host part of a scp command, and thus copying a file locally instead of copying to a remote host, e.g. I do

scp foo host

instead of

scp foo host:

But I never use scp to copy a file locally. So I wonder if there is a way to make scp fail if both (the source and destination) arguments refer to local files.

share|improve this question
up vote 1 down vote accepted

You could write a wrapper function in Bash and name it scp that uses getopts to process the options into a new array leaving the source and destination in the positional parameters, then check those for the presence of "@" and ":" and, if absent, issue an error message. If they're present, then call the real scp using something like command scp "${args[@]}" "$source" "$dest". The command command causes Bash to call the program that's found in the PATH instead of the function by the same name.

Since, at least on my system, scp only supports the short option type, getopts (which also only supports short options) will work fine for this.

The function would look something like this (untested):

scp () {
    # wrapper function to prevent scp of local-only files
    while getopts $options option
        case $option in
            1 | 2 | 4 | 6 | B | C | p | q | r | v)
            c | F | i | l | o | P | S)
                args+=($option "$OPTARG")
    if [[ $1 != *@*:* && $2 != *@*:* ]]
        echo "Local-only copy not permitted"
        echo "to override, use 'command scp ARGS'"
        return 1
    command scp "${args[@]}" "$1" "$2"
share|improve this answer
Nice trick. However, you should consider naming your wrapper something else than "scp" (maybe "scop"). Overloading well-known commands can be a source of tricky problems because most people/scripts will expect the standard functionality. – sleske May 17 '10 at 8:55
Scripts won't use the overloaded function unless you export the function, for example scp () { ... }; export scp – Dennis Williamson May 17 '10 at 10:49
Oh, @sleske, in addition to the export information in my previous comment, this type of thing is the purpose of the command and builtin commands rather than being just a nice trick. – Dennis Williamson May 17 '10 at 11:06
Interesting info about "command" and "builtin", didn't know that. Still, I'd be wary of overloading scp, if just for the "principle of least surprise". Well, maybe a matter of taste... – sleske May 17 '10 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .