6

How can I count the number of all files/folders that exist on a system, using the command-line?

I can find it out using a GUI, simply by opening the properties window for the entire / folder, but it would be nice to know how to do it using the command-line.

Would I need a whole series of commands, or will just one be possible?

1
  • 2
    Do you want the number of files or the number of links? Dec 20, 2015 at 4:33

6 Answers 6

11

Since file / folder names can contain newlines:

sudo find / -type f -printf '.' | wc -c
sudo find / -type d -printf '.' | wc -c

This will count any file / folder in the current / directory. But as muru points out you might want to exclude virtual / other filesystems from the count (the following will exclude any other mounted filesystem):

find / -xdev -type f -printf '.' | wc -c
find / -xdev -type d -printf '.' | wc -c
  • sudo find / -type f -printf '.': prints a dot for each file in /;
  • sudo find / -type d -printf '.': prints a dot for each folder in /;
  • wc -c: counts the number of characters.

Here's an example of how not taking care of newlines in file / folder names may break other methods such as e.g. find / -type f | wc -l and how using find / -type f -printf '.' | wc -c actually makes it right:

% ls
% touch "file
\`dquote> with newline"
% find . -type f | wc -l
2
% find . -type f -printf '.' | wc -c
1

If STDOUT is not a terminal, find will print each file / folder name literally; this means that a file / folder name containing a newline will be printed across two different lines, and that wc -l will count two lines for a single file / folder, ultimately printing a result off by one.

0
7

1 method would be

sudo find / -type f | wc -l
sudo find / -type d | wc -l

(sudo to prevent accessing errors)

f for files, d for directories.

The /proc/ filesystem will error out but I do not consider those files ;)

7
  • do I need sudo? all files are like this: find: ‘/run/lock/schroot’: Permission denied
    – TellMeWhy
    Dec 19, 2015 at 17:36
  • yes. I got "permission denied' running it as a user.
    – Rinzwind
    Dec 19, 2015 at 17:36
  • I got 483664... not as many as I expected - does that make sense though?
    – TellMeWhy
    Dec 19, 2015 at 17:40
  • I got less :D Hmmm. funny You will run into 1 problem: the /proc/ system is changing always.
    – Rinzwind
    Dec 19, 2015 at 17:47
  • 1
    You should use -xdev, or at least exclude /proc, /sys, and probably /dev as well.
    – muru
    Dec 20, 2015 at 7:13
7

If you really want the total number of objects in your filesystems, use df -i to count inodes. You won't get the breakdown between directories and plain files, but on the plus side it runs near-instantly. The total number of used inodes is something filesystems already track.


If you want to use one of the find-based suggestions, don't just run it on /. Use find -xdev on a list of mount points generated by something like findmnt --list -v -U -t xfs,ext3,ext4,btrfs,vfat,ntfs -o TARGET or something. That doesn't exclude bind mounts, though, so files under bind mounts will get counted twice. findmnt is pretty cool.

Also, surely there's a straightforward way to list all your "disk" mounts without having to list explicit filesystem types, but I'm not sure exactly what.

As suggested by another answer, use find -printf . | wc -c to avoid any possible problems counting funny characters in filenames. Use -not -type d to count non-directory files. (You don't want to exclude your symlinks, do you?)

2
  • 2
    This is a better approach than all the other answers suggesting to traverse the entire file system. A different command line tool that could also be used is stat. Using stat -f / or df -i / will both obtain data using the statfs system call, so the output from those two commands should always be consistent.
    – kasperd
    Dec 20, 2015 at 17:18
  • 1
    Note: df -i doesn't seem to work on all filesystems. e.g. on btrfs it shows 0 inodes.
    – arielf
    Dec 26, 2015 at 10:30
4
sudo find / -type f | wc -l

will tell you the number of regular files on your system, and

sudo find / -type d | wc -l

the number of folders.

2

Using zsh:

As root, for regular files:

files=( /**/*(.D) )

this will take all the regular files including the ones starting with a . into the array files, now we can simply count the number of elements of the array:

echo $#files

this will handle all the edge cases e.g. unusual file names.

Similarly for directories:

dirs=( /**/*(/D) )
echo $#dirs
2
  • @kos: but then need to manually differentiate between regular files and others..
    – heemayl
    Dec 20, 2015 at 5:34
  • @kos No ..then you need /*/**/*(.D)..also this is not right as it will ignore any file in /
    – heemayl
    Dec 20, 2015 at 5:45
2

Another approach that leverages locatedb:

locate / | wc -l

Advantages:

  • Doesn't require sudo
  • Much faster than the find based approaches (already pre-indexed)
  • Already applies -xdev: i.e. skips special files: /dev, /proc etc.

Downsides:

  • Not 100% accurate: includes directories, skips files under /tmp, may double-or-more-count files with newlines in their name, for example
  • Slower than the df -i approach
  • Reflects "last ~24 hour snapshot reality" rather than exact current state

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .