Perl脚本:列出目录下所有的文件扩展名(后缀名)

 

#!/bin/perl
# Extension_filename_lister v0.1
# List all extension filenames without duplicate in a directory.

use strict;
use warnings;
use File::Find;

my %extname_list;

sub process_file {
    my $cur_file = $_; 

    # Ignore the directories . and ..
    if ($cur_file eq "." || $cur_file eq "..") {
        return;
    }

    (my $ext_name) = ($cur_file =~ /\.(\w+)$/);

    if (defined ($ext_name)) {
        $extname_list{$ext_name} = "Yes";
    }

#    print $cur_file, "\n";
#    print $ext_name, "\n";
}

my @DIRLIST = ("."); 
find(\&process_file, @DIRLIST);

print "Extension filenames:\n";
foreach my $item (keys %extname_list) {
    print $item, "\n";
}

 

你可能感兴趣的:(Code,Fragments)