Unix Command: To find number of files with required extension inside a directory

I want to get the total number of files -

  • within all directories and sub directories.
  • with extension .tif/.jpeg/.gif
  • name shouldn't start with a "."

Is it possible to list the number of files as per

for eg.

if the directory structure is like -

MM
|
|-- ART1 
|    |
|    |-- img1.tif
|    |-- img2.tif
|    |-- img3.tif
|    |-- img4.tif
|
|-- ART2 
|    |
|    |-- img1.tif
|    |-- .img2.tif
|    |-- img.doc
|    |-- img4.tif

the result should be like -

MM\ART1 4 files 
MM\ART2 2 files

Total Files: 6

I tried using -

find . -print | grep -E '\.tif+$'

Answers


What about this?

$ find . -mindepth 1 -type f ! -name ".*" \( -name "*.tif" -or -name "*.jpeg" -or -name "*.gif" \) -exec sh -c 'file={}; printf "%s %s %s\n" $(dirname {}) $(basename {}) ${file##*.}' \; | awk '{a[$1]++; b[$3]++; tot++} END{for (i in a) print i, a[i]; for (i in b) print i, b[i]; print tot " files"}'
./ART1 4
./ART2 2
tif 6
6 files

And if we create another file in ART1/foo.jpeg:

$ find . -mindepth 1 -type f ! -name ".*" \( -name "*.tif" -or -name "*.jpeg" -or -name "*.gif" \) -exec sh -c 'file={}; printf "%s %s %s\n" $(dirname {}) $(basename {}) ${file##*.}' \; | awk '{a[$1]++; b[$3]++; tot++} END{for (i in a) print a[i], i; for (i in b) print b[i], i; print tot " files"}'
5 ./ART1
2 ./ART2
1 jpeg
6 tif
7 files
Explanation
$ find . -mindepth 1 -type f ! -name ".*" \( -name "*.tif" -or -name "*.jpeg" -or -name "*.gif" \) -exec sh -c 'file={}; printf "%s %s %s\n" $(dirname {}) $(basename {}) ${file##*.}' \; 
./ART1 img2.tif tif
./ART1 img4.tif tif
./ART1 foo.jpeg jpeg
./ART1 img3.tif tif
./ART1 img1.tif tif
./ART2 img4.tif tif
./ART2 img1.tif tif

This find command looks for files matching what is requested: - file. - name not starting with . - extension tif, jpeg or gif.

Then it prints the directory in which it is stored + the name + the extension.

awk '{a[$1]++; b[$3]++; tot++} END{for (i in a) print a[i], i; for (i in b) print b[i], i; print tot " files"}'

Based on the previous output, it counts: - how many files are there for every directory (1st record, that is $1) - how many files are there with each extension (3rd record, that is $3) - total number of files.

Then it prints them on the END{} block.


Need Your Help

CSS - js rendering links useless

javascript css hyperlink

Ok, well, thanks to people here, I figured out the portion of my js code that's rendering my links useless is what's pasted below... Still trying to figure out why, but if someone has any insight,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.