Count number of files within a directory in Linux?
this is one:
ls -l . | egrep -c '^-'
Note:
ls -1 | wc -l
Which means:ls
: list files in dir
-1
: (that's a ONE) only one entry per line. Change it to -1a if you want hidden files too
|
: pipe output onto...
wc
: "wordcount"
-l
: count l
ines.
How to count number of files in each directory?
Assuming you have GNU find, let it find the directories and let bash do the rest:
find . -type d -print0 | while read -d '' -r dir; do
files=("$dir"/*)
printf "%5d files in directory %s\n" "${#files[@]}" "$dir"
done
How to count the number of files in a directory using Python
os.listdir()
will be slightly more efficient than using glob.glob
. To test if a filename is an ordinary file (and not a directory or other entity), use os.path.isfile()
:
import os, os.path
# simple version for working with CWD
print len([name for name in os.listdir('.') if os.path.isfile(name)])
# path joining version for other paths
DIR = '/tmp'
print len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
Recursively counting files in a Linux directory
This should work:
find DIR_NAME -type f | wc -l
Explanation:
-type f
to include only files.|
(and not¦
) redirectsfind
command's standard output towc
command's standard input.wc
(short for word count) counts newlines, words and bytes on its input (docs).-l
to count just newlines.
Notes:
- Replace
DIR_NAME
with.
to execute the command in the current folder. - You can also remove the
-type f
to include directories (and symlinks) in the count. - It's possible this command will overcount if filenames can contain newline characters.
Explanation of why your example does not work:
In the command you showed, you do not use the "Pipe" (|
) to kind-of connect two commands, but the broken bar (¦
) which the shell does not recognize as a command or something similar. That's why you get that error message.
Counting the number of files in a directory using C
No guarantee that this code compiles, and it's really only compatible with Linux and the BSDs:
#include <dirent.h>
...
int file_count = 0;
DIR * dirp;
struct dirent * entry;
dirp = opendir("path"); /* There should be error handling after this */
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_REG) { /* If the entry is a regular file */
file_count++;
}
}
closedir(dirp);
Find the number of files in a directory
readdir is not as expensive as you may think. The knack is avoid stat'ing each file, and (optionally) sorting the output of ls.
/bin/ls -1U | wc -l
avoids aliases in your shell, doesn't sort the output, and lists 1 file-per-line (not strictly necessary when piping the output into wc).
The original question can be rephrased as "does the data structure of a directory store a count of the number of entries?", to which the answer is no. There isn't a more efficient way of counting files than readdir(2)/getdents(2).
How can I find the number of files within a directory?
You could use .glob()
or .rglob()
:
from pathlib import Path
fileDir = Path("C:/Users/Jonas/Desktop/Test")
excelFiles = fileDir.rglob('*.xl*')
print(len(list(excelFiles)))
>>> 3
Efficient way to find number of files in directory
It appears there is no easy way.. while documentation says that there are begin and end defined for directory_iterator, it doesn't behave like a range-based one.
directory_iterator begin( directory_iterator iter ) noexcept;
(1) (since C++17)
directory_iterator end( const directory_iterator& ) noexcept;
(2) (since C++17)
1) Returns
iter
unchanged2) Returns a default-constructed
directory_iterator
, which serves as the end iterator. The argument is
ignored. These non-member functions enable the use of
directory_iterators
with range-based for loops.
#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::directory_iterator a(".");
for(auto& p: a)
std::cout << p.path() << '\n';
//std::cout << (std::end(a) - std::begin(a)) << '\n'; // apparently impossible,
std::cout <<
std::distance(a,
fs::directory_iterator()) << '\n'; // always returns 1
std::cout <<
std::distance(fs::directory_iterator("."),
fs::directory_iterator{}) << '\n'; // need a new iterator
}
Whenever you increment directory_iterator, it WILL NOT be equal to default constructed unless you reach the end of list. Increment operator is the "magic" which scans file system for next entry. The way to do this using standard library is:
fs::directory_iterator a(".");
int count = 0;
for(auto p: a) {
++count;
}
std::cout << count << '\n';
This is exactly what std::distance
does. The state of iterator changes during increment, which must be taken in account.
Obviously there is problem that content of directory can change during the loop execution, so actual loop might look much more complex.
Python count files in a directory and all its subdirectories
IIUC, you can just do
sum(len(files) for _, _, files in os.walk('path/to/folder'))
or perhaps, to avoid the len
for probably slightly better performance:
sum(1 for _, _, files in os.walk('folder_test') for f in files)
Related Topics
How to Execute Shell Builtin from Scala
How to Update Minikube Latest Version
Using Output of Previous Commands in Bash
What Is Eof!! in The Bash Script
Os X Permission Denied for /Usr/Local/Lib
Openssl/Rsa - Using a Public Key to Decrypt
On-The-Fly Output Redirection, Seeing The File Redirection Output While The Program Is Still Running
How to Create a Multi Partition Sd Disk Image Without Root Privileges
Set Environment Variables in an Aws Instance
Why Doesn't Tar Preserve File Permissions
Siege Aborted Due to Excessive Socket Failure
Identify Program That Connects to a Unix Domain Socket
Why I Am Not Getting Signal Sigkill on Kill -9 Command in Bash
How to Get Errno When Epoll_Wait Returns Epollerr
Using "Touch" to Create Directories