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!

Tags

More like this

A repost, continuing along the lines of bashing the shell. Having examined Learning the bash Shell (In a Nutshell (O'Reilly)) (see here, here, and here), it is now time to turn to a more advanced reference to help you geek out on your Linux computer. If you want to have only one book on bash,…
I have a file called "email.txt" and I want to count the number of lines in it. cat email.txt | wc -l cat email.txt | sed -n '$=' The first example uses wc (which stands for "what's the count") with the -l (lines only) optin. The contents is pushed via cat through standard output into a pipe…
I thought I was done with the command line for the week, but then I did something cool that I thought I'd share with you. Linux users only ... others will think this is silly ... join me below the fold. OK, are we alone? Good. It's nice to be away from all those Windows Symps for a while. Oh…
I know all my fellow bloggers are jealous about my Linux calendar posts (like this one) and normally I don't reveal my secrets. But this is so cool I have to share it. The Linux calendar command (in the terminal window) puts out, by default, a listing of events, etc. from today and tomorrow.…

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

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]

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

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

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

By Virgil Samms (not verified) on 03 Aug 2008 #permalink

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.

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.

By Peter Lund (not verified) on 18 Aug 2009 #permalink