Linux One Liners

ls | wc -l

How many files are in the current directory on my hard drive?

The command ls gives me a list of files (ls stands for “list stuff”)

the vertical line is a pipe. This means the standard output of the left side of the pipe is sent (like in a pipe) to the standard input of the right side.

wc means “word count” … the default output is the number of lines, nmber of words, and number of bytes for a file or for standard input. the -l option puts out only the number of lines. That, then, is the number of files.

Try it!


  1. #1 Colin M
    August 1, 2008

    And for recursive (list the number of files under the current directory and all subdirectories, etc):

    find | wc -l

    If you want to count just normal files (i.e. not directories and other weird things):

    find -type f | wc -l

  2. #2 Winter Toad
    August 2, 2008

    The command in the article will give you the number of directory entries in the current directory that do have a leading dot in the name.

    This isn’t quite the number of files in the directory, as it includes things like symbolic links, multiply-linked single files, subdirectories, and other special directory entries, and it excludes those normally directory entries whose names begin with ‘.’.

    Also, it is actually legal to have a newline in a directory entry, and an entry like that would be counted as two separate objects. In order to avoid that problem, it is usually good to remember that there are two characters that cannot appear in a POSIX directory entry, ASCII NUL and ‘/’.

    The ASCII NUL is an unambiguous separator when referring to path names or directory entries. That’s why ‘find’ has an option switch “-print0″, which prints the entries with a trailing NUL. And ‘xargs’ has the “-0″ switch which accepts arguments separated by NUL.

    So, if the question is “how many regular file directory entries are in this directory?”, I would use a command like this:

    find . -type f -maxdepth 1 -print0 
    | xargs -0 PRINT-ARGC-MINUS-1

    Where PRINT-ARGC-MINUS-1 would be a C program written to print the value of argc less 1.

    There’s a better way, though. How many distinct directory entries for regular files are in this directory, not counting multiple hardlinks more than once?

    find . -type f -maxdepth 1 -printf "%i\n" | sort 
    | uniq | wc -l

    That is, don’t send filenames around, they’re awkward to parse because of the dangers of embedded whitespace and newlines. Instead, let ‘find’ produce a single line guaranteed to contain only numbers for each regular file. I print the inode number, which is shared among multiple hardlinks. Sort the inode list because ‘uniq’ requires a sorted list to work, pipe through ‘uniq’ to get the list of unique inode numbers for directory entries of regular files, with or without a leading ‘.’, in this directory, not descending subdirectories (because of the -maxdepth 1 switch). Count the number of unique inodes, and you have your answer.

    NOTE: In the above code examples, I’ve added a line break to make them fit. They are in fact one line each. …. [the management]

  3. #3 TimJ
    August 2, 2008

    …and my favorite script
    cd /
    find -exec DONTDOTHIS rm {} \;

    (*hint* yes, I’m evil and DO NOT do this!)

  4. #4 Virgil Samms
    August 3, 2008

    /usr/bin/du -sk * | sort -n

  5. #5 Ben Zvan
    August 4, 2008

    The interesting thing about this command is that ‘ls’ doesn’t list one file per line and yet ‘wc’ sees each file as a line. Somebody was thinking ahead.

  6. #6 Peter Lund
    August 19, 2009

    Ben: a program can test whether a file handle (such as stdout) is really a file, or a pipe, or a (pseudo)terminal, or …
    ls does this.

    Winter Toad: as long as we are being pedantic, wouldn’t it be better to use “%D,%i\n” in the printf? Somebody might be playing with union mounts and I don’t believe all union mount implementations “remap” the device numbers, in other words, the inode numbers may clash.