Difference between revisions of "Findutils"

From Christoph's Personal Wiki
Jump to: navigation, search
 
Line 23: Line 23:
 
* search for files modified within the last day that match a certain filenaming pattern and remove them (i.e. execute  rm):
 
* 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!!
 
  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]], [[Gimp|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, "<tt>resized</tt>". Then you would execute the following commands:
 +
 +
<pre>find . -maxdepth 1 -name '*.jpg' -type f -exec convert -resize 800x600 {} resized/{} \;</pre>
 +
 +
It is also possible to have the above commands run recursively through a directory and its sub-directories like so:
 +
 +
<pre>find . -follow -name '*.jpg' -type f -exec convert -resize 800x600 {} ../resized/{} \;</pre>
 +
 +
Note that the programme <tt>convert</tt> 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 <tt>ls</tt>-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):
 +
 +
<pre>find some_directory/ -size +2000k -ls</pre>
 +
 +
which will only list files over 2000 kb (2 MB).
 +
 +
=== Finding files containing a string in a directory hierarchy ===
 +
In this example, all <tt>.php</tt> files will be searched for the string "<tt>MySQL</tt>" (case-insensitive with <tt>-i</tt>) and the line numbers will also be returned (using <tt>-n</tt>):
 +
 +
<pre>find . -name '*.php' -type f | xargs grep -n -i 'MySQL'</pre>
  
 
== External links ==
 
== External links ==

Revision as of 07:56, 2 November 2006

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 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'

External links