2

I am trying to run a simple command that searches for a dedicated file type and once found copy's that file and directory structure to a remote server.

For example:

find Source/ -name *.CR2 | xargs -d '\n' cp --parents -t /STORAGE/location/ 

In this case it takes the found file and copy's it to a separate directory with the parent folder structure. I would like to do this same thing with either rsync or scp.

I have tried some altercations to the code below but had no luck.

find /src/dir/ -name *.CR2 -printf %P\\0 \ | rsync -av -e ssh --files-from=- --from0 /src/dir/ remote@server:/Storage/

Any assistance would be appreciated.

I am in the process of sorting years of family photos and instead of finding them in one directory on my zfs array and rewriting them to another location on the same array (which eats up time) I instead want to find them and send them to a separate machine where they will be sorted by type, run through fdupes, then run through phockup to sort them by date. I am doing it this way in case I screw up, I'll have the orig files safe and sound.

Edit: I would like to thank the ppl that responded to my question. Fortunately I found a solution before you replied, unfortunately I didn't think about how slow the solution would be. So I opted to just do it the way I had been previously doing it.

This was the code that worked for me. Though it required some ssh-key hand offs to work properly so I wouldn't need to enter the password for every file.

find Sorted/ -name *.JPG -exec rsync -a -e ssh --relative '{}' usr@remote:/1/2/3/ ';'

I will try some of the other recommendations mentioned and see if they are faster but as it stands I ended up just making a shell script that runs in the background.

Thanks again!

  • Welcome on U&L! You may be interested in the rsync options used in this Q/A: https://unix.stackexchange.com/q/2161/315749 – fra-san Mar 17 '20 at 23:22
  • find ... -exec ... ';' will execute an instance of rsync for every file to be transferred. Especially if you have a high number of small files, it will be significantly slower than roaima's answer or e.g. rsync -a --include='*.JPG' --include='*/' --exclude='*' source/ host:/path/to/dest/ (from the Q/A I previously linked to). – fra-san Mar 18 '20 at 15:03
  • @fra-san thanks for the info! It makes sense now that you mention it, not sure why I didnt think of it that way sooner. – Star-Bandit Mar 19 '20 at 14:40

2 Answers2

3
find Source/ -type f -name '*.CR2' -print0 |
    rsync --dry-run -av --files-from - --from0 . remote@server:/Storage/

Remove --dry-run when you're happy with what is planning to do.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
2

Below command we can use

 find  path -type f -iname "filename" -exec scp {} destinationhost:/var/tmp/ \;