SCP + FIND from local [SOLVED]

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
zip

SCP + FIND from local [SOLVED]

Post by zip »

Hello! Need a help.

The task is:

Connect from local to remote, find there some files and SCP them to the local from remote.

Now I'm use (and it works well):

scp -r user@server:/some/directory /home/backups

but I want to copy only FIND /some/directory -mtime +1

How I can compose both this command?

P.S.

I know, that exist a metod:

find /some/directory -mtime +1 -exec scp -r /some/directory user@server:/home/backups {} \;

but I need to initialise connection from the local computer, not from remote.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: SCP + FIND from local

Post by Pilosopong Tasyo »

Use ssh to run the find command remotely that will eventually scp those files from the remote machine to the local host, i.e. ssh <user@server> <command>
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
zip

Re: SCP + FIND from local

Post by zip »

Pilosopong Tasyo wrote: Wed Jul 25, 2018 7:03 pm Use ssh to run the find command remotely that will eventually scp those files from the remote machine to the local host, i.e. ssh <user@server> <command>
Yea, that's true, but I can't find the correct syntax.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: SCP + FIND from local

Post by rene »

scp is a bit annoying in this regard seeing as how it doesn't automatically create leading directories; I'd advise using rsync. I.e.,

Code: Select all

rsync -avn --files-from <(ssh user@server find /some/directory -mtime +1 -printf "%P\\\n") user@server:/some/directory /home/backups/
Remove the -n (--dry-run) once satisfied this does what you want.
galaxy0815

Re: SCP + FIND from local

Post by galaxy0815 »

Code: Select all

for i in $(ssh user@server find /some/directory -type d -mtime +1);
do
  scp -r user@server:$i /home/backups
done
In a different way you can run a script job on the server.

Code: Select all

for i in $(ssh user@server ~/find_my_dirs);
do
  scp -r user@server:$i /home/backups
done
In ~/find_my_dirs on the server you can write any search jobs in any scripting language with many lines of code you want.
zip wrote: Wed Jul 25, 2018 5:03 am but I need to initialise connection from the local computer, not from remote.
If you meant which directory is to be searched for relevant files you can do that:

Code: Select all

for i in $(ssh user@server "~/find_my_files $SEARCH_DIR);
do
  scp -r user@server:$i /home/backups
done
zip

Re: SCP + FIND from local

Post by zip »

rene wrote: Fri Jul 27, 2018 8:35 am scp is a bit annoying in this regard seeing as how it doesn't automatically create leading directories; I'd advise using rsync. I.e.,

Code: Select all

rsync -avn --files-from <(ssh user@server find /some/directory -mtime +1 -printf "%P\\\n") user@server:/some/directory /home/backups/
Remove the -n (--dry-run) once satisfied this does what you want.
Thank you! That is just what I need. And this solution works!
Locked

Return to “Scripts & Bash”