0

Since I'm often copying a varying number of specific files to a (rather convoluted) destination directory over the network, my life would be so much easier if I could use cp's -t flag: With it, you can specify the target directory "at the beginning"—and not as the final argument.

That way, I could CTRL-R to my copy command and then CTRL-W the previously copied files, all the while leaving the (complex) setup and destination destination system and directory no matter how many files there were earlier.

But I can't find any fitting flags, neither for scp nor for rsync. (I'd use either, if one of them offered this option.)

Do either scp or rsync offer anything akin to cp -t?

In my desperation, I've even toyed around with bash's history control, the argument extraction mechanisms in particular: Unfortunately, it doesn't seem to be possible to specify ranges in the form of "the x-th argument to the last-but-one", which I would need since I don't always have the number of arguments (unless I count…).

  • 1
    Mount the remote destination via sshfs and use cp with your -t flag :) – ivanivan Nov 17 '17 at 4:14
  • 1
    Write your command using variables, then just change the variable value and recall the (unchanged) command. f='a b' and scp ... $f ... then f='c d e' and recall (assuming no whitespace in filenames). But a small function is best, as in answer by Olorin. – meuh Nov 17 '17 at 7:49
5

Maybe you can define a function that wraps around scp:

scpt () {
    scp "$@" user@host:/some/convoluted/path
}

And then do

scpt file1
scpt /dir1/file2

Also, with history interaction, you can get the last argument of the previous scp command with !scp:$, so you could also do:

scp /dir1/file2 !scp:$

(since the target directory is usually the last)

  • Thanks, that's probably the "easiest" way to solve all my troubles. I hadn't thought of using !scp:$; not sure if it's universal enough in my case, but still good advice! – NicApicella Nov 17 '17 at 10:40

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.