Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to extract information how much files are in some directory tree:

  /.../testRoot/test1/test11/..../file1
  /.../testRoot/test1/test11/file2
  /.../testRoot/test1/test11/..../file3
  /.../testRoot/test1/test11/file4
.....................................
  /.../testRoot/test1/test1n/fileq
  /.../testRoot/test1/test1n/..../filew
  /.../testRoot/test1/test1n/filee
  /.../testRoot/test1/test1n/.../.../ .../filer

How to calculate how many files are inside of testRoot? Thanks.

share|improve this question

migrated from stackoverflow.com Feb 23 '12 at 19:17

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 8 down vote accepted

find /path/to/testRoot -type f | wc -l

share|improve this answer
1  
This will break for files with newlines in the name. –  Chris Down Feb 23 '12 at 20:39
1  
And also won't count symlinks, sockets, device specials, and anything other than plain ole boring files. Which is okay in many cases. If files with newlines are an issue, you can try find /path/to/testRoot -type f | awk -v RS='\0' '{n++} END {print n}' –  Alexios Feb 24 '12 at 17:58

In bash4 and above:

shopt -s nullglob globstar
i=0
for f in /path/to/testRoot/*/**; do
    [[ -f $f ]] && (( i++ ))
done
echo "$i"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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