Possible Duplicate:
How can I count the number of folders in a drive using Linux?
I have a really deep directory tree on my Linux box. I would like to count all of the files in that path, including all of the subdirectories.
For instance, given this directory tree:
/home/blue
/home/red
/home/dir/green
/home/dir/yellow
/home/otherDir/
If I pass in /home
, I would like for it to return four files. Or, bonus points if it returns four files and two directories. Basically, I want the equivalent of right-clicking a folder on Windows and selecting properties and seeing how many files/folders are contained in that folder.
How can I most easily do this? I have a solution involving a Python script I wrote, but why isn't this as easy as running ls | wc
or similar?
Answer
find . -type f | wc -l
Explanation:find . -type f
finds all files ( -type f ) in this ( . ) directory and in all sub directories, the filenames are then printed to standard out one per line.
This is then piped | into wc
(word count) the -l
option tells wc to only count lines of its input.
Together they count all your files.
Comments
Post a Comment