{
    "mode": "info",
    "parameter": "File::Find",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/File%3A%3AFind/json",
    "generated": "2026-07-05T16:10:30Z",
    "synopsis": "use File::Find;\nfind(\\&wanted, @directoriestosearch);\nsub wanted { ... }\nuse File::Find;\nfinddepth(\\&wanted, @directoriestosearch);\nsub wanted { ... }\nuse File::Find;\nfind({ wanted => \\&process, follow => 1 }, '.');",
    "sections": {
        "NAME": {
            "content": "File::Find - Traverse a directory tree.\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use File::Find;\nfind(\\&wanted, @directoriestosearch);\nsub wanted { ... }\n\nuse File::Find;\nfinddepth(\\&wanted, @directoriestosearch);\nsub wanted { ... }\n\nuse File::Find;\nfind({ wanted => \\&process, follow => 1 }, '.');\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "These are functions for searching through directory trees doing work on\neach file found similar to the Unix find command.  File::Find exports\ntwo functions, \"find\" and \"finddepth\".  They work similarly but have\nsubtle differences.\n\nfind\nfind(\\&wanted,  @directories);\nfind(\\%options, @directories);\n\n\"find()\" does a depth-first search over the given @directories in\nthe order they are given.  For each file or directory found, it\ncalls the &wanted subroutine.  (See below for details on how to use\nthe &wanted function).  Additionally, for each directory found, it\nwill \"chdir()\" into that directory and continue the search,\ninvoking the &wanted function on each file or subdirectory in the\ndirectory.\n\nfinddepth\nfinddepth(\\&wanted,  @directories);\nfinddepth(\\%options, @directories);\n\n\"finddepth()\" works just like \"find()\" except that it invokes the\n&wanted function for a directory after invoking it for the\ndirectory's contents.  It does a postorder traversal instead of a\npreorder traversal, working from the bottom of the directory tree\nup where \"find()\" works from the top of the tree down.\n\nDespite the name of the \"finddepth()\" function, both \"find()\" and\n\"finddepth()\" perform a depth-first search of the directory hierarchy.\n\n%options\nThe first argument to \"find()\" is either a code reference to your\n&wanted function, or a hash reference describing the operations to be\nperformed for each file.  The code reference is described in \"The\nwanted function\" below.\n\nHere are the possible keys for the hash:\n\n\"wanted\"\nThe value should be a code reference.  This code reference is\ndescribed in \"The wanted function\" below. The &wanted subroutine is\nmandatory.\n\n\"bydepth\"\nReports the name of a directory only AFTER all its entries have\nbeen reported.  Entry point \"finddepth()\" is a shortcut for\nspecifying \"{ bydepth => 1 }\" in the first argument of \"find()\".\n\n\"preprocess\"\nThe value should be a code reference. This code reference is used\nto preprocess the current directory. The name of the currently\nprocessed directory is in $File::Find::dir. Your preprocessing\nfunction is called after \"readdir()\", but before the loop that\ncalls the \"wanted()\" function. It is called with a list of strings\n(actually file/directory names) and is expected to return a list of\nstrings. The code can be used to sort the file/directory names\nalphabetically, numerically, or to filter out directory entries\nbased on their name alone. When follow or followfast are in\neffect, \"preprocess\" is a no-op.\n\n\"postprocess\"\nThe value should be a code reference. It is invoked just before\nleaving the currently processed directory. It is called in void\ncontext with no arguments. The name of the current directory is in\n$File::Find::dir. This hook is handy for summarizing a directory,\nsuch as calculating its disk usage. When follow or followfast are\nin effect, \"postprocess\" is a no-op.\n\n\"follow\"\nCauses symbolic links to be followed. Since directory trees with\nsymbolic links (followed) may contain files more than once and may\neven have cycles, a hash has to be built up with an entry for each\nfile.  This might be expensive both in space and time for a large\ndirectory tree. See \"followfast\" and \"followskip\" below.  If\neither follow or followfast is in effect:\n\no   It is guaranteed that an lstat has been called before the\nuser's \"wanted()\" function is called. This enables fast file\nchecks involving \"\".  Note that this guarantee no longer holds\nif follow or followfast are not set.\n\no   There is a variable $File::Find::fullname which holds the\nabsolute pathname of the file with all symbolic links resolved.\nIf the link is a dangling symbolic link, then fullname will be\nset to \"undef\".\n\nThis is a no-op on Win32.\n\n\"followfast\"\nThis is similar to follow except that it may report some files more\nthan once.  It does detect cycles, however.  Since only symbolic\nlinks have to be hashed, this is much cheaper both in space and\ntime.  If processing a file more than once (by the user's\n\"wanted()\" function) is worse than just taking time, the option\nfollow should be used.\n\nThis is also a no-op on Win32.\n\n\"followskip\"\n\"followskip==1\", which is the default, causes all files which are\nneither directories nor symbolic links to be ignored if they are\nabout to be processed a second time. If a directory or a symbolic\nlink are about to be processed a second time, File::Find dies.\n\n\"followskip==0\" causes File::Find to die if any file is about to\nbe processed a second time.\n\n\"followskip==2\" causes File::Find to ignore any duplicate files\nand directories but to proceed normally otherwise.\n\n\"danglingsymlinks\"\nSpecifies what to do with symbolic links whose target doesn't\nexist.  If true and a code reference, will be called with the\nsymbolic link name and the directory it lives in as arguments.\nOtherwise, if true and warnings are on, a warning of the form\n\"symboliclinkname is a dangling symbolic link\\n\" will be issued.\nIf false, the dangling symbolic link will be silently ignored.\n\n\"nochdir\"\nDoes not \"chdir()\" to each directory as it recurses. The \"wanted()\"\nfunction will need to be aware of this, of course. In this case, $\nwill be the same as $File::Find::name.\n\n\"untaint\"\nIf find is used in taint-mode (-T command line switch or if EUID !=\nUID or if EGID != GID), then internally directory names have to be\nuntainted before they can be \"chdir\"'d to. Therefore they are\nchecked against a regular expression untaintpattern.  Note that\nall names passed to the user's \"wanted()\" function are still\ntainted. If this option is used while not in taint-mode, \"untaint\"\nis a no-op.\n\n\"untaintpattern\"\nSee above. This should be set using the \"qr\" quoting operator.  The\ndefault is set to \"qr|^([-+@\\w./]+)$|\".  Note that the parentheses\nare vital.\n\n\"untaintskip\"\nIf set, a directory which fails the untaintpattern is skipped,\nincluding all its sub-directories. The default is to \"die\" in such\na case.\n\nThe wanted function\nThe \"wanted()\" function does whatever verifications you want on each\nfile and directory.  Note that despite its name, the \"wanted()\"\nfunction is a generic callback function, and does not tell File::Find\nif a file is \"wanted\" or not.  In fact, its return value is ignored.\n\nThe wanted function takes no arguments but rather does its work through\na collection of variables.\n\n$File::Find::dir is the current directory name,\n$ is the current filename within that directory\n$File::Find::name is the complete pathname to the file.\n\nThe above variables have all been localized and may be changed without\naffecting data outside of the wanted function.\n\nFor example, when examining the file /some/path/foo.ext you will have:\n\n$File::Find::dir  = /some/path/\n$                = foo.ext\n$File::Find::name = /some/path/foo.ext\n\nYou are chdir()'d to $File::Find::dir when the function is called,\nunless \"nochdir\" was specified. Note that when changing to directories\nis in effect, the root directory (/) is a somewhat special case\ninasmuch as the concatenation of $File::Find::dir, '/' and $ is not\nliterally equal to $File::Find::name. The table below summarizes all\nvariants:\n\n$File::Find::name  $File::Find::dir  $\ndefault      /                  /                 .\nnochdir=>0  /etc               /                 etc\n/etc/x             /etc              x\n\nnochdir=>1  /                  /                 /\n/etc               /                 /etc\n/etc/x             /etc              /etc/x\n\nWhen \"follow\" or \"followfast\" are in effect, there is also a\n$File::Find::fullname.  The function may set $File::Find::prune to\nprune the tree unless \"bydepth\" was specified.  Unless \"follow\" or\n\"followfast\" is specified, for compatibility reasons (find.pl,\nfind2perl) there are in addition the following globals available:\n$File::Find::topdir, $File::Find::topdev, $File::Find::topino,\n$File::Find::topmode and $File::Find::topnlink.\n\nThis library is useful for the \"find2perl\" tool (distributed as part of\nthe App-find2perl CPAN distribution), which when fed,\n\nfind2perl / -name .nfs\\* -mtime +7 \\\n-exec rm -f {} \\; -o -fstype nfs -prune\n\nproduces something like:\n\nsub wanted {\n/^\\.nfs.*\\z/s &&\n(($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($)) &&\nint(-M ) > 7 &&\nunlink($)\n||\n($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($))) &&\n$dev < 0 &&\n($File::Find::prune = 1);\n}\n\nNotice the \"\" in the above \"int(-M )\": the \"\" is a magical\nfilehandle that caches the information from the preceding \"stat()\",\n\"lstat()\", or filetest.\n\nHere's another interesting wanted function.  It will find all symbolic\nlinks that don't resolve:\n\nsub wanted {\n-l && !-e && print \"bogus link: $File::Find::name\\n\";\n}\n\nNote that you may mix directories and (non-directory) files in the list\nof directories to be searched by the \"wanted()\" function.\n\nfind(\\&wanted, \"./foo\", \"./bar\", \"./baz/epsilon\");\n\nIn the example above, no file in ./baz/ other than ./baz/epsilon will\nbe evaluated by \"wanted()\".\n\nSee also the script \"pfind\" on CPAN for a nice application of this\nmodule.\n",
            "subsections": []
        },
        "WARNINGS": {
            "content": "If you run your program with the \"-w\" switch, or if you use the\n\"warnings\" pragma, File::Find will report warnings for several weird\nsituations. You can disable these warnings by putting the statement\n\nno warnings 'File::Find';\n\nin the appropriate scope. See warnings for more info about lexical\nwarnings.\n",
            "subsections": []
        },
        "BUGS AND CAVEATS": {
            "content": "$dontusenlink\nYou can set the variable $File::Find::dontusenlink to 0 if you\nare sure the filesystem you are scanning reflects the number of\nsubdirectories in the parent directory's \"nlink\" count.\n\nIf you do set $File::Find::dontusenlink to 0, you may notice an\nimprovement in speed at the risk of not recursing into\nsubdirectories if a filesystem doesn't populate \"nlink\" as\nexpected.\n\n$File::Find::dontusenlink now defaults to 1 on all platforms.\n\nsymlinks\nBe aware that the option to follow symbolic links can be dangerous.\nDepending on the structure of the directory tree (including\nsymbolic links to directories) you might traverse a given\n(physical) directory more than once (only if \"followfast\" is in\neffect).  Furthermore, deleting or changing files in a symbolically\nlinked directory might cause very unpleasant surprises, since you\ndelete or change files in an unknown directory.\n",
            "subsections": []
        },
        "HISTORY": {
            "content": "File::Find used to produce incorrect results if called recursively.\nDuring the development of perl 5.8 this bug was fixed.  The first fixed\nversion of File::Find was 1.01.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "find(1), find2perl.\n\nperl v5.34.0                      2026-06-23                 File::Find(3perl)",
            "subsections": []
        }
    },
    "summary": "File::Find - Traverse a directory tree.",
    "flags": [],
    "examples": [],
    "see_also": [
        {
            "name": "find",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/find/1/json"
        },
        {
            "name": "Find",
            "section": "3perl",
            "url": "https://www.chedong.com/phpMan.php/man/Find/3perl/json"
        }
    ]
}