Counting Number of Files in Multiple Subdirectories from Command Line

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

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

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

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.

How to count all files inside a folder, its subfolder and all . The count should not include folder count

find . -type f | wc -l will recursively list all the files (-type f restricts to only files) in the current directory (replace . with your path). The output of this is piped into wc -l which will count the number of lines.

How can I count all the lines of code in a directory recursively?

Try:

find . -name '*.php' | xargs wc -l

or (when file names include special characters such as spaces)

find . -name '*.php' | sed 's/.*/"&"/' | xargs  wc -l

The SLOCCount tool may help as well.

It will give an accurate source lines of code count for whatever
hierarchy you point it at, as well as some additional stats.

Sorted output:

find . -name '*.php' | xargs wc -l | sort -nr

Batch Count Files And Subfolders in a directory

Aligning columns requires substrings and as this is in a (code block) it requires delayed expansion.

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
(Echo Folders #Sub #Files ##Sub ##Files
FOR /D %%G in (*) DO (
PUSHD "%%G"
Set /A "Sub#=Files#=0,SUB##=Files##=0"
Set "Folders=%%~G "
FOR /F %%H in ('dir /a-d /b 2^>NUL^|find /C /V "" ') DO Set Files#=%%H
FOR /F %%I in ('dir /ad /b 2^>NUL^|find /C /V "" ') DO Set Sub#=%%I
if !Sub#! gtr 0 (
FOR /F %%H in ('dir /a-d /b /S 2^>NUL^|find /C /V "" ') DO Set Files##=%%H
FOR /F %%I in ('dir /ad /b /S 2^>NUL^|find /C /V "" ') DO Set Sub##=%%I
Set /A "Files##-=Files#,Sub##-=Sub#"
)
Set "Sub#= !Sub#!"
Set "Files#= !Files#!"
Set "Sub##= !Sub##!"
Set "Files##= !Files##!"
Echo !Folders:~,15! !Sub#:~-7! !Files#:~-7! !Sub##:~-7! !Files##:~-7!
POPD
)) > "count.txt"
start count.txt

Sample output

Folders            #Sub  #Files   ##Sub ##Files
ManagedWinApi 0 5 0 0
vb 0 16 0 0
PoSh 9 249 2 181
_StackOverflow 1 0 0 5


Related Topics



Leave a reply



Submit