Search This Blog

Showing posts with label mv. Show all posts
Showing posts with label mv. Show all posts

Thursday, June 18, 2009

Using 'find', 'exec' and 'mv' - A More Complex Example

This example follows on from my previous example of using find in conjunction with the nifty -exec command. It builds on my previous example as it adds a bit of complexity through the use of the 'mv' command and parameters AFTER the '{}'. The commands are listed in bold below along with the results.

First up I run the 'find' command to get a listing of all the '*.tar' files within the current directory structure.

The second bold command shows how I use the results of the listing from the first command to feed into the 'mv; command. What I'm doing here is basically saying;
- Find all the files in the current directory tree that end in *.tar.
- For each file name thats found we get the path to it.
- then we call 'exec' and pass in the file path to the 'mv' command.

So the end result is to find all of the *.tar files and then to move them to the directory of my choice (temp_burn in this case).

mkopka@random:~/Sandbox$ find . -name '*.tar'
./lisasoft/DefaultProject.tar
./terrapages/TerraLoad.tar
./terrapages/TPLoc-Demo.tar
./terrapages/TPAPI.tar
./terrapages/OpenGIS-API.tar
./terrapages/TerraDirectory.tar
./terrapages/TerraAddressHelper.tar
./terrapages/FreeMaps.tar
./sa.deh.tar
./telstra/WebFOOT/lib/ldapSchemaUpdate.tar
./telstra-WEBFOOT.tar
./sa.epa/eelf.tar

mkopka@random:~/Sandbox$ find . -name '*.tar' -exec mv {} ~/temp_burn/ \;

As you can see above, the location to 'mv' the files to is given after the '{}'. So, in this situation you can think of the '{}' as simply a placeholder for the path from the command above it. This means that you could have the '{}' used as a parameter anywhere within whatever command you are using.