4

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!

5
  • 7
    "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.
    – Rinzwind
    Sep 24 at 9:21
  • 5
    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. Sep 24 at 20:12
  • 2
    Just curious, why do you need this info? Sep 25 at 5:58
  • 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?
    – terdon
    Sep 25 at 13:16
  • 3
    @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. Sep 25 at 13:28
16

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.

5
  • The speed of df -i I suspect it pulls it from somewhere, do you know where?
    – bac0n
    Sep 24 at 11:21
  • 2
  • 2
    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 … Sep 25 at 7:31
  • 2
    @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.
    – KeyWeeUsr
    Sep 25 at 10:38
  • 1
    True! Thanks, I added this information.
    – pLumo
    Sep 27 at 6:12
14

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.

15
  • 1
    locate is a lot quicker :D if you don't prune directories
    – Rinzwind
    Sep 24 at 9:05
  • 2
    counting inodes is probably quicker too.
    – Rinzwind
    Sep 24 at 9:06
  • Does locate have access to all files?
    – choroba
    Sep 24 at 9:07
  • 4
    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.
    – Ruslan
    Sep 24 at 19:49
  • 5
    This will exclude directories, which are also files. Sep 24 at 20:13

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