14

I want to know how many regular files have the extension .c in a large complex directory structure, and also how many directories these files are spread across. The output I want is just those two numbers.

I've seen this question about how to get the number of files, but I need to know the number of directories the files are in too.

  • My filenames (including directories) might have any characters; they may start with . or - and have spaces or newlines.
  • I might have some symlinks whose names end with .c, and symlinks to directories. I don't want symlinks to be followed or counted, or I at least want to know if and when they are being counted.
  • The directory structure has many levels and the top level directory (the working directory) has at least one .c file in it.

I hastily wrote some commands in the (Bash) shell to count them myself, but I don't think the result is accurate...

shopt -s dotglob
shopt -s globstar
mkdir out
for d in **/; do
     find "$d" -maxdepth 1 -type f -name "*.c" >> out/$(basename "$d")
done
ls -1Aq out | wc -l
cat out/* | wc -l

This outputs complaints about ambiguous redirects, misses files in the current directory, and trips up on special characters (for example, redirected find output prints newlines in filenames) and writes a whole bunch of empty files (oops).

How can I reliably enumerate my .c files and their containing directories?


In case it helps, here are some commands to create a test structure with bad names and symlinks:

mkdir -p cfiles/{1..3}/{a..b} && cd cfiles
mkdir space\ d
touch -- i.c -.c bad\ .c 'terrible
.c' not-c .hidden.c
for d in space\ d 1 2 2/{a..b} 3/b; do cp -t "$d" -- *.c; done
ln -s 2 dirlink
ln -s 3/b/i.c filelink.c

In the resulting structure, 7 directories contain .c files, and 29 regular files end with .c (if dotglob is off when the commands are run) (if I've miscounted, please let me know). These are the numbers I want.

Please feel free not to use this particular test.

N.B.: Answers in any shell or other language will be tested & appreciated by me. If I have to install new packages, no problem. If you know a GUI solution, I encourage you to share (but I might not go so far as to install a whole DE to test it) :) I use Ubuntu MATE 17.10.

  • Writing a program to deal with bad programming habits turned out to be quite challenging ;) – WinEunuuchs2Unix Apr 13 '18 at 0:21
16

I haven't examined the output with symlinks but:

find . -type f -iname '*.c' -printf '%h\0' |
  sort -z |
  uniq -zc |
  sed -zr 's/([0-9]) .*/\1 1/' |
  tr '\0' '\n' |
  awk '{f += $1; d += $2} END {print f, d}'
  • The find command prints the directory name of each .c file it finds.
  • sort | uniq -c will gives us how many files are in each directory (the sort might be unnecessary here, not sure)
  • with sed, I replace the directory name with 1, thus eliminating all possible weird characters, with just the count and 1 remaining
  • enabling me to convert to newline-separated output with tr
  • which I then sum up with awk, to get the total number of files and the number of directories that contained those files. Note that d here is essentially the same as NR. I could have omitted inserting 1 in the sed command, and just printed NR here, but I think this is slightly clearer.

Up until the tr, the data is NUL-delimited, safe against all valid filenames.


With zsh and bash, you can use printf %q to get a quoted string, which would not have newlines in it. So, you might be able to do something like:

shopt -s globstar dotglob nocaseglob
printf "%q\n" **/*.c | awk -F/ '{NF--; f++} !c[$0]++{d++} END {print f, d}'

However, even though ** is not supposed to expand for symlinks to directories, I could not get the desired output on bash 4.4.18(1) (Ubuntu 16.04).

$ shopt -s globstar dotglob nocaseglob
$ printf "%q\n" ./**/*.c | awk -F/ '{NF--; f++} !c[$0]++{d++} END {print f, d}'
34 15
$ echo $BASH_VERSION
4.4.18(1)-release

But zsh worked fine, and the command can be simplified:

$ printf "%q\n" ./**/*.c(D.:h) | awk '!c[$0]++ {d++} END {print NR, d}'
29 7

D enables this glob to select dot files, . selects regular files (so, not symlinks), and :h prints only the directory path and not the filename (like find's %h) (See sections on Filename Generation and Modifiers). So with the awk command we just need to count the number of unique directories appearing, and the number of lines is the file count.

  • That's awesome. Uses exactly what's needed and no more. Thank you for teaching :) – Zanna Apr 9 '18 at 8:55
  • @Zanna if you post some commands to recreate a directory structure with symlinks, and the expected output with symlinks, I might be able to fix this accordingly. – muru Apr 9 '18 at 8:57
  • I have added some commands to make a (needlessly complicated as usual) test structure with symlinks. – Zanna Apr 9 '18 at 9:58
  • @Zanna I think this command doesn't need any adjustments to get 29 7. If I add -L to find, that goes up to 41 10. Which output do you need? – muru Apr 9 '18 at 10:09
  • 1
    Added a zsh + awk method. There's probably some way to get zsh itself to print the count for me, but no idea how. – muru Apr 10 '18 at 4:38
11

Python has os.walk, which makes tasks like this easy, intuitive, and automatically robust even in the face of weird filenames such as those that contain newline characters. This Python 3 script, which I had originally posted in chat, is intended to be run in the current directory (but it doesn't have to be located in the current directory, and you can change what path it passes to os.walk):

#!/usr/bin/env python3

import os

dc = fc = 0
for _, _, fs in os.walk('.'):
    c = sum(f.endswith('.c') for f in fs)
    if c:
        dc += 1
        fc += c
print(dc, fc)

That prints the count of directories that directly contain at least one file whose name ends in .c, followed by a space, followed by the count of files whose names end in .c. "Hidden" files--that is, files whose names start with .--are included, and hidden directories are similarly traversed.

os.walk recursively traverses a directory hierarchy. It enumerates all the directories that are recursively accessible from the starting point you give it, yielding information about each of them as a tuple of three values, root, dirs, files. For each directory it traverses to (including the first one whose name you give it):

  • root holds the pathname of that directory. Note that this is totally unrelated to the system's "root directory" / (and also unrelated to /root) though it would go to those if you start there. In this case, root starts at the path .--i.e., the current directory--and goes everywhere below it.
  • dirs holds a list of the pathnames of all the subdirectories of the directory whose name is currently held in root.
  • files holds a list of the pathnames of all the files that reside in the directory whose name is currently held in root but that are not themselves directories. Note that this includes other kinds of files than regular files, including symbolic links, but it sounds like you don't expect any such entries to end in .c and are interested in seeing any that do.

In this case, I only need to examine the third element of the tuple, files (which I call fs in the script). Like the find command, Python's os.walk traverses into subdirectories for me; the only thing I have to inspect myself is the names of the files each of them contains. Unlike the find command, though, os.walk automatically provides me a list of those filenames.

That script does not follow symbolic links. You very probably don't want symlinks followed for such an operation, because they could form cycles, and because even if there are no cycles, the same files and directories may be traversed and counted multiple times if they are accessible through different symlinks.

If you ever did want os.walk to follow symlinks--which you usually wouldn't--then you can pass followlinks=true to it. That is, instead of writing os.walk('.') you could write os.walk('.', followlinks=true). I reiterate that you would rarely want that, especially for a task like this where you are recursively enumerating an entire directory structure, no matter how big it is, and counting all the files in it that meet some requirement.

7

Find + Perl:

$ find . -type f -iname '*.c' -printf '%h\0' | 
    perl -0 -ne '$k{$_}++; }{ print scalar keys %k, " $.\n" '
7 29

Explanation

The find command will find any regular files (so no symlinks or directories) and then print the name of directory they are in (%h) followed by \0.

  • perl -0 -ne : read the input line by line (-n) and apply the script given by -e to each line. The -0 sets the input line separator to \0 so we can read null-delimited input.
  • $k{$_}++ : $_ is a special variable that takes the value of the current line. This is used as a key to the hash %k, whose values are the number of times each input line (directory name) was seen.
  • }{ : this is a shorthand way of writing END{}. Any commands after the }{ will be executed once, after all input has been processed.
  • print scalar keys %k, " $.\n": keys %k returns an array of the keys in the hash %k. scalar keys %k gives the number of elements in that array, the number of directories seen. This is printed along with the current value of $., a special variable that holds the current input line number. Since this is run at the end, the current input line number will be the number of the last line, so the number of lines seen so far.

You could expand the perl command to this, for clarity:

find  . -type f -iname '*.c' -printf '%h\0' | 
    perl -0 -e 'while($line = <STDIN>){
                    $dirs{$line}++; 
                    $tot++;
                } 
                $count = scalar keys %dirs; 
                print "$count $tot\n" '
4

Here's my suggestion:

#!/bin/bash
tempfile=$(mktemp)
find -type f -name "*.c" -prune >$tempfile
grep -c / $tempfile
sed 's_[^/]*$__' $tempfile | sort -u | grep -c /

This short script creates a tempfile, finds every file in and under the current directory ending in .c and writes the list to the tempfile. grep is then used to count the files (following How can I get a count of files in a directory using the command line?) twice: The second time, directories that are listed multiple times are removed using sort -u after stripping filenames from each line using sed.

This also works properly with newlines in filenames: grep -c / counts only lines with a slash and therefore considers only the first line of a multi-line filename in the list.

Output

$ tree
.
├── 1
│   ├── 1
│   │   ├── test2.c
│   │   └── test.c
│   └── 2
│       └── test.c
└── 2
    ├── 1
    │   └── test.c
    └── 2

$ tempfile=$(mktemp);find -type f -name "*.c" -prune >$tempfile;grep -c / $tempfile;sed 's_[^/]*$__' $tempfile | sort -u | grep -c /
4
3
4

Small shellscript

I suggest a small bash shellscript with two main command lines (and a variable filetype to make it easy to switch in order to look for other file types).

It does not look for or in symlinks, only regular files.

#!/bin/bash

filetype=c
#filetype=pdf

# count the 'filetype' files

find -type f -name "*.$filetype" -ls|sed 's#.* \./##'|wc -l | tr '\n' ' '

# count directories containing 'filetype' files

find -type d -exec bash -c "ls -AF '{}'|grep -e '\.'${filetype}$ -e '\.'${filetype}'\*'$ > /dev/null && echo '{} contains file(s)'" \;|grep 'contains file(s)$'|wc -l

Verbose shellscript

This is a more verbose version that also considers symbolic links,

#!/bin/bash

filetype=c
#filetype=pdf

# counting the 'filetype' files

echo -n "number of $filetype files in the current directory tree: "
find -type f -name "*.$filetype" -ls|sed 's#.* \./##'|wc -l

echo -n "number of $filetype symbolic links in the current directory tree: "
find -type l -name "*.$filetype" -ls|sed 's#.* \./##'|wc -l
echo -n "number of $filetype normal files in the current directory tree: "
find -type f -name "*.$filetype" -ls|sed 's#.* \./##'|wc -l
echo -n "number of $filetype symbolic links in the current directory tree including linked directories: "
find -L -type f -name "*.$filetype" -ls 2> /tmp/c-counter |sed 's#.* \./##' | wc -l; cat /tmp/c-counter; rm /tmp/c-counter

# list directories with and without 'filetype' files (good for manual checking; comment away after test)
echo '---------- list directories:'
 find    -type d -exec bash -c "ls -AF '{}'|grep -e '\.'${filetype}$ -e '\.'${filetype}'\*'$ > /dev/null && echo '{} contains file(s)' || echo '{} empty'" \;
echo ''
#find -L -type d -exec bash -c "ls -AF '{}'|grep -e '\.'${filetype}$ -e '\.'${filetype}'\*'$ > /dev/null && echo '{} contains file(s)' || echo '{} empty'" \;

# count directories containing 'filetype' files

echo -n "number of directories with $filetype files: "
find -type d -exec bash -c "ls -AF '{}'|grep -e '\.'${filetype}$ -e '\.'${filetype}'\*'$ > /dev/null && echo '{} contains file(s)'" \;|grep 'contains file(s)$'|wc -l

# list and count directories including symbolic links, containing 'filetype' files
echo '---------- list all directories including symbolic links:'
find -L -type d -exec bash -c "ls -AF '{}' |grep -e '\.'${filetype}$ -e '\.'${filetype}'\*'$ > /dev/null && echo '{} contains file(s)' || echo '{} empty'" \;
echo ''
echo -n "number of directories (including symbolic links) with $filetype files: "
find -L -type d -exec bash -c "ls -AF '{}'|grep -e '\.'${filetype}$ -e '\.'${filetype}'\*'$ > /dev/null && echo '{} contains file(s)'" \; 2>/dev/null |grep 'contains file(s)$'|wc -l

# count directories without 'filetype' files (good for checking; comment away after test)

echo -n "number of directories without $filetype files: "
find -type d -exec bash -c "ls -AF '{}'|grep -e '\.'${filetype}$ -e '\.'${filetype}'\*'$ > /dev/null || echo '{} empty'" \;|grep 'empty$'|wc -l

Test output

From short shellscript:

$ ./ccntr 
29 7

From verbose shellscript:

$ LANG=C ./c-counter
number of c files in the current directory tree: 29
number of c symbolic links in the current directory tree: 1
number of c normal files in the current directory tree: 29
number of c symbolic links in the current directory tree including linked directories: 42
find: './cfiles/2/2': Too many levels of symbolic links
find: './cfiles/dirlink/2': Too many levels of symbolic links
---------- list directories:
. empty
./cfiles contains file(s)
./cfiles/2 contains file(s)
./cfiles/2/b contains file(s)
./cfiles/2/a contains file(s)
./cfiles/3 empty
./cfiles/3/b contains file(s)
./cfiles/3/a empty
./cfiles/1 contains file(s)
./cfiles/1/b empty
./cfiles/1/a empty
./cfiles/space d contains file(s)

number of directories with c files: 7
---------- list all directories including symbolic links:
. empty
./cfiles contains file(s)
./cfiles/2 contains file(s)
find: './cfiles/2/2': Too many levels of symbolic links
./cfiles/2/b contains file(s)
./cfiles/2/a contains file(s)
./cfiles/3 empty
./cfiles/3/b contains file(s)
./cfiles/3/a empty
./cfiles/dirlink empty
find: './cfiles/dirlink/2': Too many levels of symbolic links
./cfiles/dirlink/b contains file(s)
./cfiles/dirlink/a contains file(s)
./cfiles/1 contains file(s)
./cfiles/1/b empty
./cfiles/1/a empty
./cfiles/space d contains file(s)

number of directories (including symbolic links) with c files: 9
number of directories without c files: 5
$ 
4

Simple Perl one liner:

perl -MFile::Find=find -le'find(sub{/\.c\z/ and -f and $c{$File::Find::dir}=++$c}, @ARGV); print 0 + keys %c, " $c"' dir1 dir2

Or simpler with find command:

find dir1 dir2 -type f -name '*.c' -printf '%h\0' | perl -l -0ne'$c{$_}=1}{print 0 + keys %c, " $."'

If you like golfing and have recent (like less than decade old) Perl:

perl -MFile::Find=find -E'find(sub{/\.c$/&&-f&&($c{$File::Find::dir}=++$c)},".");say 0+keys%c," $c"'
find -type f -name '*.c' -printf '%h\0'|perl -0nE'$c{$_}=1}{say 0+keys%c," $."'
2

Consider using the locate command which is much faster than find command.

Running on test data

$ sudo updatedb # necessary if files in focus were added `cron` daily.
$ printf "Number Files: " && locate -0r "$PWD.*\.c$" | xargs -0 -I{} sh -c 'test ! -L "$1" && echo "regular file"' _  {} | wc -l &&  printf "Number Dirs.: " && locate -r "$PWD.*\.c$" | sed 's%/[^/]*$%/%' | uniq -cu | wc -l
Number Files: 29
Number Dirs.: 7

Thanks to Muru for his answer to help me through stripping symbolic links out of the file count in Unix & Linux answer.

Thanks to Terdon for his answer of $PWD (not directed at me) in Unix & Linux answer.


Original answer below referenced by comments

Short Form:

$ cd /
$ sudo updatedb
$ printf "Number Files: " && locate -cr "$PWD.*\.c$"
Number Files: 3523
$ printf "Number Dirs.: " && locate -r "$PWD.*\.c$" | sed 's%/[^/]*$%/%' | uniq -c | wc -l 
Number Dirs.: 648
  • sudo updatedb Update database used by locate command if .c files were created today or if you've deleted .c files today.
  • locate -cr "$PWD.*\.c$" locate all .c files in the current directory and it's children ($PWD). Instead of printing file names, and print count with -c argument. The r specifies regex instead of default *pattern* matching which can yield too many results.
  • locate -r "$PWD.*\.c$" | sed 's%/[^/]*$%/%' | uniq -c | wc -l. Locate all *.c files in current directory and below. Remove file name with sed leaving only directory name. Count number of files in each directory using uniq -c. Count number of directories with wc -l.

Start at current directory with one-liner

$ cd /usr/src
$ printf "Number Files: " && locate -cr "$PWD.*\.c$" &&  printf "Number Dirs.: " && locate -r "$PWD.*\.c$" | sed 's%/[^/]*$%/%' | uniq -c | wc -l
Number Files: 3430
Number Dirs.: 624

Notice how file count and directory count have changed. I believe all users have the /usr/src directory and can run above commands with different counts depending on number of installed kernels.

Long Form:

The long form includes the time so you can see how much faster locate is over find. Even if you have to run sudo updatedb it is many times faster than a single find /.

───────────────────────────────────────────────────────────────────────────────────────────
rick@alien:~/Downloads$ sudo time updatedb
0.58user 1.32system 0:03.94elapsed 48%CPU (0avgtext+0avgdata 7568maxresident)k
48inputs+131920outputs (1major+3562minor)pagefaults 0swaps
───────────────────────────────────────────────────────────────────────────────────────────
rick@alien:~/Downloads$ time (printf "Number Files: " && locate -cr $PWD".*\.c$")
Number Files: 3523

real    0m0.775s
user    0m0.766s
sys     0m0.012s
───────────────────────────────────────────────────────────────────────────────────────────
rick@alien:~/Downloads$ time (printf "Number Dirs.: " && locate -r $PWD".*\.c$" | sed 's%/[^/]*$%/%' | uniq -c | wc -l) 
Number Dirs.: 648

real    0m0.778s
user    0m0.788s
sys     0m0.027s
───────────────────────────────────────────────────────────────────────────────────────────

Note: This is all files on ALL drives and partitions. ie we can search for Windows commands too:

$ time (printf "Number Files: " && locate *.exe -c)
Number Files: 6541

real    0m0.946s
user    0m0.761s
sys     0m0.060s
───────────────────────────────────────────────────────────────────────────────────────────
rick@alien:~/Downloads$ time (printf "Number Dirs.: " && locate *.exe | sed 's%/[^/]*$%/%' | uniq -c | wc -l) 
Number Dirs.: 3394

real    0m0.942s
user    0m0.803s
sys     0m0.092s

I have three Windows 10 NTFS partitions automatically mounted in /etc/fstab. Be aware locate knows everything!

Interesting Count:

$ time (printf "Number Files: " && locate / -c &&  printf "Number Dirs.: " && locate / | sed 's%/[^/]*$%/%' | uniq -c | wc -l)
Number Files: 1637135
Number Dirs.: 286705

real    0m15.460s
user    0m13.471s
sys     0m2.786s

It takes 15 seconds to count 1,637,135 files in 286,705 directories. YMMV.

For a detailed breakdown on locate command's regex handling (appears not to be needed in this Q&A but used just in case) please read this: Use "locate" under some specific directory?

Additional reading from recent articles:

  • 1
    This doesn't count the files in a specific directory. As you point out, it counts all files (or directories, or any other type of file) matching .c (note that it will break if there's a file named -.c in the current directory since you're not quoting *.c) and then it will print all directories in the system, irrespective of whether they contain .c files. – terdon Apr 10 '18 at 9:01
  • @terdon You can pass a directory ~/my_c_progs/*.c. It's counting 638 directories with .c programs, the total directories is show later as 286,705. I'll revise the answer to double quote `"*.c". Thanks for the tip. – WinEunuuchs2Unix Apr 10 '18 at 10:03
  • 3
    Yes, you can use something like locate -r "/path/to/dir/.*\.c$", but that isn't mentioned anywhere in your answer. You only give a link to another answer that mentions this but with no explanation of how to adapt it to answer the question being asked here. Your entire answer is focused on how to count the total number of files and directories on the system, which isn't relevant to the question asked which was "how can I count the number of .c files, and the number of directories containing .c files in a specific directory". Also, your numbers are wrong, try it on the example in the OP. – terdon Apr 10 '18 at 10:45
  • @terdon Thanks for your input. I've improved the answer with your suggestions and an answer you posted on other SE site for $PWD variable: unix.stackexchange.com/a/188191/200094 – WinEunuuchs2Unix Apr 11 '18 at 0:45
  • 1
    Now you have to ensure that $PWD doesn't contain characters that maybe special in a regex – muru Apr 11 '18 at 0:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.