unix – find files

(most of the unix shell script have been taken from refs on the web andthe oreilly book unix-power-tools)
  1. Finding files that contains a particular word or string:
    1. $find . -name '*.css' -type f -print | xargs fgrep 'myParagraphStyle' /dev/null
    2. /dev/null – adding this makes grep print the match even if it just matches one file
    3. xargs – splits long set of arguments into chunks; this will prevent the command line exceeding any limits the command might have
    4. fgrep is same as ‘grep -F‘; this makes grep treat the search pattern as fixed strings separated by new lines any of which is to be matched
  2. Using what you find with exec
    1. $find . -name '*.css' -exec echo {} \;
    2. When not using xargs, -exec can be used to get find to execute a command on what it finds; the output of find can be given to another find command
    3. {} is a special argument that contains the name of the file – found by the find command
    4. Semicolon is used to indicate to find the end of the command that find has to execute; the semi-colon must be escaped either \; or ';' so all execs end with a semi-colon
    5. Example copying files
      $find /data/bkups/
      -name 'svn_2007-06-01.[0-9][0-9]'
      -exec sudo cp {}
      /media/bkupserver/svnbkups/svn_2007-06-01/ \;
  3. [placeholder]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.