How to sudo over scp

Today, I like to copy a file from a server to a different server using command line in Unix. Since I like to put the file at a location that require root permission, if I do it using scp, I have to split it into two commands, i.e.,

1.) scp the file to a remote location
2.) copy the file to destination location via sudo and ssh

However, I like to do it in one single command. After trial and error for few times, finally I come up with a solution to solve this computer server problem.

Suppose I want to copy a file from local server:/my_source_file to remoteserver.com:/root/file, I can simply do everything in one single command:

ssh remoteserver.com sudo sh -c '"cat > /root/file"' < /my_source_file

This computer problem is easy and piece of cake.

--Derrick

Our sponsors: