Comments on How to get number of files in directory
Parent
Post
+5
−0
Populate an array of the file names and then print how many entries are in the array:
$ ( shopt -s nullglob; files=( .* * ); echo "${#files[@]}" )
124
That will work correctly even if your file names contain newlines, unlike anything piped to wc -l
. I'm using a subshell to restrict the environment that shopt -s nullglob
applies to - if you don't care about that then you don't need a subshell.
Note that the above includes both files and directories (which are, of course, a type of file) - if you want to exclude directories then you need a different solution.
0 comment threads