Thursday, 14 August 2008

Getting an Approximate File Count

A quick Unix tip I thought I should share. If you need to get an approximation of how many files are in a directory and its subdirectories, the du command is a great help. By default, to use the du command you would type something like this:


du .


This command lists all the subdirectories of the current directory and their size in 512 byte blocks.


The following is a more useful version of the command.


du -ak .


That command lists all the subdirectories and files in the current directory and lists their size in kilobytes. This command can be very useful when used with grep to do quick searches.


Finally, if I want to get a line count, I add the following:


du -ak . | wc -l


The wc -l command counts the number of lines in the output of the du command. Since there is one line per directory and file its not an exact file count, but it can be real useful for quick comparisons and the like.



No comments:

Post a Comment