Is it possible to copy files from various subdirectories to one single directory. For example I have a structure that looks like:

+ dir1
  + file1.ext
  + file2.ext
+ dir2
  + file3.ext
  + file4.ext
  + subdir

Currently I am using the following command

find "//mysourcepathdirectory/" -maxdepth 2 -type f -name "*.ext" -exec rsync -vr -e ssh {} user@server:/mypath/videos/ \;

This command of course cause me to log repeatedly to the server. I would like a command that require me to login only one time. I am using an SSH key to avoid the pain but something is telling me that this is not the right approach. I would like to use rsync because only a few files will change over time. Also I would like to avoid copying files in subdirectories below my main path (e.g., subdir in the example above)

1 Answer 1


Yes, there is rsync command with -r (recursive copy) option.

rsync -r /path/in/source/dir*/*.ext /path/to/destination

You could even use --exclude option to exclude sub-directories and directories structures.

rsync -rv --exclude='*/' /path/in/source/dir*/ /path/to/destination/

Or with sub-directories:

rsync -rv --exclude='*/*' /path/in/source/dir*/ /path/to/destination/

You could also use scp command as well

scp -r /path/in/source/dir*/*.ext /path/to/destination
  • Thanks; I dont want to copy the directory structure; in my example above I would like file1.ext, file2.ext, file3.ext, and file4.ext to be in the same destination directory if that make sense. I though rsync -r will copy the source directory structure.
    – rjss
    Aug 9, 2017 at 17:01
  • Perfect! Thanks! I used your command and worked but not sure the --include switch is doing anything here since there is no parameter (value) associated with it. Not sure if I am missing something. However in the other hand I was able to use the --exclude option since all my subdirs that I want to avoid have the same pattern. My command now looks like: rsync -vr --exclude misc/ -e ssh //mysourcepath/*/*.mp4 user@server:/destpath/myvidtest/
    – rjss
    Aug 9, 2017 at 17:21

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.