Linux – Copy files recursively preserving directory structure

When you want to recursively copy files from one directory to another, use find and pipe its output to cpio.

find . -name '*.jpg' | cpio -pdm /path/to/<dest_dir>

This will find all files with extension ‘jpg’ and copy them to <dest_dir> directory. cpio -pdm copies files to destination directory and also creates the directory structure.

find . -name '*.jpg' | cpio -pdumv /path/to/<dest_dir>

This will copy all ‘jpg’ files and replace any existing files unconditionally (-u). -v is the verbose flag.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s