# count how many FILES are in this FOLDER and ALL SUBFOLDERS
find . -type f|wc -l

# count how many FILES are in this FOLDER only (maxdepth 1)
find . -maxdepth 1 -type f | less; # check what lines will be counted: comprehensive list of all files in format ./filename

find . -maxdepth 1 -type f | wc -l; # count all files in the current directory (excluding folders, excluding symbolic links)

# test it
mkdir temp
cd temp
touch 1 2 3
find . -maxdepth 1 -type f | wc -l; # count all files in the current directory
3

actually not precise enough: https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x700.html

in the above test

ls -l | less; # check what lines will be counted
# it will also count leading line "total 0"
ls -l | wc -l
4

because: this will also count directories!

and prone to errors: “It doesn’t count dotfiles”

also bad:

ls -l | grep -v ^l | less; # check what lines will be counted
# it will also count leading line "total 0"
ls -l | grep -v ^l | wc -l

 

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin