Solved

Shell Script to count different extention of each file

Posted on 2014-03-30
4
618 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 76

Assisted Solution

by:arnold
arnold earned 250 total points
Comment Utility
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
Comment Utility
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 27

Assisted Solution

by:serialband
serialband earned 125 total points
Comment Utility
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 76

Accepted Solution

by:
arnold earned 250 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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é.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

772 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

14 Experts available now in Live!

Get 1:1 Help Now