Now on ScienceBlogs: The Lights Stay On Inside a Black Hole!

Seed Media Group

Collective Imagination

Greg Laden's Blog

Evolution, Life Sciences, Science Education, Human Evolution, and Stuff

Recent Comments

Profile


Welcome to Greg Laden's Blog.




Nature Blog Network



Search

Join the best atheist themed blogroll!

Archives

Recent Posts

« Current Carnivals | Main | Bugs Can Be Good (Open Source Hardware) »

Linux One Liners

Category: one-liners
Posted on: August 1, 2008 8:14 PM, by Greg Laden

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!

Share this: Stumbleupon Reddit Email + More

TrackBacks

TrackBack URL for this entry: http://scienceblogs.com/mt/pings/77058

Comments

1

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

Posted by: Colin M | August 1, 2008 8:50 PM

2

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]

Posted by: Winter Toad | August 2, 2008 9:01 AM

3

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

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

Posted by: TimJ | August 2, 2008 11:50 AM

4

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

Posted by: Virgil Samms | August 3, 2008 10:52 AM

5

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.

Posted by: Ben Zvan | August 4, 2008 2:03 PM

6

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.

Posted by: Peter Lund | August 19, 2009 3:04 AM

Post a Comment

(Email is required for authentication purposes only. On some blogs, comments are moderated for spam, so your comment may not appear immediately.)





ScienceBlogs

Search ScienceBlogs:

Go to:

Advertisement
Enter to win a free copy of The Monty Hall Problem
Visit the Collective Imagination blog
Advertisement
Collective Imagination

© 2006-2009 Seed Media Group LLC. ScienceBlogs is a registered trademark of Seed Media Group. All rights reserved.

Sites by Seed Media Group: Seed Media Group | ScienceBlogs | SEEDMAGAZINE.COM