A web developing, photo taking, Muay Thai fighting man.

Count the Number of Files in a Directory in Linux

Posted: May 15th, 2009 | Author: James | Filed under: linux | Tags: | 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

Share/Save/Bookmark


3 Comments on “Count the Number of Files in a Directory in Linux”

  1. 1 flipouk said at 9:07 am on May 16th, 2009:

    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.

  2. 2 James said at 9:36 am on May 16th, 2009:

    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’

  3. 3 diaspara said at 9:16 am on May 18th, 2009:

    Use ‘ls -1p | grep -v “/” | wc -l’ to count only files, not directories.


Leave a Reply