How to Include Hidden Files When Using du

How to Include Hidden Files When Using du

If you’ve ever wondered about the mysteries of your computer’s disk space, you might have come across the du program. It’s short for “disk usage,” and it’s like your trusty sidekick for figuring out how much space files and folders hog on your hard drive. But here’s the scoop – du doesn’t spill the beans on hidden files by default. In this post, we’re going to show you how to unveil those hidden files with du. Ready? Let’s dive in!

First Off, What’s du All About?

Before we dive into the hidden file adventure, let’s cover the basics of using du.1 Here’s how you generally use it:

du [options] [directories or files]

When you fire up this command, du will start listing the disk usage of each directory and file you point it to. By default, it’s going to give you the sizes in kilobytes.

For instance, to see how much space your trusty /home directory is gobbling up, you’d run:

du /home

Doing that will display the disk usage for /home and all the stuff nested inside it.

Hidden Files: What’s the Deal?

Before we get into making du reveal hidden files, let’s chat about what these sneaky files actually are and why they’re hidden.

Hidden files in Unix-based systems are the ones that kick off their names with a dot (.), like .config or .bashrc. These files are usually used for system configurations or other stuff that’s best left untouched by casual users. So, they’re hidden from view by default.2

The reason du doesn’t include these files in its usual output is that they’re typically not what regular users need to worry about. However, if you’re a tech whiz or a sysadmin, these hidden files can hold valuable info, and you might want to see their disk usage.

How to Uncover Hidden Files with du

Now, let’s reveal the mystery of including hidden files when you’re using du.

The easiest way to do this is by using the -a or –all option. Basically, you’re telling du, “Hey, show me everything, including those sneaky hidden files.” Here’s how you do it:

du -a /home

With this command, du will lay out the disk usage for /home, including all those hidden files.

Another way to see hidden files with du is to use the -d or –max-depth option. This nifty trick lets you limit how deep into the directory structure du looks. By default, du shows you the disk usage of all subdirectories. However, if you specify a depth level with the -d option, du will only spill the beans up to that level. Here’s an example:

du -d 1 /home

With this command, du will only show the disk usage for /home and its immediate subdirectories, giving you a peek at those hidden files as well.

Want to Focus Only on Hidden Files?

Okay, so you’ve got the hidden files showing, but what if you want to zero in on only those sneaky little guys? Du can help with that too. We just need to use the –exclude option to tell du to skip files that match a certain pattern.

In our case, we want to skip everything except those starting with a dot (.), which are the hidden files. Here’s how to do it:

du --exclude="*[!.]*" /home

With this command, du will spill the beans on all hidden files and directories within /home. The –exclude=”[!.]” part tells du to exclude anything that doesn’t start with a dot (.).

Quick tip: The [!.] pattern matches any file or directory name that starts with a dot (.), which is our way of identifying hidden files. This way, you get a clean list of just the hidden files’ disk usage. Be cautious, though, as this will exclude all non-hidden files from the results.

References

  1. Ramuglia, G. (2023, December 11). “Du” Linux Command guide: Disk usage made easy. Linux Dedicated Server Blog. https://ioflood.com/blog/du-linux-command/ ↩︎
  2. Kumwenda, M. (2023, March 7). How to view hidden files and folders on Linux. MUO. https://www.makeuseof.com/view-hidden-files-and-folders-linux/ ↩︎
Published