Marco Prins
Marco Prins

Reputation: 7419

How do I copy a file to local machine while logged in to remote ssh server

I have a process (which I have put into an alias in .bash_profile) to get a log file from my remote ssh server, save it to my local machine and then empty the remote file.

At the moment, my process has these two commands:

scp [email protected]:public/proj/current/log/exceptions.log "exceptions $(date +"%d %b %Y").log"

to download the file to my local machine, and then

ssh [email protected] "> /public/proj/current/log/exceptions.log"

to clear the remote file. Doing it this way means that I'm logging in via ssh twice. I want this to be effecient as possible, so I want a way to only login once, do both operations, and then logout.

So if I can find a way to send the file to my local machine from the command-line of the server, I can do this:

ssh [email protected] "[GET FILE]; > /public/proj/current/log/exceptions.log"

Is there a way to do this? And if not, is there any other way to do achieve my goal while logging in once only?

Upvotes: 2

Views: 2182

Answers (1)

John Zwinck
John Zwinck

Reputation: 249592

ssh [email protected] "cat /public/proj/current/log/exceptions.log &&
  > /public/proj/current/log/exceptions.log" > "exceptions $(date +"%d %b %Y").log"

This works by catting the entire file to stdout which will flow through as the stdout of ssh, then truncating the file remotely (assuming cat succeeded).

Upvotes: 3

Related Questions