SCP + sudo

Sometimes you like to copy files from A to B and you have sudo rights on A and B and you do a lot of “cp” to /tmp and chmod and chown’s. This is annoying…

Firstly, I dislike tempfiles.

  • they use space
  • they generate bugs when run in parallel
  • they often are prone to code injection
  • they remain on disk for years

Secondly, unix guys like pipes. While would one do
p <a >b
q <b >c

when you can
p <a |q >c
?

Lastly, I like to type less. So I wrote a small shell script that copies and uses sudo

at the end, I can
scp++ srv1:/dir/file srv2:/dir
using sudo

see comments for the script

1 thought on “SCP + sudo

  1. Laurent Schneider Post author

    SRCSRV=${1%:*}
    SRCPATH=${1#*:}
    SRCDIR=${SRCPATH%/*}
    SRCFILE=${SRCPATH##*/}
    DSTSRV=${2%:*}
    DSTDIR=${2#*:}
    ssh $SRCSRV "cd $SRCDIR;tar cf - $SRCFILE"|
    ssh $DSTSRV "cd $DSTDIR;sudo tar xf -"
    ssh $DSTSRV "ls -l $DSTDIR/$SRCFILE"

Comments are closed.