Unix How-To: Making Better Use of Find

By Sandra Henry-Stocker  Add a new comment

Find is not just useful for locating files, but also for doing things with the files once it finds them. You can find files based on numerous criteria -- their names, their owners, their size, etc. -- and you can then perform some operation on the files from simply listing them to changing permissions or analyzing their contents. You can also make the operation that you specify to occur only if you confirm that you want it to happen after you are prompted. Let's see how this works.

The most standard use of the find command seeks to locate files based on some simple parameter such as their names, owners, size, etc.

find . -name *.sh
find / -user jdoe
find . -mtime -1

You can also pass the list of found files to some other utility using find's -exec clause, xargs or some other tool.

find . -name *.sh -exec chmod g+x {} \;
find / -user jdoe >> /tmp/jdoe.files
find . -mtime -1 | xargs chmod g+r

The {} specification in the first command is replaced for each iteration of the chmod command by the found file's name.

You can, however, elect to take action on each found file on a case by case basis. Using the -ok parameter, the find command will take the action specific (the "head -1" command in the example below) only if you confirm by typing "y" or "Y" at the prompt.

$ find . -type f -ok head -1 {} \;
< head ... ./dir1/file1 >?   n
< head ... ./dir1/file2 >?   n
< head ... ./dir2/file1 >?   y
This file contains a listing of all participants in the August 2010 retreat.  Note that all

In other words, the -ok option takes the place of the exec command and adds the behavior of prompting the user for a response of whether to proceed with the action specified. This can be useful if you, for example, want to avoid counting lines or grepping on the contents of binary files.

ITworld LIVE

Operating SystemsWhite Papers & Webcasts

White Paper

A Comparison of PowerVM and VMware vSphere (4.1 & 5.0) Virtualization Performance

This technical white paper presents benchmark results showing greater VM consolidation ratios than demonstrated in previous benchmarks and demonstrating the extent of the performance lead that PowerVM virtualization technologies deliver over x86-based add-on virtualization products.

White Paper

Consolidating Lotus Domino x86 Workloads on IBM Power Systems

Read the white paper to learn how moving up to Lotus Domino 8.5 and consolidating with IBM Power Servers can help you boost performance results and ROI.

White Paper

Task, workflow & issue management for teams. Try free!

Need a flexible system for managing team tasks, issue tracking, and automating and managing workflow processes? Comindware® Tracker helps you do it all.

Webcast On Demand

Best Practices in Monitoring VMware

The benefits of virtualization are unassailable: increased agility, scale, and cost savings to name a few. However, so too are the monitoring challenges posed by these environments-including complexities, lack of visibility and control, and inefficiency.

Sponsor: Nimsoft

White Paper

How Nimsoft Service Desk Speeds Deployment and Time to Value

For years, many support teams have been hamstrung by their traditional service desk platforms, which require complex, time-consuming coding for virtually every aspect of customization. This complexity makes it costly and difficult for support organizations to adapt-and places an increasingly substantial burden on the agility and efficiency of the business as a whole.

See more White Papers | Webcasts

Ask a question

Ask a Question