spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / programming/perl / minimal_perl

[previous] [next]

Technical Lead
Thomson Reuters (Markets) LLC
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


Perl as a (Better) Find Command

6.2.1 Augmenting find with Perl

A useful way to exploit their individual strengths is to use find to generate an initial set of pathnames and Perl to eliminate those whose files lack some additional attributes.

For example, any of the following commands could be used as the first stage of a pipeline to take advantage of find's ability to locate files according to their size, name, and timestamp attributes:

Then Perl commands, having forms such as these, could be added as the filtering stage in the pipeline:

In these commands, -A, -B, and -C are placeholders for the file-type attributes of interest, and "!" has the effect of negating the meaning of the following test (as it does with find). Note also that or, being weaker in precedence than and (see section 2.4.5), needs parentheses around its arguments.7

Therefore, Example 2 reports files from its input that have attributes A and B, Example 3 reports those having A but not (!) B, and Example 6 reports those having at least one of A, B, or C.

Here is a pipeline based on Example 1 th at lists regular files under the directory /home/ersimpson that contain text. Although find is used for the regular file (-typef) test, Perl must be used for the text-file test that find doesn't provide:

Because many programs work best when users feed them files having exactly these properties, you'll find the Perl component of that pipeline to be useful in many future commands. For this reason, it's worth converting to a script:

We'll use this script later in this chapter, in an example that provides a file-validating service for grep.

As an example of a case using or, the following command lists files that are regular (-type f) and either empty or nontext:

The parentheses surrounding or's conditions in that command are critical, due to the higher precedence of and. Without them, a True result from the first test—signifying emptiness—wouldn't lead to the filename being printed as desired, due to implicit parentheses being placed as follows:

Now that we've discussed how to find filenames by file attributes, we'll turn next to finding filenames according the characteristics of the names themselves.

home / programming / programming/perl / minimal_perl

[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

URL: