Find and Copy Files using bash find -exec Command

Written by
Date: 2012-03-05 16:40:00 00:00

This is a very useful command, find and I have written about it before, today I want to share with you how to use it to find and copy files.

What makes it wonderful, is that is can locate files all over your disk, and copy all those that match a specific criteria to a given folder.

Pretty useful, if you want in example, send all log files to a given person.

Continuing with the example above, lets find all .log files and copy them to a folder.

First create that folder.

mkdir /tmp/log-files

Then find and copy all .log files there.

find . -name \*.log -print0 | xargs -I{} -0 cp -v {} /tmp/log-files

Now you can send the files to anyone you want. To finish this small article, lets see how to compress them, so we can send them.

tar -cvzf log-files.tar.gz /tmp/log-files/

Now, just send log-files.tar.gz file by email or using the method you may prefer.

As this is a bash command, you can use it in Mac OS X or with favorite flavor of Linux.