问题
I have a directory which has a lot of header files(.h) and other .o and .c files and other files. This directory has many nested directories inside. I want to copy only header files to a separate directory preserving the same structure in the new directory.
cp -rf oldDirectory newDirectory
will copy all files.
I want to copy only header files.
回答1:
(cd src && find . -name '*.h' -print | tar --create --files-from -) | (cd dst && tar xvfp -)
You can do something similar with cpio if you just want to hard link the files instead of copying them, but it's likely to require a little mv'ing afterward. If you have lots of data and don't mind (or need!) sharing, this can be much faster. It gets confused if dst needs to have a src in it - this is, if it isn't just a side effect:
- find src -name '*.h' -print | cpio -pdlv dst
- mv dst/src/* dst/.
- rmdir dst/src
回答2:
this one worked for me:
rsync -avm --include='*.h' -f 'hide,! */' . /destination_dir
from here
回答3:
cp -rf oldDirectory/*.h newDirectory
or something like (depending on the actual paths)
find oldDirectory -type f -name "*.h" -print0 | xargs -file cp file newDirectory
来源:https://stackoverflow.com/questions/10176849/how-can-i-copy-only-header-files-in-an-entire-nested-directory-to-another-direct