count files recursively

Hi,
New to shell scripting.
I am trying to count number of files in a directory that contains lot of sub-directories. Any input on this greatly appreciated.
thank you!

Have you looked at the du command?

or

do a

ls -l *

Try:

find . -type f | wc -l

Hi,

I tried

find /dir_name -type f | wc -l

but this also counts hidden(.) files that i don't want.

find . -type f ! -name ".??*" | wc -l

---------- Post updated at 11:04 PM ---------- Previous update was at 10:35 PM ----------

find . -type f ! -name ".*" | wc -l

Hi ctsgnb,
thanks for ur input.

I am trying to find number of files in a directory that contains subdirs.
script should go to a directory in a given path count no of files in it and print no of files along with last modification date and then come out of that dir and go to next dir and do the same and print output. I tried the following script but obviously its not getting right results. Can anyone tell me how to achieve this.

code that i am trying with is: (i am sure this is messy as i am new to scripting)

#!/bin/bash
for file in /home/admin
do
 if [ -d "$file" ];then
   cd $file
   for file in $PWD
   do
     if [ -d "$file" ];then
       echo -n "File count within directory $file is:"
       find $file -type f ! -name ".*" | wc -l
       echo "last modification time of $file : $(stat -c %y $file)"
     else
       echo "Skipping $file since not a directory"
       cd ..
     fi
   done
 fi
done

Try this :

cd /home/admin
ls -Rl1 | awk -F: 'NF==2&&!($2){x=$1;s[x]=0;next}/^-/{s[x]++}END{for(i in s){print "The dir " i " has " s " files and was last modified :" ;system("stat -c %y " i);print}}'

If you are running on SunOS/Solaris plateform, use "nawk" instead of "awk"

Or something like (you may need to make the code more elegant but):

DIR=/home/admin
find $DIR -type d >>/tmp/dirlist
find $DIR -type f ! -name ".*" >>/tmp/flist
for i in `cat /tmp/dirlist`
do
    n=$(( $(egrep -e "^$i" /tmp/flist | wc -l) - 1 ))
    l=$(stat -c %y "$i")
    echo "Dir $i has $n file and were last modified $l"
done
rm /tmp/dirlist /tmp/flist

or you are looking for something like

count=0
sum(){
cd $1
ls | while read a
do
if [[ -f "$a" ]]
then let count+=1
elif [[ -d "$a" ]]
then let count+=$(sum "$a")
fi
done
echo $count
}
find . -type d | while read b
do
echo $b has $(ksh k $b) files and were last modified $(stat -c %y $b)
done

Hi ctsgnb,
Thank you!

How about if i want to count files in a directory created by year.
I mean i want count files by the year they are created and go to next directory and do the same.

Thanks in advnce!

You are out of luck. No creation date is available. Last modification time will have to do.

Ok. How could i count files according to last modification date ?

Thanks in advance!

How about this:

#! /bin/bash
shopt -s dotglob

function process_directory
{
        local olddir
        olddir=$(pwd)
        cd $1
        for name in *  ; do
                if [[ $name = '*' ]] ; then
                        continue
                elif [[ -d $name ]] ; then
                        process_directory $name
                elif [[ -f $name ]] ; then
                        stat -c "%y\n" $name | sed 's/\(....\).*/\1/'
                fi
        done
        cd $olddir
        return 0
}

process_directory $1 | awk ' {years[$0]++} END {for (y in years) print y, years[y]}'
exit 0
1 Like

Hi thanks all,

I have another question.
I want to find number of files modified 5 years, 4 years and 3 years ago and the disk space they are occupying.
How can i do that ?
Thanks in advance!

any help on this !!!!!!

@Iramsb4u
May I politely suggest that you prepare your question before posting and that you confine your prepared and concise questions to one post.
Don't forget to mention what Operating System and version you have and what Shell you are using.

Anyway. It sounds like homework or coursework to me.

Hi methyl,

I agree that my question should have been clear.
Here what i am trying to do.
To find the space occupied by files modified more than 4 years ago, i wrote following.I am wondering if it is right ?

 
find $file -type d ! -name ".*" -mtime +1460 | wc -l |du -sh

OS:RHEL5
Shell:bash

Thanks in advance!