Movatterモバイル変換


[0]ホーム

URL:


 / 
perl-5.40.1
River stage five • 11905 direct dependents • 33393 total dependents
/File::Find

NAME

File::Find - Traverse a directory tree.

SYNOPSIS

use File::Find;find(\&wanted, @directories_to_search);sub wanted { ... }use File::Find;finddepth(\&wanted, @directories_to_search);sub wanted { ... }use File::Find;find({ wanted => \&process, follow => 1 }, '.');

DESCRIPTION

These are functions for searching through directory trees doing work on each file found similar to the Unixfind(1) command.File::Find exports two functions,find andfinddepth. They work similarly but have subtle differences.

find
find(\&wanted,  @directories);find(\%options, @directories);

find() does a depth-first search over the given@directories in the order they are given. For each file or directory found, it calls the&wanted subroutine. (See below for details on how to use the&wanted function). Additionally, for each directory found, it willchdir() into that directory and continue the search, invoking the&wanted function on each file or subdirectory in the directory.

finddepth
finddepth(\&wanted,  @directories);finddepth(\%options, @directories);

finddepth() works just likefind() except that it invokes the&wanted function for a directoryafter invoking it for the directory's contents. It does a postorder traversal instead of a preorder traversal, working from the bottom of the directory tree up wherefind() works from the top of the tree down.

Despite the name of thefinddepth() function, bothfind() andfinddepth() perform a depth-first search of the directory hierarchy.

%options

The first argument tofind() is either a code reference to your&wanted function, or a hash reference describing the operations to be performed for each file. The code reference is described in"The wanted function" below.

Here are the possiblekeys for the hash:

wanted

The value should be a code reference. This code reference is described in"The wanted function" below. The&wanted subroutine is mandatory.

bydepth

Reports the name of a directory only AFTER all its entries have been reported. Entry pointfinddepth() is a shortcut for specifying{ bydepth => 1 } in the first argument offind().

preprocess

The value should be a code reference. This code reference is used to preprocess the current directory. The name of the currently processed directory is in$File::Find::dir. Your preprocessing function is called afterreaddir(), but before the loop that calls thewanted() function. It is called with a list of strings (actually file/directory names) and is expected to return a list of strings. The code can be used to sort the file/directory names alphabetically, numerically, or to filter out directory entries based on their name alone. Whenfollow orfollow_fast are in effect,preprocess is a no-op.

postprocess

The value should be a code reference. It is invoked just before leaving the currently processed directory. It is called in void context with no arguments. The name of the current directory is in$File::Find::dir. This hook is handy for summarizing a directory, such as calculating its disk usage. Whenfollow orfollow_fast are in effect,postprocess is a no-op.

follow

Causes symbolic links to be followed. Since directory trees with symbolic links (followed) may contain files more than once and may even have cycles, a hash has to be built up with an entry for each file. This might be expensive both in space and time for a large directory tree. See"follow_fast" and"follow_skip" below. If eitherfollow orfollow_fast is in effect:

  • It is guaranteed that anlstat has been called before the user'swanted() function is called. This enables fast file checks involving_. Note that this guarantee no longer holds iffollow orfollow_fast are not set.

  • There is a variable$File::Find::fullname which holds the absolute pathname of the file with all symbolic links resolved. If the link is a dangling symbolic link, then fullname will be set toundef.

follow_fast

This is similar tofollow except that it may report some files more than once. It does detect cycles, however. Since only symbolic links have to be hashed, this is much cheaper both in space and time. If processing a file more than once (by the user'swanted() function) is worse than just taking time, the optionfollow should be used.

follow_skip

follow_skip==1, which is the default, causes all files which are neither directories nor symbolic links to be ignored if they are about to be processed a second time. If a directory or a symbolic link are about to be processed a second time,File::Find dies.

follow_skip==0 causesFile::Find to die if any file is about to be processed a second time.

follow_skip==2 causesFile::Find to ignore any duplicate files and directories but to proceed normally otherwise.

dangling_symlinks

Specifies what to do with symbolic links whose target doesn't exist. If true and a code reference, will be called with the symbolic link name and the directory it lives in as arguments. Otherwise, if true and warnings are on, a warning of the form"symbolic_link_name is a dangling symbolic link\n" will be issued. If false, the dangling symbolic link will be silently ignored.

no_chdir

Does notchdir() to each directory as it recurses. Thewanted() function will need to be aware of this, of course. In this case,$_ will be the same as$File::Find::name.

untaint

If find is used intaint-mode (-T command line switch orif EUID != UID orif EGID != GID), then internally directory names have to be untainted before they can bechdir'd to. Therefore they are checked against a regular expressionuntaint_pattern. Note that all names passed to the user'swanted() function are still tainted. If this option is used while not in taint-mode,untaint is a no-op.

untaint_pattern

See above. This should be set using theqr quoting operator. The default is set toqr|^([-+@\w./]+)$|. Note that the parentheses are vital.

untaint_skip

If set, a directory which fails theuntaint_pattern is skipped, including all its sub-directories. The default is todie in such a case.

The wanted function

Thewanted() function does whatever verifications you want on each file and directory. Note that despite its name, thewanted() function is a generic callback function, and doesnot tellFile::Find if a file is "wanted" or not. In fact, its return value is ignored.

Thewanted function takes no arguments but rather does its work through a collection of variables.

$File::Find::dir is the current directory name,
$_ is the current filename within that directory
$File::Find::name is the complete pathname to the file.

The above variables have all been localized and may be changed without affecting data outside of the wanted function.

For example, when examining the file/some/path/foo.ext you will have:

$File::Find::dir  = /some/path/$_                = foo.ext$File::Find::name = /some/path/foo.ext

You arechdir()'d to$File::Find::dir when the function is called, unlessno_chdir was specified. Note that when changing to directories is in effect, the root directory (/) is a somewhat special case inasmuch as the concatenation of$File::Find::dir,'/' and$_ is not literally equal to$File::Find::name. The table below summarizes all variants:

             $File::Find::name  $File::Find::dir  $_default      /                  /                 .no_chdir=>0  /etc               /                 etc             /etc/x             /etc              xno_chdir=>1  /                  /                 /             /etc               /                 /etc             /etc/x             /etc              /etc/x

Whenfollow orfollow_fast are in effect, there is also a$File::Find::fullname. The function may set$File::Find::prune to prune the tree unlessbydepth was specified. Unlessfollow orfollow_fast is specified, for compatibility reasons (find.pl,find2perl) there are in addition the following globals available:$File::Find::topdir,$File::Find::topdev,$File::Find::topino,$File::Find::topmode and$File::Find::topnlink.

This library is useful for thefind2perl tool (distributed with theApp::find2perl CPAN module), which when fed:

find2perl / -name .nfs\* -mtime +7 \  -exec rm -f {} \; -o -fstype nfs -prune

produces something like:

sub wanted {   /^\.nfs.*\z/s &&   (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) &&   int(-M _) > 7 &&   unlink($_)   ||   ($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))) &&   $dev < 0 &&   ($File::Find::prune = 1);}

Notice the_ in the aboveint(-M _): the_ is a magical filehandle that caches the information from the precedingstat(),lstat(), or filetest.

Here's another interesting wanted function. It will find all symbolic links that don't resolve:

sub wanted {     -l && !-e && print "bogus link: $File::Find::name\n";}

Note that you may mix directories and (non-directory) files in the list of directories to be searched by thewanted() function.

find(\&wanted, "./foo", "./bar", "./baz/epsilon");

In the example above, no file in./baz/ other than./baz/epsilon will be evaluated bywanted().

See also the scriptpfind on CPAN for a nice application of this module.

WARNINGS

If you run your program with the-w switch, or if you use thewarnings pragma, File::Find will report warnings for several weird situations. You can disable these warnings by putting the statement

no warnings 'File::Find';

in the appropriate scope. Seewarnings for more info about lexical warnings.

BUGS AND CAVEATS

$dont_use_nlink

You can set the variable$File::Find::dont_use_nlink to0 if you are sure the filesystem you are scanning reflects the number of subdirectories in the parent directory'snlink count.

If you do set$File::Find::dont_use_nlink to 0, you may notice an improvement in speed at the risk of not recursing into subdirectories if a filesystem doesn't populatenlink as expected.

$File::Find::dont_use_nlink now defaults to 1 on all platforms.

Symlinks

Be aware that the option to follow symbolic links can be dangerous. Depending on the structure of the directory tree (including symbolic links to directories) you might traverse a given (physical) directory more than once (only iffollow_fast is in effect). Furthermore, deleting or changing files in a symbolically linked directory might cause very unpleasant surprises, since you delete or change files in an unknown directory.

HISTORY

File::Find used to produce incorrect results if called recursively. During the development of perl 5.8 this bug was fixed. The first fixed version ofFile::Find was 1.01.

SEE ALSO

find(1),find2perl

Module Install Instructions

To install less, copy and paste the appropriate command in to your terminal.

cpanm

cpanm less

CPAN shell

perl -MCPAN -e shellinstall less

For more information on module installation, please visitthe detailed CPAN module installation guide.

Keyboard Shortcuts

Global
sFocus search bar
?Bring up this help dialog
GitHub
gpGo to pull requests
gigo to github issues (only if github is preferred repository)
POD
gaGo to author
gcGo to changes
giGo to issues
gdGo to dist
grGo to repository/SCM
gsGo to source
gbGo to file browse
Search terms
module: (e.g.module:Plugin)
distribution: (e.g.distribution:Dancer auth)
author: (e.g.author:SONGMU Redis)
version: (e.g.version:1.00)

[8]ページ先頭

©2009-2025 Movatter.jp