Minimal Perl for Unix and Linux People: Part 2/Page 7
[previous] [next]
Perl as a (Better) Find Command: Part 2
6.7 A UNIX-LIKE, OS-PORTABLE find COMMAND
When the Perl language is installed, some useful ready-made scripts are installed along with it, which can be of considerable valueÂeven to those who think a Perl is something manufactured by irritated mollusks! As a case in point, we'll discuss find2perl
next, which provides the valuable service of emulating the Unix find command for systems that lack it.
6.7.1 Making the most of find2perl
Many Unix users are accustomed to having the power of find at their disposal. That allows powerful commands like the following to be quickly unleashed to burrow through the file system and process the indicated files:
# Show JPEG files last accessed in 24 hours ( But what's the hapless Windows user or sy stem administrator to do? Is she doomed to wade forever through the "friendly" GUI interface ofSearch>All file and folders
, cutting and pasting its output into delete commands in a cmd.exe
window? Fortunately, after installing Perl (and therefore find2perl
), such people can use find commands like those shown previously.
The procedure is as follows. The find2perl
command is run with options appropriate for the real Unix find command, with its output redirected to a file. That file then contains a custom-crafted Perl script that implements the functionality of the particular find command that the options specified. Then, the script is executed (perhaps after being shipped to a different Perl-equipped system), and its results are obtained.
Here's a sample session, from a DOS-like session on a Windows box:
That's just about all there is to using find-like commands on non-Unix systems, except for taking care to comply with local filename conventions.
For example, the following command on a Unix system creates a script that lists all the filenames of the indicated directories:
But a comparable script destined for a Windows system may have to specify drive key-letters:
Fortunately for those with Unix habits, using slashes (rather than backslashes) in Windows pathnames will work, because Perl automatically handles such OS-specific conversions for you anywhere filenames are expected (see https://TeachMePerl.com/Perl_on_non-Unix_systems.html for additional details).
By the way, the find2perl approach to script-generation works on Unix systems too, and there are cases where it's useful to run find2perl
rather than the real find on Unix. One such situation is described next.
6.7.2 Helping non-Unix friends with find2perl
Do you know someone who could benefit from the power of the Unix find command but doesn't have it on his system, and wouldn't know how to use it even if he did? If so, and Perl is installed on his computer, you can generate an appropriate script on your system using find2perl and email it to your friend for him to use. And you won't even need to be on a Unix system when you do that! All you need is a Perl environment, an understanding of find's syntax, and (for certain find commands) an understanding of the pathname conventions on your friend's system.
Figure 6.2 illustrates the concepts with a fictional interchange.
It's as simple as that! By using this technique of composing a custom file-finding script and supplying it to a friend in need (or to yourself on a non-Unix platform), anybody who has a working Perl installation can reap the benefits of the Unix find commandÂwithout having access to it, or even knowing how to use it.
Are you wondering how well the Unix rm command, shown with the Âexec option in the figure, works on Windows? Perfectly, in fact, because find2perl watches for the frequently used rm command and translates it into its native Perl equivalent, unlink (see table 7.18). But other Unix commands aren't automagically transformed, so as a general rule, commands specified with Âexec must be restricted to those that will be present on th e system that will run the script.
6.8 Summary
The Unix find command is a valuable tool for finding files that have particular attributes. But some attributes are more easily specified with Perl than with find, such as whether a file is readable by the current process, or whether a file's name matches a non-trivial pattern. What's more, Perl uniquely offers tests for some especially useful attributes, such as the -T
operator for identifying text files.
You can easily overcome many of these limitations of find with a little help from Perl. A simple yet effective method is to use find | perl pipelines, where find is used to generate an initial set of pathname s, and Perl to provide additional filtering on specified attributes. In some cases, it's sufficient to let the output of such a pipeline flow to the screen; in others, the output is presented as the argument list for another command, using Shell-level command substitution, or the xargs commandÂthe input-to-argument converter of Unix.
You saw how augmenting find's capabilities with Perl can produce enhanced grep
-like scripts that can automatically disregard non-text files and process directory arguments as requests for recursion into the file system, as users expect. You also saw these techniques used to handle classic problems in Unix file-wrangling, such as the proper treatment of multi-word filenames.
Savvy Unix users are wise to be fond of find's sidekickÂxargsÂwhich is prized for its ability to efficiently allocate arguments to processes. However, they shouldn't let their admiration for it blind them to its in trinsic limitations. As a case in point, the use of xargs in sorting applications can lead to incorrect results, so you should use dependable Perl scripts such as most_recent_file instead.
Finally, although there's generally no Unix-like find command available on other OSs, you can use the find2perl script that's part of the standard Perl distribution to create custom find-like scripts that will run on any Perl-equipped system. With this technique, find-savvy individuals can create find-like scripts that can be used by anyone who has access to Perl.
Excerpted from Minimal Perl for Unix and Linux People by Tim Maher. Copyright © 2006. Used with permission of Manning Publications Co..
[previous] [next]
URL: