Perl | Finding Files and Directories
Last Updated :
19 Jul, 2019
For traversing a directory tree in Perl, there are several ways/methods. Traversing can be performed through function calls opendir and readdir which are a part of Perl programming language. Traversing files and directories in Perl can also be done through File::Find
module which comes with the Perl language.
File::Find contains 2 modules:
- Find:
find()
function performs a depth-first search on the mentioned/defined @directories. It calls and invokes the "&wanted"
function for each file or sub-directory found in that directory. find()
works from top to down.
- Finddepth:
finddepth()
performs a post-order traversal instead of performing pre-order, working from down to top. Working of finddepth()
function is almost similar to the find()
function except for the fact that it invokes &wanted
for directory’s content first than invokes it for the directory.
The only difference among both the modules is the order in which the files and directories are parsed. Find modules in Perl has all the functions similar to the Unix Find
command.
Find
function takes two arguments:
- 1st argument is a subroutine called for each file which we found through
find
function.
- 2nd argument is the list of the directories where
find
function is going to search the files.
Following are some example scripts of Perl to find the Files and Directories:
Example 1: To print all the available directories in the searched folder.
print "content-type: text/html\n\n" ;
use strict;
use warnings;
use File::Find;
find(
{
wanted => \&findfiles,
},
'GeeksforGeeks'
);
sub findfiles
{
print "$File::Find::dir\n" ;
}
exit ;
|
Output:
Example 2: To print out the files available in a directory
print "content-type: text/html\n\n" ;
use strict;
use warnings;
use File::Find;
find(
{
wanted => \&findfiles,
},
'GeeksforGeeks'
);
sub findfiles
{
print "$File::Find::name\n" ;
}
exit ;
|
Output:
Example 3: Printing only once all the directories available/present in the folder/directory we are accessing.
print "content-type: text/html\n\n" ;
use strict;
use warnings;
use File::Find;
find(
{
wanted => \&findfiles,
},
'GeeksforGeeks'
);
sub findfiles
{
print "$File::Find::name\n" if -d;
}
exit ;
|
Output:
Example 4: To access the files present inside a directory without accessing the other directories available in that same directory.
print "content-type: text/html\n\n" ;
use strict;
use warnings;
use File::Find;
find(
{
wanted => \&findfiles,
},
'GeeksforGeeks'
);
sub findfiles
{
$File::Find::prune = 1 if /Geeks_New/;
$File::Find::prune = 1 if /Images/;
print "$File::Find::name\n" ;
}
exit ;
|
Output:
Example 5: Use of finddepth()
function to find the files and sub-directories in a directory.
print "content-type: text/html\n\n" ;
use strict;
use warnings;
use File::Find;
finddepth(
{
wanted => \&findfiles,
},
'GeeksforGeeks'
);
sub findfiles
{
print "$File::Find::name\n" ;
}
exit ;
|
Example 6: To find all types of text files.
print "content-type: text/html\n\n" ;
use strict;
use warnings;
use File::Find;
find(
{
wanted => \&findfiles,
},
'GeeksforGeeks'
);
sub findfiles
{
print "$File::Find::name\n" if -T;
}
exit ;
|
Output:
Share your thoughts in the comments
Please Login to comment...