Click to See Complete Forum and Search --> : How to count files per type in the current directory (or including sub directory?
dba_pinay
08-18-2007, 10:22 AM
Hello,
I'm thinking how can I display the total count files per file type in the current directory? (or including subdirectory?)
Or is it possible to display the count of file per type?
Example:
Current Directory: /
File Type Qty
SH File 10
ORA File 3
....or
.sh 10
.ora 3
.env 5
Any help from you guys is really appreciated.
thanks in advance
DrChuck
08-18-2007, 11:10 AM
You can combine ls with wc (word count) to do what you want:
ls -1 *.dat |wc -1
Note that the argument to ls is a numeric "one". The argument to wc is a lowercase "L". (They look the same in my font) Repeat this in a "for" loop for all the file types you want to test.
hotcold
08-18-2007, 11:55 AM
Hi.
I had something like that from some time ago:
#!/bin/sh
# @(#) s3 Demonstrate tabulation of files by suffix.
debug="echo"
debug=":"
set -o nounset
echo
start=${1-.}
echo "GNU bash $BASH_VERSION" >&2
version cut file
if [ "$start" = "." ]
then
echo
echo " Sample of files in current directory with a suffix, e.g. *.*:"
ls *.*
fi
echo
find $start -name '*.*' |
while read line
do
$debug " in loop, line |$line|"
type=${line##*.}
$debug " in loop, type |$type|"
echo "$type"
done |
sort -t: -k2,2 |
uniq -c
exit 0
producing (by default):
% ./s3
GNU bash 2.05b.0(1)-release
cut (coreutils) 5.2.1
file-4.12
Sample of files in current directory with a suffix, e.g. *.*:
com.f one.c t1.txt t2.txt two.c x.ora y.ora
2 c
1 f
2 ora
2 txt
and if you provide a directory:
% ./s3 ~/projects
GNU bash 2.05b.0(1)-release
cut (coreutils) 5.2.1
file-4.12
1 err
1 ext
1 mango
1 odt
1 orig
2 out
1 pecan
2 plx
4 sh
1 t2t
1 tar
21 txt
change it to suit your situation ... cheers, hotcold
dba_pinay
09-04-2007, 01:15 AM
For DrChuck..thanks but I'm not that good yet in scripting in linux...
For Hotcold...this is the only time I tested the script. Then I tried it in one of the test instance that I'm using here at work.
The script is pretty cool though, it counts all the file existing including the subdirectory exist in the directory I indicated...
Thanks a lot on this, this is a great help to me because I can use it during verifying if copying a lots of file and then compare both the source and destination folder...
ghostdog74
09-04-2007, 08:08 AM
try this
ls -1 | xargs file
i leave you to do the counting part....