I want to transfer files which are older then 40 days from remote to local host. I am able to connect the remote server but unable to transfer the files from remote to local server. It results into error which says no files No such file or directory but the file exists on the remote hosts.


file=`sshpass -p "password" ssh username@server_ip "find /arch -type f -ctime -40"`
sshpass -p "password" scp -r  username@server_ip:$file /arch
echo SCP Completed.

Error details:

cp: cannot stat ‘/arch/file1.xls’: No such file or directory
  • Does the error message actually say cp and not scp? – Kusalananda Sep 26 '18 at 12:00
  • did time yourscript work to capture start and end time? – ingroxd Nov 4 '18 at 20:19

You have to take care of one thing or two:

  • Permissions: You might not have the right permissions to read a file;
  • Multiline answer: Your find could give you more than one file as result.

Long story short, probably your problem is not sshpass.

From now, we take for granted that you do not have issues related to permissions.

What you can do is something like that:

files="$(sshpass -p "passwd" ssh -l username rhost "find /arch -type f -ctime -40")"
for file in ${files}; do
  if sshpass -p "passwd" scp "username@rhost:${file}" /arch; then
    printf "SCP Completed\n"

Theoretically, $files will not contain what you can't grab, because find will print in stderr the files (folders) that you cannot reach, so you should manage that in a separate way.

Among all the options, you can merge stderr with stdout and add a condition in your for cycle or save the errors in another var (or file) and cycle them separately.

|improve this answer|||||
  • The for loop errors out as mentioned below and unable to copy the file from remote server. Error details: usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2 – James Sep 26 '18 at 9:28
  • Yes, I'm sorry, I mistakenly used a -l username in scp – ingroxd Sep 26 '18 at 10:39
  • Thanks ingroxd it worked, can we include the timings as well so that i can capture the start time and end time?. – James Sep 27 '18 at 5:29
  • You can just type time yourscript – ingroxd Sep 27 '18 at 6:08

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.