Copy files by extension that keeps directory structure

Some more essential Linux knowledge, written down to document it.


Copy only files by their type (or file extension), while preserving the directory structure.


I would recommend using rsync that copies files recursively and allows filtering them by their file name. The challenge is configuring a set of filter rules.

Command line:

To copy everything: rsync -avim –include=’*/’ –include=’*.doc’ –exclude=’*’ /source /destination


Copy recursively. Go down into directory structure.

-a (Archive) Copy everything (files, directories, symbolic links, devices), recursively and preserve applicable file attributes (permissions, time, ownership).
-m (Prune) Do not create destination directories that would be empty as they do not contain files that with intended file extension.
-i (Summary) Print change-summary for all copied files.
–include=’*/’ Thats the point. Prevents the exclude parameter (shown and explained below) from skipping only unwanted files, but not from inadvertently skipping subdirectories.
–include=’*.doc’ (Include rule) Copy files of intended file extension, for exemple, ‘*.doc’ or ‘*.txt’. You may repeat this parameter if more that one file type should be copied. Pay attention to the quotes.
–exclude=’*’ Ignore all other files that were not included by extension.

Optionally, you may replace the “-a” option by “-rlptgoD” if you want to specify yourself which file types (file, directory, device, symbolic link) should be copied and while file attributes (time, ownership, permissions) should be preserved. But do not forget the “-r” for recursive file discovery.