Terminal 101: Use the Find Command to Move a Large Batch of Files
Posted 04/09/2012 at 11:41am
| by Cory Bohon
Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!
Last week, we showed you how to use Terminal’s find command to search for misplaced items on your Mac. This week, we’ll take it a bit further by showing you how to use the find command in conjunction with other commands to copy files found in your search criteria to another location on your Mac. This can be especially handy when you need to round up a large batch of files (like your music) to copy them to an external hard drive or move them to one local folder.
Using the -mtime modifier

The find command has many modifiers, but there’s one in particular that is especially handy to have: -mtime. With this modifier, the find command will only search for files that have been modified in a certain number of days.
find $HOME -iname "*.html" -mtime -2
The above command will search for files containing a .html file extension in your home directory that have been modified in the past 2 days only. Using the tips that we mentioned last week, you can easily change the file extension to jpg, tiff, png, mp3, or something else.
Executing a command on the search results

Another modifier for the find command that we will put to use is the -exec (or execute) command. Using this command, you can execute another Terminal command that will be run for each search result found. For instance, take the following command:
find $HOME -iname "*.jpg" -mtime -2 -exec cp {} ~/Desktop/archive \;
This command will search your home directory for any JPEG files that have been modified in the past two days, and will then use the -exec command to copy (note the cp command) the files that’s to an existing folder called “archive” on the Desktop.
You can also replace the cp (copy command) with the mv (move command) to have the found files moved to any location that you choose. Remember, the folder you wish to save the items to must already exist, otherwise there will be copying issues.

Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.