Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • Last Modified:

Shell Script to count different extention of each file

Our application we have lot of files.
each file has it's  3-different extention.

for example.....

testfile.ext1
testfile.ext2
testfile.ext2

report.ext1
report.ext2



I want to have shell script, which can test, how many extentions had for each file.
so that, I can easily track missting files.

In otherwords .... , from the above example of files which were listed, I want to have out put like below.

OutPut
==================
testfile ----- has count ---- 3
report  ----- has count -----2

please advice
0
mac_g
Asked:
mac_g
  • 2
4 Solutions
 
arnoldCommented:
The simplest is to use perl
Presuming you are looking at the current level
As well as there is only one period in the filename and

ls | perl -e 'while (<>) {
chomp();
next if test -d "$_";
($filename,$extension)=split($_,2);
$list{$filename}+=1;
}
foreach $extension (keys %list) {
print "$extension --------> $list{$extension}\n";
}
'

Remove the last characters if it is a Control character(cr or lf)
skip directory names in the listing.
Build a hash with the name portion of the file.
0
 
simon3270Commented:
Or using standard tools:
$ ls
double.ext1  mine.ext1	  report.ext2	 testfile.ext2
double.ext2  report.ext1  testfile.ext1  testfile.ext3
$ ls | sed 's/\.[^.]*$//'| sort | uniq -c
      2 double
      1 mine
      2 report
      3 testfile
$

Open in new window

0
 
serialbandCommented:
Or with awk instead of sed:

ls |awk -F"." '{print $1}'|uniq -c

The extra sort in the sed example may not be necessary with a standard presorted ls.
0
 
arnoldCommented:
simon3270 in the sed pattern match took care of having a period in the name
user.lastname.ext which I "excluded as a posibility.

The issue that is not clear is whether the user is looking through a directory hierarchy
such that you can have
file.txt
directory1\file.txt

i.e. user A always copies and saves files into directory1.

Yes, I realize that similarly named files are not necessarily the same file with the same content.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now