Difference between revisions of "Findutils"

From Christoph's Personal Wiki
Jump to: navigation, search
(Examples)
(Examples)
Line 13: Line 13:
 
* Here is an example operation to make all HTML files in the subdirectory <code>htdocs</code> readable by all using <tt>find</tt> and <tt>xargs</tt>. This is a typical example of how find and xargs are used with other utilities to provide powerful directory traversal capability:
 
* Here is an example operation to make all HTML files in the subdirectory <code>htdocs</code> readable by all using <tt>find</tt> and <tt>xargs</tt>. This is a typical example of how find and xargs are used with other utilities to provide powerful directory traversal capability:
 
  find htdocs -name '*.html' -print0 | xargs -0 chmod a+r  
 
  find htdocs -name '*.html' -print0 | xargs -0 chmod a+r  
 +
 +
*Find all files in a given path a print them to the STDOUT (one file/line):
 +
find . -name \*.htm -printf %f\\n
  
 
* find all files in a given path containing the word "<code>EXAMPLE</code>" in them (time the difference):
 
* find all files in a given path containing the word "<code>EXAMPLE</code>" in them (time the difference):

Revision as of 02:18, 20 March 2007

The GNU Find Utilities (or Findutils) are the basic directory searching utilities of the GNU operating system. These programs are typically used in conjunction with other programs to provide modular and powerful directory search and file locating capabilities to other commands.

Findutils tools

The tools supplied with this package are:

  • find - search for files in a directory hierarchy
  • locate - list files in databases that match a pattern
  • updatedb - update a file name database
  • xargs - build and execute command lines from standard input

Examples

Note: Adapted from the GNU Project homepage.

  • Here is an example operation to make all HTML files in the subdirectory htdocs readable by all using find and xargs. This is a typical example of how find and xargs are used with other utilities to provide powerful directory traversal capability:
find htdocs -name '*.html' -print0 | xargs -0 chmod a+r 
  • Find all files in a given path a print them to the STDOUT (one file/line):
find . -name \*.htm -printf %f\\n
  • find all files in a given path containing the word "EXAMPLE" in them (time the difference):
time find /home/foo/bar -type f -exec grep EXAMPLE {} \; -print
time find /home/foo/bar -type f|xargs grep EXAMPLE   # <- faster
  • find (list) all files in a given path having "Fo*" in their filename:
find bar/ -name 'Fo*' -exec ls -l {} \+ 
  • search for files modified within the last day that match a certain filenaming pattern and remove them (i.e. execute rm):
find . -name "[Tt][Ee][Ss][Tt]*" -mtime -1 -exec rm {} \;  # CAREFUL!!

Bulk image resize

If you are like me and have a high resolution digital camera, it is often necessary to resize the images before emailing them to friends and family. It is, of course, possible to manually resize them using Adobe Photoshop, The Gimp, or any other image editing programme. However, it is possible to automate this task using simple command line tools.

For an example, say you want to resize all of the jpeg images in your current directory to 800x600 and place them in a sub-directory called, "resized". Then you would execute the following commands:

find . -maxdepth 1 -name '*.jpg' -type f -exec convert -resize 800x600 {} resized/{} \;

It is also possible to have the above commands run recursively through a directory and its sub-directories like so:

find . -follow -name '*.jpg' -type f -exec convert -resize 800x600 {} ../resized/{} \;

Note that the programme convert is part of the ImageMagick suite and you will need to have it installed to use the above commands (it is, by default, in SuSE Linux).

Tracking down large files

Sometimes it is necessary to find files over a certain size and it can be somewhat tedious ls-ing through your many directories. The following command will list only those files over a certain size and only within the specified directory (and sub-directories):

find some_directory/ -size +2000k -ls

which will only list files over 2000 kb (2 MB).

Finding files containing a string in a directory hierarchy

In this example, all .php files will be searched for the string "MySQL" (case-insensitive with -i) and the line numbers will also be returned (using -n):

find . -name '*.php' -type f | xargs grep -n -i 'MySQL'

Log rotate

We don't we run-away logging to fill up our /var partition, so we can archive (tar) old logs (e.g. older than one day):

find /var/log/ -name "*.log" -mtime +1 -exec bzip2 -z '{}' \;

and then delete old tars (e.g. older than 30 days):

find /var/log -name "*.bz2" -mtime +30 -exec rm '{}' \;

External links