How to Count Number of Files in Each Directory

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

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 ¦) redirects find command's standard output to wc 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.

Count number of files in several folders with Unix command

A possible solution using xargs is to use the -I option, which replaces occurrences of replace-str (% in the code sample below) in the initial-arguments with names read from standard input:

find . -maxdepth 1 -type d -print0 | xargs -0 -I% sh -c 'echo -n "%: "; find "%" -type f | wc -l'

You also need to pass the find command to sh if you want to pipe it with wc, otherwise wc will count files in all directories.

Another solution (maybe less cryptic) is to use a one-liner for loop:

for d in */; do echo -n "$d: "; find "$d" -type f | wc -l; done

Script to count number of files in each directory

Try the below one:

du -a | cut -d/ -f2 | sort | uniq -c | sort -nr

from http://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-the-total-number-of-files-in-a-folder-510009/#post3466477

R - How to count the number of files in each folder

Base R comes with a handy function for this called list.files. Using the pattern argument you can narrow down your search ("." gives you all files). Using all.files = TRUE you could also include hidden files.

folder <- "C:/Users/Johannes Gruber/Pictures"
files <- list.files(folder, pattern = ".", all.files = FALSE, recursive = TRUE, full.names = TRUE)

# number of all files
length(files)
#> [1] 182

You can use split to split up this vector into a list with the content of each folder in separate list elements.

# 1. The number of files in each subfolder
dir_list <- split(files, dirname(files))
files_in_folder <- sapply(dir_list, length)
head(files_in_folder)
#> C:/Users/Pictures
#> 10
#> C:/Users/Pictures/2019/2019-12-30
#> 3
#> C:/Users/Pictures/2019/2019-12-31
#> 9
#> C:/Users/Pictures/2020/2020-01-01
#> 6
#> C:/Users/Pictures/2020/2020-01-03
#> 2
#> C:/Users/Pictures/2020/2020-01-04
#> 26

# 2. Whether that number is odd or even (but this is minor)
even <- sapply(files_in_folder, function(x) x %% 2 == 0)
head(even)
#> C:/Users/Pictures
#> TRUE
#> C:/Users/Pictures/2019/2019-12-30
#> FALSE
#> C:/Users/Pictures/2019/2019-12-31
#> FALSE
#> C:/Users/Pictures/2020/2020-01-01
#> TRUE
#> C:/Users/Pictures/2020/2020-01-03
#> TRUE
#> C:/Users/Pictures/2020/2020-01-04
#> TRUE

Counting number of files in multiple subdirectories from command line

Instead of using find, use a for loop. I am assuming that you are using bash or similar since that is the most common shell on most of the modern Linux distros:

for i in treedir_*; do ls "$i" | wc -l; done

Given the following structure:

treedir_001
|__ a
|__ b
|__ c
treedir_002
|__ d
|__ e
treedir_003
|__ f

The result is:

3
2
1

You can get fancy and print whatever you want around the numbers:

for i in treedir_*; do echo $i: $(ls "$i" | wc -l); done

gives

treedir_001: 3
treedir_002: 2
treedir_003: 1

This uses $(...) to get the output of a command as a string and pass it to echo, which can then print everything on one line.

for i in treedir_*; do echo $i; ls "$i" | wc -l; done

gives

treedir_001
3
treedir_002
2
treedir_003
1

This one illustrates the use of multiple commands in a single loop.

for can be redirected to a file or piped just like any other command, so you can do

for i in treedir_*; do ls "$i" | wc -l; done > list.txt

or better yet

for i in treedir_*; do ls "$i" | wc -l; done | tee list.txt

The second version sends the output to the program tee, which prints it to standard output and also redirects it to a file. This is sometimes nicer for debugging than a simple redirect with >.

find is a powerful hammer, but not everything is a nail...

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))])

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 lines.

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



Leave a reply



Submit