Solved

Shell Script to count different extention of each file

Posted on 2014-03-30
4
644 Views
Last Modified: 2014-04-14
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
Comment
Question by:mac_g
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 78

Assisted Solution

by:arnold
arnold earned 250 total points
ID: 39964773
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
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 125 total points
ID: 39966495
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
 
LVL 29

Assisted Solution

by:serialband
serialband earned 125 total points
ID: 39967320
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
 
LVL 78

Accepted Solution

by:
arnold earned 250 total points
ID: 39967460
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question