Member Sign In
Not a member?

A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.


It's fast and free.

Webmonkey is a property of Wired Digital.
processing...
Join Webmonkey

Please send me occasional e-mail updates about new features and special offers from Wired/Webmonkey.
Yes No

Please send occasional e-mail offers from Wired/Webmonkey affiliated web sites and publications, and carefully selected companies.
Yes No

I understand and agree that registration on or use of this site constitutes agreement to Webmonkey's User Agreement and Privacy Policy.
Webmonkey is a property of Wired Digital.
processing...

Retrieve Sign In

Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.

or
Webmonkey is a property of Wired Digital.
processing...

Welcome to Webmonkey

A private profile page has been created for you.
As a member of Webmonkey, you can now:
  • edit articles
  • add to the code library
  • design and write a tutorial
  • comment on any Webmonkey article
Close
Webmonkey is a property of Wired Digital.

Sign In Information Sent

An e-mail has been sent to the e-mail address registered in this account.
If you cannot find it in your in-box, please check your bulk or junk folders.
Sign In
Webmonkey is a property of Wired Digital.

Find

/skill level/
/viewed/
0 Times

find searches through directory trees beginning with each pathname and finds the files that match the specified condition(s). You must specify at least one pathname and one condition.

Structure:find pathname(s) condition(s)

A wise Unix-master friend once told me, "Never forget that in Unix find is your friend." He was right. The find command is useful for searching for particular files, directories, and patterns in your system.


There are several handy conditions you can use to find exactly what you want. The -name condition will find files whose names match a specified pattern. The structure for the name condition is:

find pathname -name pattern


The condition -print will print the matching files to the pathname specified. -print can also be used in conjunction with other conditions to print the output.

If you wanted to find all the files named favorites.html in the directory john_hughes, then you'd do this:


find /john_hughes -name favorites.html -print

This looks through the directory john_hughes and finds all the files in that directory that contain favorites.html, then prints them to the screen. Your output would look like this:

 /john_hughes/sixteen_candles/favorites.html 
/john_hughes/favorites.html 
/john_hughes/breakfast_club/favorites.html 

All meta-characters (!, *, ., etc.) used with -name should be escaped (place a \ before the character) or quoted. Meta-characters come in handy when you are searching for a pattern and only know part of the pattern or need to find several similar patterns. For example, if you are searching for a file that contains the word "favorite," then use the meta-character

  • to represent matching zero or more of the preceding characters. This

will show you all files which contain favorite.

find /john_hughes -name '*favorite*' -print

This looks through the directory john_hughes and finds all the files in that directory that contain the word "favorite." The output would look like this:

 /john_hughes/sixteen_candles/favorites.html
/john_hughes/favorites.html
/john_hughes/least_favorites.html
/john_hughes/breakfast_club/favorites.html
/john_hughes/favorite_line.html

The -user condition finds files belonging to a particular user ID or name. For more conditions, use the online Unix manual (man).</pre>

If you wanted to find all the files in john_hughes that are owned by the user clarence, you would do this:

find /john_hughes -user clarence

Your output would look something like this:

/john_hughes/criminals/judd_nelson.html 
/john_hughes/geeks/anthony_michael_hall.html 

You can also use find to do recursive operations for commands that don't have recursive options. For example, if you want to <a href="/webmonkey/reference/unix_guide/grep.html">grep</a> an entire directory tree, you could use find with grep to do this. If you wanted to find all the index.html files in the john_hughes directory tree, you would type:

 find /john_hughes/ -print |xargs grep '*index.html' 

In this example, you are looking through the john_hughes directory tree for all files named index.

Back to Unix Guide

  • This page was last modified 00:54, 16 May 2008.
Edit this article
Reddit Digg
 
Subscribe now

Special Offer For Webmonkey Users

WIRED magazine:
The first word on how technology is changing our world.

Subscribe for just $10 a year