Solved

Shell Script to count different extention of each file

Posted on 2014-03-30
4
620 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
  • 2
4 Comments
 
LVL 77

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 28

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 77

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now