I have a folder and I want count all regular files in it, and for this I use this bash command:
find pathfolder -type f 2> err.txt | wc -l
In the folder there are 3 empty text files and a subfolder with inside it other text files. For this reason I should get 3 as a result, but I get 6 and I don't understand why. Maybe there is some options that I did not set.
If I remove the subfolder I get 4 as result
Below solutions ignore the filenames starting with dot.
To count the files in
pathfolder
only:To count the files in ALL child directories of
pathfolder
:UPDATE: Converting comments into an answer
Based on the suggestions received from anubhava, by creating a dummy file using the command
touch $'foo\nbar'
, thewc -l
counts this filename twice, like in below example:To avoid this, get rid of the newlines before calling
wc
(anubhava's solution):or avoid calling
wc
at all: