Disk Usage

“What happened to all my disk space!”

It can be amazing how quickly all your disk’s capacity gets filled up with random files and forgotten downloads. Here are a few commands to point you towards some of the culprits clogging up your file system.

This command will list the largest 10 objects in the current directory.

sudo du -a . | sort -n -r | head -n 10

Here’s the breakdown of the command: du is a utility on mac and linux that can list disk usage of directories and files. The -a option specifies du to list all (both files and directories). The period stands for the current directory (so you could change this to any directory you want to examine that folder’s disk usage). The sudo command executes the following command (du) as the root user so that you will be able to read all the files and folders on the system.

The pipe character ( | ) captures the output of the command to its left and makes it the input to the command on the right. So the output of du gets piped into sort, which sorts lists of things. The -n and -r flags direct it to sort the list like they are numbers (since we are giving the file sizes, they are numbers) and to sort in reverse order. We then pipe the output of the sort into the head command. head will display only the first few lines of input. We specify how many lines to read by passing the -n flag followed by the number of lines to read: 10, because we want to see only the 10 largest files.