ted
ted

Reputation: 5329

input parametrs for script, count file in dir by extension

I'd like to count the amount of the files due to their extension via script in terminal;
like:
sm1@smth:~$ ./scriptname.pl pathname extension
/home/dir/ contains 5 file of *.extention

Upvotes: 0

Views: 601

Answers (8)

user unknown
user unknown

Reputation: 36229

find -name "*.pdf" -exec echo -n "1" ";" | wc -c 

will not fail if filename contains '\n' which isn't illegal. Find visits subdirectories too.

Why do you want to use perl?

Upvotes: 2

Dagang Wei
Dagang Wei

Reputation: 26498

Very simple:

echo ${DIR}/*.${EXT} | wc -w

Upvotes: 0

Alex Reynolds
Alex Reynolds

Reputation: 96967

Here is an equivalent with Perl:

#!/usr/bin/perl 

# countFiles.pl    

use strict;
use warnings;
use File::Glob qw(:glob);

my $directory = $ARGV[0];
my $extension = $ARGV[1];
my @fileList = <$directory/*.$extension>;
my $fileListCount = scalar @fileList;

print STDOUT "$directory contains $fileListCount files of *.$extension\n";

Example usage:

$ countFiles.pl /Users/alexreynolds/Desktop png
/Users/alexreynolds/Desktop contains 21 files of *.png

Upvotes: 2

Powertieke
Powertieke

Reputation: 2408

Hopping in late :)

#!/usr/bin/perl

use warnings;
use strict;

scalar @ARGV == 2 or die "Need two args";

opendir(my $dh, $ARGV[0]);
my @files = grep { /\.$ARGV[1]/ } readdir($dh);
closedir($dh);

printf "Directory '%s' contains %d files with extension '.%s'\n", $ARGV[0], scalar @files, $ARGV[1];

Usage as described:

$ ./countfiles.pl <dirname> <extensionminusthedot>

Upvotes: 1

Schwern
Schwern

Reputation: 164919

In shell, use globbing and the wc command.

ls -d /some/path/*.ext | wc -l

Or you can do it in Perl with glob()

#!/usr/bin/env perl

use strict;
use warnings;

my($path, $ext) = @ARGV;
my @files = glob "$path/*$ext";
printf "Found %d files in %s with extension %s\n", scalar @files, $path, $ext;

Upvotes: 1

sysrev
sysrev

Reputation: 11

Here's a function that counts files, optionally by extension:

countfiles() {
   command find "${1:-.}" -type f -name "${2:-*}" -print0 | command tr -dc '\0' | command wc -c
   return 0
}

countfiles . "*.txt"

Using -print0 ensures that your file count remains correct in case there are file names with embedded newline characters "\n".

Upvotes: 1

Alex Reynolds
Alex Reynolds

Reputation: 96967

ls ${DIR}/*.${EXT} \
    | wc -l \
    | sed -e 's/^[ \t]*//' \
    | awk -v dir=$DIR -v ext=$EXT '{print dir" contains "$0" files of *."ext}'

Example usage:

$ DIR=/Users/alexreynolds/Desktop
$ EXT=png
$ ls ${DIR}/*.${EXT} | wc -l | sed -e 's/^[ \t]*//' | awk -v dir=$DIR -v ext=$EXT '{print dir" contains "$0" files of *."ext}'
/Users/alexreynolds/Desktop contains 21 files of *.png

Upvotes: 0

jitendra
jitendra

Reputation: 1458

The following command at console will give you the number of files in DIR directory with EXT extension.

ls DIR | grep .*\.EXT$ | wc | awk '{print $1}'

You can format the message appropriately to match your requirements.

Upvotes: 0

Related Questions