Skip to main content
hijacking the accepted answer to include the canonical way, sorry
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Using a broad definition of "file"

ls | wc -l

(note that it doesn't count hidden files and assumes that file names don't contain newline characters).

To include hidden files (except . and ..) and avoid problems with newline characters, the canonical way is:

find . ! -name . -prune -print | grep -c /

Or recursively:

find .//. ! -name . -print | grep -c //

Using a broad definition of "file"

ls | wc -l

(note that it doesn't count hidden files and assumes that file names don't contain newline characters).

Using a broad definition of "file"

ls | wc -l

(note that it doesn't count hidden files and assumes that file names don't contain newline characters).

To include hidden files (except . and ..) and avoid problems with newline characters, the canonical way is:

find . ! -name . -prune -print | grep -c /

Or recursively:

find .//. ! -name . -print | grep -c //
deleted 112 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Using a broad definition of "file"

ls | wc -l

(note that it doesn't count hidden files and assumes a POSIX ls implementation (won't work with older versions of busybox ls where you need the -1 option) and that file names don't contain newline characters).

Using a broad definition of "file"

ls | wc -l

(note that it doesn't count hidden files and assumes a POSIX ls implementation (won't work with older versions of busybox ls where you need the -1 option) and that file names don't contain newline characters).

Using a broad definition of "file"

ls | wc -l

(note that it doesn't count hidden files and assumes that file names don't contain newline characters).

added 219 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Using a broad definition of "file"

ls | wc -l

(note that it doesn't count hidden files and assumes a POSIX ls implementation (won't work with older versions of busybox ls where you need the -1 option) and that file names don't contain newline characters).

Using a broad definition of "file"

ls | wc -l

Using a broad definition of "file"

ls | wc -l

(note that it doesn't count hidden files and assumes a POSIX ls implementation (won't work with older versions of busybox ls where you need the -1 option) and that file names don't contain newline characters).

Source Link
James
  • 4.8k
  • 1
  • 18
  • 4
Loading