How does I list the files in a directory?

Source: Internet
Author: User
Tags glob

The original address: How does I list the files in a directory?

You want a list of all the files, or all the files matching a certain pattern, or with a certain ending, in a directory

The solution

Reading directories is a bit like Reading files. First you open the directory, then you read from it and then you close it. You use the A directory handle much as you use a file handle.

Step 1:opening the Directory

To open the directory, we use a function called opendir . The open function to open files is much. In the example below, we open the/tmp directory:

    #!/usr/bin/perl use    strict;    Use warnings;    My $directory = '/tmp ';    Opendir (DIR, $directory) or Die $!;
Step 2:reading the Directory

To read the files and directories in the directory we use the readdir function. readdir returns the name of each file or Direc Tory in the opened directory in turn if used in scalar context, or a list of the names of all files and directories in T Hat directory when used in list context. This means, we can use in readdir a foreach loop (or any other loop construct):

    while (My $file = Readdir (DIR)) {        print "$file \ n";    }
Step 3:closing the Directory

We use the function to closedir close the directory Once we is finished with it. Like files, the directory would be closed if the program terminates, but sometimes you'll need to explicitly close the Directory

    Closedir (DIR);
Directory Listing

Provided your program have sufficient access to the directory being read, would readdir list every file and directory Containe D in that directory. However, you often won't want all files or directories. For example, it's quite common to exclude all filenames beginning with a period:

    #!/usr/bin/perl use    strict;    Use warnings;    My $dir = '/tmp ';    Opendir (DIR, $dir) or Die $!;    while (My $file = Readdir (DIR)) {        # Use a regular expression to ignore files beginning with a period        next if ($fil E =~ m/^\./);p rint "$file \ n";    }    Closedir (DIR);    Exit 0;

See further-a more compact-a-doing this.

Find the Directories

Sometimes want to find all the directories in a directory. Remember readdir() that gives your names of the files and directories, not the paths. If you want to test a file using any of the standard file tests, you need to use the full path:

    #!/usr/bin/perl use    strict;    Use warnings;    My $dir = '/tmp ';    Opendir (DIR, $dir) or Die $!;    while (My $file = Readdir (DIR)) {        # A file test to check the it is a directory# use-f to test for a file        next UN Less (-D "$dir/$file");p rint "$file \ n";    }    Closedir (DIR);    Exit 0;
Find files ending in ...

Quite often you want to find all files ending in a given suffix. For example, want to find all files ending in .txt :

    #!/usr/bin/perl use    strict;    Use warnings;    My $dir = '/tmp ';    Opendir (DIR, $dir) or Die $!;    while (My $file = Readdir (DIR)) {        # We are only want files        next unless (-F "$dir/$file");        # Use a regular expression to the Find files ending in        . txt next unless ($file =~ m/\.txt$/);        print "$file \ n";    }    Closedir (DIR);    Exit 0;
An advanced example

A More advanced example are to use the filter out of the grep files you want. The following example (based on a code sample from perldoc -f readdir ) gets all the files (not directories) beginning with a period fro M the Open directory. The filenames is found in the array @dots .

    #!/usr/bin/perl use    strict;    Use warnings;    My $dir = '/tmp ';    Opendir (DIR, $dir) or Die $!;    My @dots         = grep {             /^\./             # begins with a period    &&-F "$dir/$_" # and is   a file} readdir (DI R);    # Loop through the array printing    out the filenames foreach my $file (@dots) {        print "$file \ n";    }    Closedir (DIR);    Exit 0;
File::find

You can use the File::find module to recursively search through a directory (or directories). It is the best used if you want to perform some operation on each file. See for more perldoc File::Find information.

Glob

Another a getting a directory listing-if you ' re only interested in the current directory and not in any Sub-directo Ries-is to use glob . You can pass glob a pattern (or patterns) to match and it'll return any files that match. The example below would list all. pl and. pm files in the current directory:

    #!/usr/bin/perl use    strict;    Use warnings;    My @files = Glob ("*.pl *.pm");    foreach my $file (@files) {        print "$file \ n";    }    Exit 0;
See Also
    Perldoc-f opendir    perldoc-f readdir    perldoc-f closedir    perldoc-f-x    perldoc-f grep    perldoc File: : Find    perldoc-f glob    perldoc File::glob

How does I list the files in a directory?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.