简体   繁体   中英

The fastest way to count the number of files in a directory (including subdirectories)

I'm running a script that looks at all the files in a directory and its subdirectories.

The script has been running for a day, and I'd like to estimate how long it will keep running. I know how many files it processed so far (73,000,000), but I don't know the total number of files.

What is the fastest way to count the files?

I tried right-clicking on the directory and selecting "properties", and it's slowly counting up. I tried redirecting ls into a file, and it's just churning & churning...

Should I write a program in c?

The simplest way:

find <dir> -type f | wc -l

Slightly faster, perhaps:

find <dir> -type f -printf '\n' | wc -l

I did a quick research. Using a directory with 100,000 files I compared the following commands:

ls -R <dir>
ls -lR <dir>
find <dir> -type f

I ran them twice, once redirecting into a file ( >file ), and once piping into wc ( |wc -l ). Here are the run times in seconds:

        >file   |wc
ls -R     14     14
find      89     56
ls -lR    91     82

The difference between >file and |wc -l is smaller than the difference between ls and find .

It appears that ls -R is at least 4x faster than find .

Fastest I know about:

ls | wc -l

Note: keep in mind though that it lists all nodes inside a directory, including subdirectories and the two references to the current and the parent directory ( . & .. ).

If you need the recursive count of files in all subdirectories (as opposed to everything including subdirectories inside the current directory), then you can add the "recursive" flag to the ls command:

ls -R | wc -l

If you compare this in speed to the suggestion using find you will see that it is much faster (factor 2 to 10), but keep in mind the note above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM