I had some directory, with large number of files. Every time I tried to access the list of files within it, I was not able to do that or there was significant delay. I was trying to use ls
command within command-line on Linux and web interface from my hosting provider did not help also.
The problem is, that when I just do ls
, it takes significant amount of time to even start displaying something. Thus, ls | wc -l
would not help also.
After some research I came up with this code (in this example it counts number of new emails on some server):
print sum([len(files) for (root, dirs, files) in walk('/home/myname/Maildir/new')])
The above code is written in Python. I used Python's command-line tool and it worked pretty fast (returned result instantly).
I am interested in the answer to the following question: is it possible to count files in a directory (without subdirectories) faster? What is the fastest way to do that?
To count the folders and files in a folder, open the Command Prompt and run the following command: dir /a:-d /s /b "Folder Path" | find /c ":". For example, we wanted to count the files and subfolders in our "E:\OneDrive\Documents" folder, so we had to run dir /a:-d /s /b "E:\OneDrive\Documents" | find /c ":".
Open Windows Explorer. Navigate to the folder containing the files you want to count. In the bottom left portion of the window, it displays how many items (files and folders) are in the current directory.
To determine how many files there are in the current directory, put in ls -1 | wc -l. This uses wc to do a count of the number of lines (-l) in the output of ls -1.
du command -h option : Display sizes in human readable format (e.g., 1K, 234M, 2G). du command -s option : It shows only a total for each argument (summary). du command -x option : Skip directories on different file systems. sort command -r option : Reverse the result of comparisons.
I'm not sure about speed, but if you want to just use shell builtins this should work:
#!/bin/sh
COUNT=0;
for file in /path/to/directory/*
do
COUNT=$(($COUNT+1));
done
echo $COUNT
Total number of files in the given directory
find . -maxdepth 1 -type f | wc -l
Total number of files in the given directory and all subdirectories under it
find . -type f | wc -l
For more details drop into a terminal and do man find
ls
does a stat(2)
call for every file. Other tools, like find(1)
and the shell wildcard expansion, may avoid this call and just do readdir
. One shell command combination that might work is find dir -maxdepth 1|wc -l
, but it will gladly list the directory itself and miscount any filename with a newline in it.
From Python, the straight forward way to get just these names is os.listdir(directory). Unlike os.walk and os.path.walk, it does not need to recurse, check file types, or make further Python function calls.
Addendum: It seems ls doesn't always stat. At least on my GNU system, it can do only a getdents call when further information (such as which names are directories) is not requested. getdents is the underlying system call used to implement readdir in GNU/Linux.
Addition 2: One reason for a delay before ls outputs results is that it sorts and tabulates. ls -U1 may avoid this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With