Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shell Script to count different extention of each file

Posted on 2014-03-30
4
Medium Priority
?
657 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 79

Assisted Solution

by:arnold
arnold earned 750 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 20

Assisted Solution

by:simon3270
simon3270 earned 375 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 30

Assisted Solution

by:serialband
serialband earned 375 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 79

Accepted Solution

by:
arnold earned 750 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

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 …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.
Suggested Courses

664 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