ishkee
ishkee

Reputation: 213

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

I want to get the total number of files -

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+$'

Upvotes: 2

Views: 344

Answers (1)

fedorqui
fedorqui

Reputation: 290075

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.

Upvotes: 2

Related Questions