tiago
tiago

Reputation: 23502

Script to count number of files in each directory

I need to count the number of files on a large number of directories. Is there an easy way to do this with a shell script (using find, wc, sed, awk or similar)? Just to avoid having to write a proper script in python.

The output would be something like this:

$ <magic_command>
dir1  2
dir2 12
dir3  5

The number after the dir name would be the number of files. A plus would be able to turn counting of dot/hidden files on and off.

Thanks!

Upvotes: 10

Views: 34736

Answers (11)

METAL
METAL

Reputation: 1

You can try out copying the output of ls command in a text file and then count the number of lines in that file.

ls $LOCATION > outText.txt; NUM_FILES=$(wc -w outText.txt); echo $NUM_FILES

Upvotes: 0

Artur Siara
Artur Siara

Reputation: 166

find -type f -printf '%h\n' | sort | uniq -c | sort -n

Upvotes: -1

Chris
Chris

Reputation: 1804

I liked the output from the du based answer, but when I was looking at a large filesystem it was taking ages, so I put together a small ls based script which gives the same output, but much quicker:

for dir in `ls -1A ~/test/`;
do
  echo "$dir `ls -R1Ap ~/test/$dir | grep -Ev "[/:]|^\s*$" | wc -l`"
done

Upvotes: 0

Shubham Chaudhary
Shubham Chaudhary

Reputation: 51143

A generic version of Mehdi Karamosly's solution to list folders of any directory without changing current directory

DIR=~/test/ sh -c 'cd $DIR; du -a | cut -d/ -f2 | sort | uniq -c | sort -nr'

Explanation:

  1. Extract directory into variable
  2. Start new shell
  3. Change directory in that shell so that current shell's directory stays same
  4. Process

Upvotes: 1

nisetama
nisetama

Reputation: 8933

I use these functions:

nf()(for d;do echo $(ls -A -- "$d"|wc -l) "$d";done)
nfr()(for d;do echo $(find "$d" -mindepth 1|wc -l) "$d";done)

Both assume that filenames don't contain newlines.

Here's bash-only versions:

nf()(shopt -s nullglob dotglob;for d;do a=("$d"/*);echo "${#a[@]} $d";done)
nfr()(shopt -s nullglob dotglob globstar;for d;do a=("$d"/**);echo "${#a[@]} $d";done)

Upvotes: 0

Vijay
Vijay

Reputation: 67309

find . -type d | xargs ls -1 | perl -lne 'if(/^\./ || eof){print $a." ".$count;$a=$_;$count=-1}else{$count++}'

below is the test:

> find . -type d
.
./SunWS_cache
./wicked
./wicked/segvhandler
./test
./test/test2
./test/tempdir.
./signal_handlers
./signal_handlers/part2
> find . -type d | xargs ls -1 | perl -lne 'if(/^\./ || eof){print $a." ".$count;$a=$_;$count=-1}else{$count++}'

.: 79
./SunWS_cache: 4
./signal_handlers: 6
./signal_handlers/part2: 5
./test: 6
./test/tempdir.: 0
./test/test2: 0
./wicked: 4
./wicked/segvhandler: 9

Upvotes: 1

tiago
tiago

Reputation: 23502

More or less what I was looking for:

find . -type d -exec sh -c 'echo "{}" `ls "{}" |wc -l`' \;

Upvotes: 4

Guru
Guru

Reputation: 17054

One way like this:

$ for dir in $(find . -type d  )
> do
>  echo $dir $(ls -A $dir | wc -l )
> done

Just remove the -A option if you do not want the hidden file count

Upvotes: 3

Mehdi Karamosly
Mehdi Karamosly

Reputation: 5438

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

Upvotes: 14

udoprog
udoprog

Reputation: 1865

find <dir> -type f | wc -l

find -type f will list all files in the specified directory one at each line, wc -l count the amount of newlines seen from stdin.

Also for future reference: answers like this are a google away.

Upvotes: 6

Milind
Milind

Reputation: 457

try ls | wc it list the file in your directory and gives list of file output to wc as input

Upvotes: 3

Related Questions