Count the Number of Files in a Directory in Linux
Posted: May 15th, 2009 | Author: James | Filed under: linux | Tags: linux | 3 Comments »Again, part of the ‘need to write this down somewhere cos i’ll need it at some point in the future’ series – counting the number of files in a directory in Linux.
ls -1 | wc -l
I would use ‘ls -l’ but simply ‘ls’ because ‘ls -l’ includes a line called ‘total’ so if you pipe its output to ‘wc -l’ you will end up having the number of files in the directory + 1.
F.
That’s not ‘ls -l’ but ‘ls -1′ (the number ‘one’). That option makes ls put each entry on a separate line – which is why we can pass it through to ‘wc -l’
Use ‘ls -1p | grep -v “/” | wc -l’ to count only files, not directories.