0

I am using this script to count files on my directory and sub-directories:

for i in $(find . -type d) ; do 
  printf "$i %s\t" ;
  ( find $i -type f | wc -l ) ; 
done

This script works fine. What I really want it to do is to print only the directories that contain more than 31 files.

3

Try:

find . -type d -exec bash -c '[[ $(find "{}" -type f | wc -l) -gt 31 ]] && echo {}'  \;

I'd advise using the -maxdepth restriction on the second find, otherwise you might find some surprising results.

2

Replace the line:

( find $i -type f | wc -l ) ; 

With this:

FILES=$( find $i -type f | wc -l ); 

Then you could ask for it:

if [ "$FILES" -gt 31 ] ; then
  • i tried this but i am getting an error – Moncef Benayad Nov 13 '14 at 15:25
2

This should to it using -gt:

for i in $(find . -type d) ; do   
  NUM=$( find $i -type f | wc -l );
  if [[ $NUM -gt 31 ]]; then 
        echo "$i $NUM" ;   
  fi
done
0

A solution using 's array :

x=( * )
((${#x[@]} > 31)) && printf '%s\n' *

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.