Script to find all .h file and put in specified folder with same structure?

2.5k views Asked by At

I need a bash script like

headers ~/headers-folder ~/output-folder

so it recursively finds all .h files in ~/headers-folder and put them all in ~/output-folder with the folder hierarchy maintained?

Thanks!

2

There are 2 answers

7
Parthiban On
find /path/to/find -name "*.h" -type f | xargs -I {} cp --parents {} /path/to/destination

Check this out.

2
Renato Zannon On

rsync is great for that too:

rsync --include '*.h' --filter 'hide,! */' -avm headers-folder/ output-folder/

This will copy all the *.h files, and create only the necessary directories.


Example:

mkdir -p headers-folder/{subdir,empty}
touch headers-folder/foo.h
touch headers-folder/subdir/foo.h

tree headers-folder

# headers-folder/
# |-- empty
# |-- foo.h
# `-- subdir
#     `-- foo.h

rsync --include '*.h' --filter 'hide,! */' -avm headers-folder/ output-folder/

tree output-folder

#  output-folder/
#  |-- foo.h
#  `-- subdir
#      `-- foo.h