Using Find command

Hi Friends,

I need to find out the number of files available in each directory.
It should list no of files and directory name.

For ex. if there are directories like : /home/Dir1/Dir2/Dir3

I need the output like the following:

Dir1 - x no of files
Dir2 - y no of files
Dir3 - z no of files.

Can anyone can let me know how to find this information using find command please?

Thanks.

 
find $top_dir -type d | while read d
do
 echo $d: $( ls -A $d|wc -l )
done

If you want to ignore sym-links or dirs, you can "l s -Al $d | grep -c '^-' " for " ls -A $d|wc -l ", and if you want to count sym-linked files, add L next to l in the ls.

Hi,

Thanks for the information. The first code (below code) which is used to find across all the directories is working.

find $top_dir -type d | while read d
do
 echo $d: $( ls -A $d|wc -l )
done

The other one to ignore sym-links or dirs is not working. The following error message has occured.

ls -Al /home/Dir1/Dir2/Dir3| grep -c '^-' " for " ls -A /home/Dir1/Dir2/Dir3| 
wc -l

grep: /home/jbandaru/jaya/naresh: invalid context length argument

It seems the issue with grep command.

Any idea please?

Thanks.

find /home/dir1 -type d | while read DIR; do
  echo "${DIR##*/} has $(find $DIR/* -prune -type f | wc -l) files"
done
dir1 has        3 files
dir2 has        5 files
dir3 has        2 files

Hi Friends,

The following code is working.

find $top_dir -type d | while read d
do
 echo $d: $( ls -A $d|wc -l )
done

But, in our environment there is a space between the two words of a directory. Thisi snot working in the while loop.

ex:

find /stage/landing/test1*test2/LAST -type d | while read d
do
 echo $d: $( ls -A $d|wc -l )
done

I tried with /stage/landing/test1 test2/LAST , but no use.

Quick help is appreciated.

Thanks
Jayaprakash.

---------- Post updated at 09:27 PM ---------- Previous update was at 03:46 PM ----------

Any Response please?

---------- Post updated at 09:27 PM ---------- Previous update was at 09:27 PM ----------

Any Response please?

Sure. Here's one...

Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.

Any response please?

how about:

find "$top_dir" -type d | while read d
do
 echo "$d": $( ls -A "$d" |wc -l )
done

Hi,

The code is working if thereis no gap betwen the two words of a dir.

e.g., /home/Dir1/Dir2/Word1 Word2/ -- > this is not working.

It is treates as 2 directories.
How to avoid this?

Thanks.

Double quotes around, or a ? for the space, if it will be globbed later, or both. White space makes shell pragmas like "for f in *" or even "find . . . | while read f" problematic, but "find . . . | sed '...' | ksh" can deal with adding double quotes before the white space characters are changed or separate things.