How to count the total number of files/folders on a system?
Solution 1
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.
Solution 2
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 ;)
Solution 3
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?)
Solution 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.
Solution 5
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
Related videos on Youtube
Comments
-
TellMeWhy over 1 year
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?
-
TellMeWhy over 8 yearsdo I need sudo? all files are like this:
find: ‘/run/lock/schroot’: Permission denied
-
Rinzwind over 8 yearsyes. I got "permission denied' running it as a user.
-
TellMeWhy over 8 yearsI got 483664... not as many as I expected - does that make sense though?
-
Rinzwind over 8 yearsI got less :D Hmmm. funny You will run into 1 problem: the /proc/ system is changing always.
-
Rinzwind over 8 years@DevRobot oh you do not know who Florian is do you? ;) Check some of his high rep answers on AU.
-
heemayl over 8 years@kos: but then need to manually differentiate between regular files and others..
-
heemayl over 8 years@kos No ..then you need
/*/**/*(.D)
..also this is not right as it will ignore any file in/
-
Peter Cordes over 8 yearsYup, like muru says, you don't want to count all the "virtual" files in with files on "real" (disk-backed) filesystems.
-
kasperd over 8 yearsThis 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
. Usingstat -f /
ordf -i /
will both obtain data using thestatfs
system call, so the output from those two commands should always be consistent.