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
Example 1: To print all the available directories in the searched folder.
Perl
Output:
Example 2: To print out the files available in a directory
Perl
Output:
Example 3: Printing only once all the directories available/present in the folder/directory we are accessing.
Perl
Output:
Example 4: To access the files present inside a directory without accessing the other directories available in that same directory.
Perl
Output:
Example 5: Use of
Perl
Example 6: To find all types of text files.
Perl
Output:
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 offinddepth()function is almost similar to thefind()function except for the fact that it invokes&wantedfor directory's content first than invokes it for the directory.
Find command.
Find function takes two arguments:
- 1st argument is a subroutine called for each file which we found through
findfunction. - 2nd argument is the list of the directories where
findfunction is going to search the files.
Example 1: To print all the available directories in the searched folder.
#!usr/bin/perl
print "content-type: text/html\n\n";
use strict;
use warnings;
use File::Find;
find(
{
wanted => \&findfiles,
},
'GeeksforGeeks'
);
sub findfiles
{
#To search only the directory
print "$File::Find::dir\n";
}
exit;
Example 2: To print out the files available in a directory
#!usr/bin/perl
print "content-type: text/html\n\n";
use strict;
use warnings;
use File::Find;
find(
{
wanted => \&findfiles,
},
'GeeksforGeeks'
);
sub findfiles
{
#To search the files inside the directories
print "$File::Find::name\n";
}
exit;
Example 3: Printing only once all the directories available/present in the folder/directory we are accessing.
#!usr/bin/perl
print "content-type: text/html\n\n";
use strict;
use warnings;
use File::Find;
find(
{
wanted => \&findfiles,
},
'GeeksforGeeks'
);
sub findfiles
{
# To search all the available directories
# in the given directory without accessing them
print "$File::Find::name\n" if -d;
}
exit;
Example 4: To access the files present inside a directory without accessing the other directories available in that same directory.
#!usr/bin/perl
print "content-type: text/html\n\n";
use strict;
use warnings;
use File::Find;
find(
{
wanted => \&findfiles,
},
'GeeksforGeeks'
);
sub findfiles
{
# Not accessing Geeks_New and Image directory
# present inside the dir GeeksforGeeks
$File::Find::prune = 1 if /Geeks_New/;
$File::Find::prune = 1 if /Images/;
# To print the files present in dir GeekforGeeks
print "$File::Find::name\n";
}
exit;
Example 5: Use of finddepth() function to find the files and sub-directories in a directory.
#!usr/bin/perl
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.
#!usr/bin/perl
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;