19

I am writing a bash script to copy a file from a remote server, to my local machine. I need to check to see if the file is available, so I can take an alternative action if it is not there.

I know how to test if a local file exists, however, using scp complicates things a bit. Common sense tells me that one way would be to try to scp the file anyhow, and check the return code from the scp command. Is this the correct way to go about it?

If yes, how do I test the return code from the scp invocation?

4 Answers 4

17

using ssh + some shell code embedded in the cmd line; use this method when you need to take a decision before the file transfer will fail;

ssh remote-host 'sh -c "if [ -f ~/myfile ] ; then gzip -c ~/myfile ; fi" ' | gzip -dc > /tmp/pkparse.py

if you want to transfer directories you may want to "tar"-it first

if you want to use scp you can check the return code like this:

if scp remote-host:~/myfile ./ >&/dev/null ; then echo "transfer OK" ; else echo "transfer failed" ; fi

it really depends on when its important for you to know if the file is there or not; before the transfer starts (use ssh+sh) or after its finished.

Sign up to request clarification or add additional context in comments.

1 Comment

It's probably more important to consider if control is required on the client side rather than the server side, so that processing can continue. This is very commonly done for "pulls" by remote clients based upon a "token" file.
1

well, since you can use scp you can try using ssh to list and see if the file is their or not before proceeding.

Comments

1

You have to run ssh command and check for file or directory before doing a scp. I am using stat command to verify existance of file/directory. The script that I wrote to check and transfer file is

if ssh write_remote_host_here "stat write_filename_here"; then
   echo "file already exist"
else
   scp write_filename_here write_remote_host_here:destination_directory_here

If SSH does not work this way and ask for password consider using sshpass before any ssh or scp command and you would have to write like

sshpass -p write_password_here ssh [email protected] "stat write_filename_here"

Don't forget to install sshpass on your machine via brew or apt

Additionally if the file you are checking does not exist in same directory than check file like this

if ssh write_remote_host_here "cd /destination_directory_path_here; stat write_filename_here"; then

Comments

0

An industrial solution will deploy an id file for automated access to the remote host, like

REM File checkRemoteFileExists.cmd

set SSH=...ssh.exe
set user=...
set host=...
set idfile="...id_rsa"

set f="%1"

set /A fileExists=0
for /F %%i in ('%SSH% -i %idfile% %user%@%host% "ls %f%"') DO if "%%i"==%f% set /A fileExists=1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.