CINXE.COM

File::Find - Traverse a directory tree. - Perldoc Browser

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>File::Find - Traverse a directory tree. - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/File::Find"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.39.10</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/File::Find">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/File::Find">5.40.1</a> <a class="dropdown-item" href="/5.40.0/File::Find">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/File::Find">5.38.3</a> <a class="dropdown-item" href="/5.38.2/File::Find">5.38.2</a> <a class="dropdown-item" href="/5.38.1/File::Find">5.38.1</a> <a class="dropdown-item" href="/5.38.0/File::Find">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/File::Find">5.36.3</a> <a class="dropdown-item" href="/5.36.2/File::Find">5.36.2</a> <a class="dropdown-item" href="/5.36.1/File::Find">5.36.1</a> <a class="dropdown-item" href="/5.36.0/File::Find">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/File::Find">5.34.3</a> <a class="dropdown-item" href="/5.34.2/File::Find">5.34.2</a> <a class="dropdown-item" href="/5.34.1/File::Find">5.34.1</a> <a class="dropdown-item" href="/5.34.0/File::Find">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/File::Find">5.32.1</a> <a class="dropdown-item" href="/5.32.0/File::Find">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/File::Find">5.30.3</a> <a class="dropdown-item" href="/5.30.2/File::Find">5.30.2</a> <a class="dropdown-item" href="/5.30.1/File::Find">5.30.1</a> <a class="dropdown-item" href="/5.30.0/File::Find">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/File::Find">5.28.3</a> <a class="dropdown-item" href="/5.28.2/File::Find">5.28.2</a> <a class="dropdown-item" href="/5.28.1/File::Find">5.28.1</a> <a class="dropdown-item" href="/5.28.0/File::Find">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/File::Find">5.26.3</a> <a class="dropdown-item" href="/5.26.2/File::Find">5.26.2</a> <a class="dropdown-item" href="/5.26.1/File::Find">5.26.1</a> <a class="dropdown-item" href="/5.26.0/File::Find">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/File::Find">5.24.4</a> <a class="dropdown-item" href="/5.24.3/File::Find">5.24.3</a> <a class="dropdown-item" href="/5.24.2/File::Find">5.24.2</a> <a class="dropdown-item" href="/5.24.1/File::Find">5.24.1</a> <a class="dropdown-item" href="/5.24.0/File::Find">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/File::Find">5.22.4</a> <a class="dropdown-item" href="/5.22.3/File::Find">5.22.3</a> <a class="dropdown-item" href="/5.22.2/File::Find">5.22.2</a> <a class="dropdown-item" href="/5.22.1/File::Find">5.22.1</a> <a class="dropdown-item" href="/5.22.0/File::Find">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/File::Find">5.20.3</a> <a class="dropdown-item" href="/5.20.2/File::Find">5.20.2</a> <a class="dropdown-item" href="/5.20.1/File::Find">5.20.1</a> <a class="dropdown-item" href="/5.20.0/File::Find">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/File::Find">5.18.4</a> <a class="dropdown-item" href="/5.18.3/File::Find">5.18.3</a> <a class="dropdown-item" href="/5.18.2/File::Find">5.18.2</a> <a class="dropdown-item" href="/5.18.1/File::Find">5.18.1</a> <a class="dropdown-item" href="/5.18.0/File::Find">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/File::Find">5.16.3</a> <a class="dropdown-item" href="/5.16.2/File::Find">5.16.2</a> <a class="dropdown-item" href="/5.16.1/File::Find">5.16.1</a> <a class="dropdown-item" href="/5.16.0/File::Find">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/File::Find">5.14.4</a> <a class="dropdown-item" href="/5.14.3/File::Find">5.14.3</a> <a class="dropdown-item" href="/5.14.2/File::Find">5.14.2</a> <a class="dropdown-item" href="/5.14.1/File::Find">5.14.1</a> <a class="dropdown-item" href="/5.14.0/File::Find">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/File::Find">5.12.5</a> <a class="dropdown-item" href="/5.12.4/File::Find">5.12.4</a> <a class="dropdown-item" href="/5.12.3/File::Find">5.12.3</a> <a class="dropdown-item" href="/5.12.2/File::Find">5.12.2</a> <a class="dropdown-item" href="/5.12.1/File::Find">5.12.1</a> <a class="dropdown-item" href="/5.12.0/File::Find">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/File::Find">5.10.1</a> <a class="dropdown-item" href="/5.10.0/File::Find">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/File::Find">5.8.9</a> <a class="dropdown-item" href="/5.8.8/File::Find">5.8.8</a> <a class="dropdown-item" href="/5.8.7/File::Find">5.8.7</a> <a class="dropdown-item" href="/5.8.6/File::Find">5.8.6</a> <a class="dropdown-item" href="/5.8.5/File::Find">5.8.5</a> <a class="dropdown-item" href="/5.8.4/File::Find">5.8.4</a> <a class="dropdown-item" href="/5.8.3/File::Find">5.8.3</a> <a class="dropdown-item" href="/5.8.2/File::Find">5.8.2</a> <a class="dropdown-item" href="/5.8.1/File::Find">5.8.1</a> <a class="dropdown-item" href="/5.8.0/File::Find">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/File::Find">5.6.2</a> <a class="dropdown-item" href="/5.6.1/File::Find">5.6.1</a> <a class="dropdown-item" href="/5.6.0/File::Find">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/File::Find">5.005_04</a> <a class="dropdown-item" href="/5.005_03/File::Find">5.005_03</a> <a class="dropdown-item" href="/5.005_02/File::Find">5.005_02</a> <a class="dropdown-item" href="/5.005_01/File::Find">5.005_01</a> <a class="dropdown-item" href="/5.005/File::Find">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/File::Find">blead</a> <a class="dropdown-item" href="/5.41.10/File::Find">5.41.10</a> <a class="dropdown-item" href="/5.41.9/File::Find">5.41.9</a> <a class="dropdown-item" href="/5.41.8/File::Find">5.41.8</a> <a class="dropdown-item" href="/5.41.7/File::Find">5.41.7</a> <a class="dropdown-item" href="/5.41.6/File::Find">5.41.6</a> <a class="dropdown-item" href="/5.41.5/File::Find">5.41.5</a> <a class="dropdown-item" href="/5.41.4/File::Find">5.41.4</a> <a class="dropdown-item" href="/5.41.3/File::Find">5.41.3</a> <a class="dropdown-item" href="/5.41.2/File::Find">5.41.2</a> <a class="dropdown-item" href="/5.41.1/File::Find">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/File::Find">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/File::Find">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/File::Find">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item active" href="/5.39.10/File::Find">5.39.10</a> <a class="dropdown-item" href="/5.39.9/File::Find">5.39.9</a> <a class="dropdown-item" href="/5.39.8/File::Find">5.39.8</a> <a class="dropdown-item" href="/5.39.7/File::Find">5.39.7</a> <a class="dropdown-item" href="/5.39.6/File::Find">5.39.6</a> <a class="dropdown-item" href="/5.39.5/File::Find">5.39.5</a> <a class="dropdown-item" href="/5.39.4/File::Find">5.39.4</a> <a class="dropdown-item" href="/5.39.3/File::Find">5.39.3</a> <a class="dropdown-item" href="/5.39.2/File::Find">5.39.2</a> <a class="dropdown-item" href="/5.39.1/File::Find">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/File::Find">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.39.10/perl">Perl</a> <a class="dropdown-item" href="/5.39.10/perlintro">Intro</a> <a class="dropdown-item" href="/5.39.10/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.39.10/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.39.10/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/perlop">Operators</a> <a class="dropdown-item" href="/5.39.10/functions">Functions</a> <a class="dropdown-item" href="/5.39.10/variables">Variables</a> <a class="dropdown-item" href="/5.39.10/modules">Modules</a> <a class="dropdown-item" href="/5.39.10/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.39.10/perlcommunity">Community</a> <a class="dropdown-item" href="/5.39.10/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.39.10/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.39.10/File::Find">File::Find</a> <div id="more"> (<a href="/5.39.10/File::Find.txt">source</a>, <a href="https://metacpan.org/pod/File::Find">CPAN</a>) </div> <div id="moduleversion">version 1.44</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.39.10. This is a development version of Perl. </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> <ul> <li> <a class="text-decoration-none" href="#%25options">%options</a> </li> <li> <a class="text-decoration-none" href="#The-wanted-function">The wanted function</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#WARNINGS">WARNINGS</a> </li> <li> <a class="text-decoration-none" href="#BUGS-AND-CAVEATS">BUGS AND CAVEATS</a> </li> <li> <a class="text-decoration-none" href="#HISTORY">HISTORY</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>File::Find - Traverse a directory tree.</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use File::Find; find(\&amp;wanted, @directories_to_search); sub wanted { ... } use File::Find; finddepth(\&amp;wanted, @directories_to_search); sub wanted { ... } use File::Find; find({ wanted =&gt; \&amp;process, follow =&gt; 1 }, &#39;.&#39;);</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>These are functions for searching through directory trees doing work on each file found similar to the Unix <a href="http://man.he.net/man1/find">find(1)</a> command. <code>File::Find</code> exports two functions, <code>find</code> and <code>finddepth</code>. They work similarly but have subtle differences.</p> <dl> <dt id="find"><a class="permalink" href="#find">#</a><b>find</b></dt> <dd> <pre><code>find(\&amp;wanted, @directories); find(\%options, @directories);</code></pre> <p><code>find()</code> does a depth-first search over the given <code>@directories</code> in the order they are given. For each file or directory found, it calls the <code>&amp;wanted</code> subroutine. (See below for details on how to use the <code>&amp;wanted</code> function). Additionally, for each directory found, it will <code>chdir()</code> into that directory and continue the search, invoking the <code>&amp;wanted</code> function on each file or subdirectory in the directory.</p> </dd> <dt id="finddepth"><a class="permalink" href="#finddepth">#</a><b>finddepth</b></dt> <dd> <pre><code>finddepth(\&amp;wanted, @directories); finddepth(\%options, @directories);</code></pre> <p><code>finddepth()</code> works just like <code>find()</code> except that it invokes the <code>&amp;wanted</code> function for a directory <i>after</i> invoking it for the directory&#39;s contents. It does a postorder traversal instead of a preorder traversal, working from the bottom of the directory tree up where <code>find()</code> works from the top of the tree down.</p> </dd> </dl> <p>Despite the name of the <code>finddepth()</code> function, both <code>find()</code> and <code>finddepth()</code> perform a depth-first search of the directory hierarchy.</p> <h2 id="%options"><a class="permalink" href="#%25options">#</a><a id="options"></a><code>%options</code></h2> <p>The first argument to <code>find()</code> is either a code reference to your <code>&amp;wanted</code> function, or a hash reference describing the operations to be performed for each file. The code reference is described in <a href="#The-wanted-function">&quot;The wanted function&quot;</a> below.</p> <p>Here are the possible <b>keys</b> for the hash:</p> <dl> <dt id="wanted"><a class="permalink" href="#wanted">#</a><code>wanted</code></dt> <dd> <p>The value should be a code reference. This code reference is described in <a href="#The-wanted-function">&quot;The wanted function&quot;</a> below. The <code>&amp;wanted</code> subroutine is mandatory.</p> </dd> <dt id="bydepth"><a class="permalink" href="#bydepth">#</a><code>bydepth</code></dt> <dd> <p>Reports the name of a directory only AFTER all its entries have been reported. Entry point <code>finddepth()</code> is a shortcut for specifying <code>{ bydepth =&gt; 1 }</code> in the first argument of <code>find()</code>.</p> </dd> <dt id="preprocess"><a class="permalink" href="#preprocess">#</a><code>preprocess</code></dt> <dd> <p>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 <code>$File::Find::dir</code>. Your preprocessing function is called after <code>readdir()</code>, but before the loop that calls the <code>wanted()</code> 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. When <code>follow</code> or <code>follow_fast</code> are in effect, <code>preprocess</code> is a no-op.</p> </dd> <dt id="postprocess"><a class="permalink" href="#postprocess">#</a><code>postprocess</code></dt> <dd> <p>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 <code>$File::Find::dir</code>. This hook is handy for summarizing a directory, such as calculating its disk usage. When <code>follow</code> or <code>follow_fast</code> are in effect, <code>postprocess</code> is a no-op.</p> </dd> <dt id="follow"><a class="permalink" href="#follow">#</a><code>follow</code></dt> <dd> <p>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 <a href="#follow_fast">&quot;follow_fast&quot;</a> and <a href="#follow_skip">&quot;follow_skip&quot;</a> below. If either <code>follow</code> or <code>follow_fast</code> is in effect:</p> <ul> <li><p>It is guaranteed that an <code>lstat</code> has been called before the user&#39;s <code>wanted()</code> function is called. This enables fast file checks involving <code>_</code>. Note that this guarantee no longer holds if <code>follow</code> or <code>follow_fast</code> are not set.</p> </li> <li><p>There is a variable <code>$File::Find::fullname</code> 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 to <code>undef</code>.</p> </li> </ul> </dd> <dt id="follow_fast"><a class="permalink" href="#follow_fast">#</a><code>follow_fast</code></dt> <dd> <p>This is similar to <code>follow</code> 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&#39;s <code>wanted()</code> function) is worse than just taking time, the option <code>follow</code> should be used.</p> </dd> <dt id="follow_skip"><a class="permalink" href="#follow_skip">#</a><code>follow_skip</code></dt> <dd> <p><code>follow_skip==1</code>, 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, <code>File::Find</code> dies.</p> <p><code>follow_skip==0</code> causes <code>File::Find</code> to die if any file is about to be processed a second time.</p> <p><code>follow_skip==2</code> causes <code>File::Find</code> to ignore any duplicate files and directories but to proceed normally otherwise.</p> </dd> <dt id="dangling_symlinks"><a class="permalink" href="#dangling_symlinks">#</a><code>dangling_symlinks</code></dt> <dd> <p>Specifies what to do with symbolic links whose target doesn&#39;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 <code>&quot;symbolic_link_name is a dangling symbolic link\n&quot;</code> will be issued. If false, the dangling symbolic link will be silently ignored.</p> </dd> <dt id="no_chdir"><a class="permalink" href="#no_chdir">#</a><code>no_chdir</code></dt> <dd> <p>Does not <code>chdir()</code> to each directory as it recurses. The <code>wanted()</code> function will need to be aware of this, of course. In this case, <code>$_</code> will be the same as <code>$File::Find::name</code>.</p> </dd> <dt id="untaint"><a class="permalink" href="#untaint">#</a><code>untaint</code></dt> <dd> <p>If find is used in <a href="/5.39.10/perlsec#Taint-mode">taint-mode</a> (<code>-T</code> command line switch or <code>if EUID != UID</code> or <code>if EGID != GID</code>), then internally directory names have to be untainted before they can be <code>chdir</code>&#39;d to. Therefore they are checked against a regular expression <code>untaint_pattern</code>. Note that all names passed to the user&#39;s <code>wanted()</code> function are still tainted. If this option is used while not in taint-mode, <code>untaint</code> is a no-op.</p> </dd> <dt id="untaint_pattern"><a class="permalink" href="#untaint_pattern">#</a><code>untaint_pattern</code></dt> <dd> <p>See above. This should be set using the <code>qr</code> quoting operator. The default is set to <code>qr|^([-+@\w./]+)$|</code>. Note that the parentheses are vital.</p> </dd> <dt id="untaint_skip"><a class="permalink" href="#untaint_skip">#</a><code>untaint_skip</code></dt> <dd> <p>If set, a directory which fails the <code>untaint_pattern</code> is skipped, including all its sub-directories. The default is to <code>die</code> in such a case.</p> </dd> </dl> <h2 id="The-wanted-function"><a class="permalink" href="#The-wanted-function">#</a><a id="The"></a>The wanted function</h2> <p>The <code>wanted()</code> function does whatever verifications you want on each file and directory. Note that despite its name, the <code>wanted()</code> function is a generic callback function, and does <b>not</b> tell <code>File::Find</code> if a file is &quot;wanted&quot; or not. In fact, its return value is ignored.</p> <p>The <code>wanted</code> function takes no arguments but rather does its work through a collection of variables.</p> <dl> <dt id="$File::Find::dir-is-the-current-directory-name,"><a class="permalink" href="#$File::Find::dir-is-the-current-directory-name,">#</a><a id="File::Find::dir-is-the-current-directory-name"></a><code>$File::Find::dir</code> is the current directory name,</dt> <dd> </dd> <dt id="$_-is-the-current-filename-within-that-directory"><a class="permalink" href="#$_-is-the-current-filename-within-that-directory">#</a><a id="is-the-current-filename-within-that-directory"></a><code>$_</code> is the current filename within that directory</dt> <dd> </dd> <dt id="$File::Find::name-is-the-complete-pathname-to-the-file."><a class="permalink" href="#$File::Find::name-is-the-complete-pathname-to-the-file.">#</a><a id="File::Find::name-is-the-complete-pathname-to-the-file"></a><code>$File::Find::name</code> is the complete pathname to the file.</dt> <dd> </dd> </dl> <p>The above variables have all been localized and may be changed without affecting data outside of the wanted function.</p> <p>For example, when examining the file <i>/some/path/foo.ext</i> you will have:</p> <pre><code>$File::Find::dir = /some/path/ $_ = foo.ext $File::Find::name = /some/path/foo.ext</code></pre> <p>You are <code>chdir()</code>&#39;d to <code>$File::Find::dir</code> when the function is called, unless <code>no_chdir</code> was specified. Note that when changing to directories is in effect, the root directory (<i>/</i>) is a somewhat special case inasmuch as the concatenation of <code>$File::Find::dir</code>, <code>&#39;/&#39;</code> and <code>$_</code> is not literally equal to <code>$File::Find::name</code>. The table below summarizes all variants:</p> <pre><code> $File::Find::name $File::Find::dir $_ default / / . no_chdir=&gt;0 /etc / etc /etc/x /etc x no_chdir=&gt;1 / / / /etc / /etc /etc/x /etc /etc/x</code></pre> <p>When <code>follow</code> or <code>follow_fast</code> are in effect, there is also a <code>$File::Find::fullname</code>. The function may set <code>$File::Find::prune</code> to prune the tree unless <code>bydepth</code> was specified. Unless <code>follow</code> or <code>follow_fast</code> is specified, for compatibility reasons (<code>find.pl</code>, <a href="/5.39.10/find2perl">find2perl</a>) there are in addition the following globals available: <code>$File::Find::topdir</code>, <code>$File::Find::topdev</code>, <code>$File::Find::topino</code>, <code>$File::Find::topmode</code> and <code>$File::Find::topnlink</code>.</p> <p>This library is useful for the <code>find2perl</code> tool (distributed with the <a href="/5.39.10/App::find2perl">App::find2perl</a> CPAN module), which when fed:</p> <pre><code class="plaintext">find2perl / -name .nfs\* -mtime +7 \ -exec rm -f {} \; -o -fstype nfs -prune</code></pre> <p>produces something like:</p> <pre><code>sub wanted { /^\.nfs.*\z/s &amp;&amp; (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) &amp;&amp; int(-M _) &gt; 7 &amp;&amp; unlink($_) || ($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))) &amp;&amp; $dev &lt; 0 &amp;&amp; ($File::Find::prune = 1); }</code></pre> <p>Notice the <code>_</code> in the above <code>int(-M _)</code>: the <code>_</code> is a magical filehandle that caches the information from the preceding <code>stat()</code>, <code>lstat()</code>, or filetest.</p> <p>Here&#39;s another interesting wanted function. It will find all symbolic links that don&#39;t resolve:</p> <pre><code>sub wanted { -l &amp;&amp; !-e &amp;&amp; print &quot;bogus link: $File::Find::name\n&quot;; }</code></pre> <p>Note that you may mix directories and (non-directory) files in the list of directories to be searched by the <code>wanted()</code> function.</p> <pre><code>find(\&amp;wanted, &quot;./foo&quot;, &quot;./bar&quot;, &quot;./baz/epsilon&quot;);</code></pre> <p>In the example above, no file in <i>./baz/</i> other than <i>./baz/epsilon</i> will be evaluated by <code>wanted()</code>.</p> <p>See also the script <code>pfind</code> on CPAN for a nice application of this module.</p> <h1 id="WARNINGS"><a class="permalink" href="#WARNINGS">#</a>WARNINGS</h1> <p>If you run your program with the <code>-w</code> switch, or if you use the <code>warnings</code> pragma, File::Find will report warnings for several weird situations. You can disable these warnings by putting the statement</p> <pre><code>no warnings &#39;File::Find&#39;;</code></pre> <p>in the appropriate scope. See <a href="/5.39.10/warnings">warnings</a> for more info about lexical warnings.</p> <h1 id="BUGS-AND-CAVEATS"><a class="permalink" href="#BUGS-AND-CAVEATS">#</a><a id="BUGS"></a>BUGS AND CAVEATS</h1> <dl> <dt id="$dont_use_nlink"><a class="permalink" href="#$dont_use_nlink">#</a><a id="dont_use_nlink"></a><code>$dont_use_nlink</code></dt> <dd> <p>You can set the variable <code>$File::Find::dont_use_nlink</code> to <code>0</code> if you are sure the filesystem you are scanning reflects the number of subdirectories in the parent directory&#39;s <code>nlink</code> count.</p> <p>If you do set <code>$File::Find::dont_use_nlink</code> to 0, you may notice an improvement in speed at the risk of not recursing into subdirectories if a filesystem doesn&#39;t populate <code>nlink</code> as expected.</p> <p><code>$File::Find::dont_use_nlink</code> now defaults to 1 on all platforms.</p> </dd> <dt id="Symlinks"><a class="permalink" href="#Symlinks">#</a>Symlinks</dt> <dd> <p>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 if <code>follow_fast</code> 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.</p> </dd> </dl> <h1 id="HISTORY"><a class="permalink" href="#HISTORY">#</a>HISTORY</h1> <p><code>File::Find</code> used to produce incorrect results if called recursively. During the development of perl 5.8 this bug was fixed. The first fixed version of <code>File::Find</code> was 1.01.</p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="http://man.he.net/man1/find">find(1)</a>, <a href="/5.39.10/find2perl">find2perl</a></p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10