Today's article involves more counting (and the terminal, of course)...

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
11,792
Reaction score
10,357
Credits
97,550
Have you ever wanted to count the number of files in a directory? Well, you can do that! You can even count all the files in a directory while including the hidden files in that directory.

All you do is use a pipe to mash the 'ls' and 'wc' commands together.

This was actually just the next notes in that file full of notes. I don't normally go counting files, though I do sometimes use it to check and see if the same number of files was copied over. It's just not that often - but I'm sure someone uses this for something. They must!


I do love me some feedback. I will note that you may want to leave the comment on the site if you want to add to it in some permanent sense. Some otherwise good comments are lost to the ether when left here.
 


Using wc and ls to find the number of files can become a little confounded. Take this example looking into a directory name "anoth" (using the -F option to show files and directories in terminal black and white without color):
Code:
[flip@flop ~]$ ls -F anoth
filea  fileb  filec  filedd  newdir/  ots/

There are 4 unhidden files and 2 directories.
Running with ls and wc, the output is actually the number of lines, which in this case does not correspond to the number of ordinary unhidden files of which there are 4:

Code:
[flip@flop ~]$ ls -F | wc -l
6

Directories are actually files in linux which carry the information about the files that they contain, but they are not commonly thought of as ordinary files. If the user wants the total of files and directories, then the command works, but that may not be the wished for outcome.

When using the long forms of the ls command, there are other confounds in the ouputs. First, to see what's in the directory "anoth":

Code:
[flip@flop ~]$ ls -al anoth
total 24
drwxr-xr-x 4 flip ben 4096 Jan  5 08:55 .
drwxr-xr-x 7 flip ben 4096 Apr 11 15:03 ..
-rw-r--r-- 1 flip ben    6 Mar 31 20:04 filea
-rw-r--r-- 1 flip ben    6 Mar 31 20:04 fileb
-rw-r--r-- 1 flip ben    0 Sep 14  2022 filec
-rw-r--r-- 1 flip ben    0 Sep 14  2022 filedd
drwxr-xr-x 2 flip ben 4096 Aug  2  2022 newdir
-rw-r--r-- 1 flip ben    0 Sep 14  2022 .newfile
drwxr-xr-x 3 flip ben 4096 Feb 12 07:48 ots

There one finds the 4 unhidden files, a newly found hidden file, and the 2 directories as before, but another 2 directories are output: . and .., and further a line with "total 24" is output, all adding to the line count which wc will pick up, as follows:

Code:
[flip@flop ~]$ ls -al anoth | wc -l
10

If one is after the number of what I'm referring to as ordinary files, unhidden and hidden, then these commands are providing something else. There are only 5 such files, but the output is 10 which can be very misleading for the unaware.

If the user just wants to output the number of ordinary files both hidden and unhidden, one can run a command like:

Code:
ls -al |grep ^- |wc -l

which applied to the example directory "anoth" yields the expected result:

Code:
[flip@flop ~]$ ls -al anoth | grep ^- |wc -l
5
 
Last edited:
Using wc and ls to find the number of files can become a little confounded.

Nice read, thanks. I tried to make it straight forward, avoiding the -l flag. I also didn't consider all that (which is great, 'cause I swear that I learn more writing these articles than my readers learn).

Hmm... I'll have to come up with an edit to be more clear.
 
Hi,

here is a short version.
Code:
ls -1 mydir | wc -l

ls -1 shows the output of ls in 1 line per dir/file
wc -l count the lines
If there are directories in "mydir", the ls -1 command will still output them in its listing, and wc -l will count them which still will produce the inaccurate number of ordinary files as described in post #2 in relation to the code block showing directories within a directory.
 
Last edited:
I came up with this:

find . -maxdepth 1 -type f | wc -l
 

Members online


Top