Am wondering if someone can advise on the best way to do this. In my Web Dir is around 100 folders; each folder is a clientID.

Within each ClientID folder is a Sub-Set of folders. In each subset of folders is a set of structured folders that are the same.



Within the Folder of Amazon and Ebay we have a directory structure like:


What I am trying to do is scan the whole Web dir and locate In folders that have more than 50 files in the IN directory.

**edited Included in the IN Directory. Only interested in File count on the IN dir.

  • 2
    The In folder has directories that should be counted too?
    – Kira
    Jan 5 '16 at 14:33
  • If one of the 3 answers solves your problem, don't forget to accept it with the green check-mark. Thank you!
    – Jeff Schaller
    Jan 7 '16 at 14:16

This one-liner should work:

find httpdocs -mindepth 2 -name In | xargs ls -1 | awk '$0 ~ /httpdocs.*In/{name=$0; start=NR} (NR-start)==50{print name;}'

It will first find all In directories, then list all its files, and, finally, filter those with more than 50 files.

Adjust -mindepth according to your start folder.

  • 1
    ls -1 will list the contents of directories if your find happens to discover one. Perhaps you want ls -1d or find ... -type f. But the whole thing will collapse if any of the filenames contains a space anyway.
    – roaima
    Jan 5 '16 at 15:16
  • I'm aware of that, I'm waiting OP to answer my comment...
    – Kira
    Jan 5 '16 at 15:31
  • This is a perfect solution, Sorry for the slow reply Filenames contain no spaces, This give me just what i was after. I was going to start filtering it down by file type, If you wanted to amend to include a filetype i.e. .xml Then that would be perfect. Its not required so if it cannot be done without re-writing then dont worry, But it would help when we have a server crash and am locatnig scripts that have not processed there IN Data. Jan 13 '16 at 17:05

This will loop through the requested directories, counting the files (and subdirectories' files) to see if there are more than 50 files; if so, it echoes the matching directory/directories:

for dir in httpdocs/*/*/In
  count=$(find $dir | wc -l)
  if [ $count -gt 50 ]
    echo $dir has more than 50 files

Note I used strictly "greater-than" 50 because find's output includes that top-level "In" directory in its output to wc -l.

 for dir in `find ./httpdocs/*/*/ -name In -type d` ; do
  count=`find $dir -type f | wc -l`
  if [[ $count -gt 50 ]]
     echo $dir

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.