How to Recursively Copy Files by Extension While Preserving Directory Structure

cpfindlinux

At the Linux command line, I'd like to copy a (very large) set of .txt files from one directory (and its subdirectories) to another.

I need the directory structure to stay intact, and I need to ignore files except those ending in .txt.

Best Answer

You can use find and cpio to do this

cd /top/level/to/copy
find . -name '*.txt' | cpio -pdm /path/to/destdir

(-updm for overwrite destination content.)
Related Question