Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
On Linux, to make the command very robust and handle files that might have newlines in their name, use this:
find -maxdepth 1 -type f -print0 | tr -cd '\0' | wc -c
This saves us from the ordeal of parsing ls output.
ls
Related:
find -maxdepth 1 -type f -print0 | wctr --files0-from=-cd '\0' | wc -lc
From man wcRelated:
man wc
--files0-from=F read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input
--files0-from=F
read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input
find -maxdepth 1 -type f -print0 | wc --files0-from=- -l
From man wc: