AIX > Tips & Techniques > Systems Management

Search Smarter With Find

Search Smarter with the Find Command

Bookmark and Share Print Email

If you need to search through directories for a particular file—or a subset of files—then the find command is your friend. The find command has many flags and options, providing flexibility on the command line. You can search for files that:

  • Match a pattern
  • Haven’t changed in the past X days
  • Were updated in the past hour
  • Have certain permissions

And a combination of flags can be used to narrow the search. You can also perform an action on the files or directories that match the search criteria. For example, find can be used to make a backup, or to move files to an archive directory. Want to delete log files (*.log) over 30 days old? No problem. Need to fix permissions on files without a valid owner? Easy.

Flexible and Powerful

The find command is very flexible, but the syntax can be daunting. Maybe that’s why some people avoid using it and stick to commands such as ls -l. But find gives you much more power, and it’s not that difficult to learn, once you understand the basic building blocks it uses.

If the find command seems too cumbersome, break it down into four elements:

  1. Specify starting directory
  2. Display the results
  3. Narrow your search pattern, and
  4. Take action on the files you’ve found

Quick Find

First, the basics: Begin by specifying a starting directory. Here’s how to list all files under the current directory you’re in:

find .

How easy is that? As find does a search recursively through the directory tree, you can list all files on your host by starting from the root directory (/):

find /

You can nominate any directory as the starting point, and use an absolute path:

find /home/frederick/logs

Or a relative path:

find ../logs

You’re not restricted to a single starting directory, either. Here’s how to use a few directories as starting points:

find /usr/java5 /home/bill/bin /usr/java6

Keeping Out of the Nest

Searching for files just within a file system might seem tricky if it has nested file systems under it. For instance, suppose you have these file systems:

/usr
/usr/IBM

To search only within /usr and not the /usr/IBM file system, just use the -xdev flag:

find /usr -xdev

This option is very helpful, especially if you’re doing a search in the root file system (/).

Anthony English is an AIX specialist based in Sydney, Australia.

Advertisement

Buyers Guide

Search our new 2012 Buyer's Guide.

Search Companies


Search Products


Advertisement

Related Articles