0

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!

hzxu
  • 5,753
  • 11
  • 60
  • 95

2 Answers2

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

Check this out.

Parthiban
  • 2,130
  • 2
  • 14
  • 27
2

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
Renato Zannon
  • 28,805
  • 6
  • 38
  • 42
  • This will output: output-folder/headers-folder/*, how can I change it so it copies header files with folder structures from headers-folder into output-folder without have another headers-folder inside output-folder? – hzxu Dec 06 '13 at 04:38
  • I haven't found that to be true on my tests. Rsync version maybe? `rsync --version` gives "version 3.1.0 protocol version 31" to me. – Renato Zannon Dec 06 '13 at 14:48