Score:4

How do I count all files on my system?

ng flag

I need to see how many files I have on my whole system. Specifically, I need to count all files from / (so on the whole system), and I need to get a number at the end. So like there are 10000 files on the whole system.

How can I accomplish this? I hope you can help!

cn flag
"Specifically, I need to count all files from / (so on the whole system)," I see an issue here... this will also count tmpfs and those are -virtual- and not actual files.
st flag
How do you define "all files"? All inodes? All links? Only regular files or also special files? Which kinds of special files? Directory special files? Character special files? Block special files? Socket special files? Symlink special files? Pipe special files? And at what point? For example, when you plug in a mouse, a character special file will be automatically created, so depending on whether you have your mouse plugged in or not, the number will be different.
Eric Duminil avatar
us flag
Just curious, why do you need this info?
terdon avatar
cn flag
Please [edit] your question and clarify what you mean by "files" because the answer will depend very much on what you actually want to count. Should symlinks be counted? Should hardlinks be counted as one file or two? Should directories be counted? How about device files? Or named pipes? What about files that only live in RAM?
in flag
@Rinzwind stuff on tmpfs may or may not be part of what the OP wants to count here. The bigger issues are `/sys`, `/proc`, and `/dev`, which the OP almost certainly does not want to count.
Score:16
in flag

Use df -i to see the number of Inodes used -> IUsed field.

Or limit the output like:

df --output=source,target,iused

Note, that numbers from find and df -i won't necessarily match:

  • df -i also counts directories
  • files that link to the same inode (hard links) will count only once
  • Some special inodes that are not linked to any directory for internal use are not counted.
  • df will not traverse into mounted directories, e.g. /boot or if you have a separate /home partition. With find, you can get that behavior using -xdev flag.

Check this Answer on U&L SE.

I'd expect the result to be slightly less than the df -i count as a few file systems (including ext4) have a few special inodes that are not linked to any directory for internal use.

bac0n avatar
cn flag
The speed of `df -i` I suspect it pulls it from somewhere, do you know where?
pLumo avatar
in flag
@bac0n [`df` uses the `statvfs()` system call and asks the filesystem for the current space statistics](https://unix.stackexchange.com/questions/445764/how-does-df-know-how-much-space-is-used-without-needing-to-go-through-all-the-fi).
vn flag
`find` and `df -i` won't match possibly for another reason, because `find` will traverse mounted folders. To avoid that, use `-xdev`, as `sudo find / -xdev …`
KeyWeeUsr avatar
pl flag
@PaddyLandau Good point on the "mounted," part. `/` is a partition on a device, but there can be more paritions or even other folders which can be mounted one through each other! Perhaps OP should reformulate according to how Unix filesystem is mounted otherwise it might be strictly `/` partition file count (so with/out mounted `/home`, `/boot` and so on. Maybe a mount table in the question would help too.
pLumo avatar
in flag
True! Thanks, I added this information.
Score:14
tm flag

You can use find / to list all the files. When find's output is redirected, it outputs file names containing newlines as spanning multiple lines, so you can't just count the lines. You can output something else for each file and count the characters, though:

sudo find / -type f -printf 1 | wc -c

Without sudo, you probably can't access all the paths, so you can't count all the files.

cn flag
`locate` is a lot quicker :D if you don't prune directories
cn flag
counting inodes is probably quicker too.
tm flag
Does `locate` have access to all files?
cn flag
yes but you can use a .conf to exclude dirs and it prunes some dirs by default (like tmpfs and /media)
bac0n avatar
cn flag
think `locate -c '*'` is an acceptable balance as @Rinzwind suggests.
cn flag
Another issue: this will count links too I would assume? So does locate :D Counting inodes is a better method
qwr avatar
kr flag
qwr
does `find` count block devices and special system files?
Ruslan avatar
bv flag
_"`find` shouldn't output files with newlines in their names on multiple lines"_ — any reference to this? How should it print in this case?
tm flag
@Ruslan: Try it to see. `touch $'a\nb' ; find`
Ruslan avatar
bv flag
`touch $'a\nb'; find | od -tx1` outputs `2e 0a 2e 2f 61 0a 62 0a`, which does contain `0a` between `61` and `62`. Since your code in the answer does pass the output to a pipe, `wc` will get two lines for this file. You can also see the piped output by piping to `cat`: `find | cat`.
st flag
This will exclude directories, which are also files.
tm flag
@Ruslan: That's why I also included the second command.
tm flag
OK, reformulated.
vn flag
I think that your answer should mention mounted folders from other partitions. To include those, use your answer as given (but with `sudo`). To exclude mounted partitions, use `-xdev`, as `sudo find / -xdev …`. It can get more complicated, e.g. if you want to include `/home` but not other mounted partitions.
Peter Cordes avatar
fr flag
If you omit `-type f`, this will count all files, including directories, not just *regular* files. (Use `-not -name .` to exclude the current directory if you don't specify an explicit `/` arg)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.