0

I have a need to regularly copy files from a specific set of source sub directories (100's of them) into a 'flat" directory structure, i.e. i want all the files from the multiple source directories in a single destination directory. I can't seem to find a way of copying that can look into the source sub directories & copy the files that doesn't re-create the sub-directory folder structure in the destination directory.

Any help appreciated.

user3047191
  • 111
  • 1
  • 1
  • 4

1 Answers1

0
sourcedir=/root/of/subdirectory/set
destdir=/where/the/files/go

find $sourcedir -type f -print | while read file; do cp $file $destdir; done

or (prevent overwrites)

find $sourcedir -type f -print | while read file; do base=$(basename $file); test -f $destdir/$base || cp $file $destdir; done

Note this will not work if any of the names of the files or subdirectories in $sourcedir contain spaces.

Guntram Blohm
  • 9,667
  • 2
  • 24
  • 31
  • `find $sourcedir -type f -exec cp '{}' $destdir \;` is the shorthand :) – FrankH. Dec 12 '13 at 10:08
  • Right :) I prefer the loop method though, because in most cases, i want to do something like the no-overwrite-check, and it's much easier to get that right in a loop than in find's strange -exec syntax. – Guntram Blohm Dec 12 '13 at 13:40