Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find all files matching pattern in directory and copy subdirectories and files found

I have a directory structure of the following:

$DIRECTORY/*.xml
$DIRECTORY/*.others
$DIRECTORY/subdir1/*.xml
$DIRECTORY/subdir1/*.others
$DIRECTORY/subdir2/*.xml
$DIRECTORY/subdir2/*.others

And wish to copy all xml files into $OUTPUT_DIRECTORY including the directory structure of $DIRECTORY so the result will be:

$OUTPUT_DIRECTORY/*.xml
$OUTPUT_DIRECTORY/subdir1/*.xml
$OUTPUT_DIRECTORY/subdir2/*.xml

I know find has both -exec and -execdir which will use the output of find as the arguments to the command specified. I can’t however see a way to specify "-execdir but keep subdirectories as the following arguments". Worth noting that the subdirectory can be however n deep.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way to do this purely with find, or will some other CLUs be needed?

find "${DIRECTORY}" -name '*.xml' -execdir cp "{}" "${OUTPUT_DIRECTORY}" \;

>Solution :

You might want to use rsync instead of find:

rsync -amv --include='*/' --include='*.xml' --exclude='*' "$DIRECTORY"/ "$OUTPUT_DIRECTORY"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading