3

I often do this:

scp some_file really@long.server.name.com

I'm annoyed by this because I always copy-paste this line, and then I need to edit the middle to change the file I want to upload.

Is it possible to tinker with this line so the file name will be at the end and it'll still work?

Basically I want any string of characters that I can paste into a shell, then type a file name and have it upload the file with scp to the server.

1

One of possible solutions:

(read FILE_NAME; scp "$FILE_NAME" really@long.server.name.com) <<< some_file
3

Why not make a super-simple script? Just edit a file named say myscp containing

#!/bin/bash
scp $1 really@long.server.name.com

and then chmod +x myscp, so that later you can simply

./myscp some_file

This is all assuming your myscp script is located at the current directory, otherwise add the path

/path/to/myscp some_file

or better yet, add the path of the location and this and -hopefully- future scripts to your $path.

1

Add to the end of your .bashrc or equivalent for your shell:

my_function_name() {
    if [ -r "$1" ]; then
        scp "$1" really@long.server.name.com
    else
        echo "File does not exists or can not be read, $1" >&2
        return 1
    fi
}

Change my_function_name to your needs.

Now you can just invoke the function by name and pass a file as argument and it will be tested for read access and run scp with it or quit if can not read it.

Note that I used return instead of exit because it will be loaded by your interactive shell, and if you use exit the shell will terminate.

1

Use ssh_config to shorten your really@long.server.name.com. Create a file ~/.ssh/config with

Host alias
  HostName long.server.name.com
  User really

and then connect simply using scp some_file alias.

0

No, you can't change the order of the parameters without wrapping it in a shell script, but you could...

R='really@long.server.name.com'
scp some_file $R
scp other_file $R
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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