Search This Blog

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.

Monday, June 15, 2009

Using 'find' and 'exec'

I needed to go through all of the files in my project workspace just recently and in particular I was looking for certain string. Now in Windoze world I would've done a GUI search for text in file. How do I do the same in Linux world. Well this is one way:

find . -exec grep "BLABLABLA" '{}' \; -print

What this does is as follows:
  1. First find all files starting at '.', ie: at my current location
  2. For every file that I find execute the command grep "BLABLABLA" '{}', where '{}' contains the results of the find command (in this case a listing of every file path.
  3. 'print' out any resulting files that have this text in it.
Sweet. All done and the results I get are something along the lines of:

mkopka@random:~/$ find . -exec grep "year.development" '{}' \; -print
<center>Copyright © @year.development@ TerraPages International</center>
./src/html/common/footer.jsp.in
<filter token="year.development" value="2006, 2007" />
./build.xml


Which shows me that there are two files 'footer.jsp.in' and build.xml which have the text that I am looking for, and it also gives me the path to each file and the context of the text Im searching for is printed out.

Some links to more info:
- http://en.wikipedia.org/wiki/Find
- http://www.ibm.com/developerworks/aix/library/au-unix-find.html