Identifying Available Disk Space

Every file you save takes up additional disk space, and while using the ls -lh command will tell you how much space the files in a directory consume, it doesn’t give you a holistic view of the filesystem. The df and du commands will give you more insight into where your space is going. These commands represent disk space and usage in slightly different ways.

The df command will show you the filesystems, size, used space, available space, and where it’s mounted on the filesystem. By default, it shows everything in bytes. If you use the -h flag, you’ll see all of the sizes represented clearly, similarly to how the -h flag worked for the ls command. Execute the df -h command to see the results on your system:

 $ ​​df​​ ​​-h
 Filesystem Size Used Avail Use% Mounted on
 udev 967M 0 967M 0% /dev
 tmpfs 200M 1.5M 198M 1% /run
»/dev/sda1 32G 6.8G 24G 23% /
 tmpfs 997M 0 997M 0% /dev/shm
 tmpfs 5.0M 4.0K 5.0M 1% /run/lock
 tmpfs 997M 0 997M 0% /sys/fs/cgroup
 tmpfs 200M 28K 200M 1% /run/user/121
 tmpfs 200M 20K 200M 1% /run/user/1000

The system in this example has several devices and disks that store files. Remember that each disk or storage device on the system is mounted somewhere on the main filesystem. The output of this command shows each device or filesystem, its total available space, the amount used, and where it’s mounted on the main filesystem.

The tempfs entries represent virtual-memory filesystems, or RAM disks.

If you had a DVD inserted, or a portable USB drive, you’d see it in this list as well. And to access it, you’d use the path listed in the Mounted on column.

But the only entry in this list you’re really interested in is the device that’s mounted to the root of the filesystem. In this case, it’s the one called /dev/sda1.

The dev directory, short for devices, contains a node for each physical device connected to your computer. On Linux-based systems, disks typically start with sd for SATA hard drives and hd for IDE hard drives. The rest of the name tells you more information about the disk and partition. a is the first disk in the system, b is the second. 1 is the first partition on the disk, 2 is the second, and so on.

For example, dev/sda1 refers to the first partition of the first SATA drive of our computer. dev/sdb2 refers to the second partition of the second SATA drive.

BSD and macOS-based systems use different naming conventions for their disks and partitions, but you’ll still find their device files in the /dev directory.

Look at the entry for /dev/sda1 in the output of df -h again:

 Filesystem Size Used Avail Use% Mounted on
 ...
»/dev/sda1 32G 6.8G 24G 23% /
 ...

The output shows the disk is 32 GB in size, and it’s 23% full. What’s taking up the space though?

The du command lets you look at disk usage, the df command looks at the filesystem metadata. Using du, you can find out where your space is going.

Use du -h on your home directory to show how much disk space each directory in your home directory is taking up. Be warned though, if you have a lot of files, this will take quite a while to scan them all:

 $ ​​du​​ ​​-h
 76K ./.local/share/gvfs-metadata
 296K ./.local/share/gnome-software
 ...
 4.0K ./Videos
 4.0K ./Pictures
 4.0K ./sharptools
 4.0K ./Desktop/website
 12K ./Desktop
 ...
 4.0K ./Music
 4.0K ./Downloads
 4.0K ./Documents
 4.0K ./Public
 4.4M .

You’ll get quite the list, but next to each entry, you’ll see how much space each entry uses.

This output only shows directories that take up space. If you wanted to see the files as well, add the -a switch to see all files.

The very last item in the list is a total of the space used for that directory and its children. If you just want to see this summary, without all of the files, use du -sh:

 $ ​​du​​ ​​-sh
 4.4M .

A lot of times, what you’re really looking for is a breakdown of the directories and their sizes at a high level. You can control the depth with the -d switch. Execute this command to show the space used by each directory:

 $ ​​du​​ ​​-h​​ ​​-d​​ ​​1
 1.8M ./.local
 4.0K ./Videos
 4.0K ./Pictures
 4.0K ./sharptools
 12K ./Desktop
 2.4M ./.cache
 16K ./.gnupg
 4.0K ./Templates
 168K ./.config
 4.0K ./.ssh
 4.0K ./Music
 4.0K ./Downloads
 4.0K ./Documents
 4.0K ./Public
 4.4M .

You still get the summary at the bottom, but it doesn’t drill down and show you the contents of each child directory.

The du command accepts multiple directories, which is great for getting a single list of directory sizes for your most common places. Use this to see the size of your Music, Pictures, and Videos directories:

 $ ​​du​​ ​​-h​​ ​​-d​​ ​​1​​ ​​Music​​ ​​Pictures​​ ​​Videos
 4.0K Music
 4.0K Pictures
 4.0K Videos

And if you add the -c switch, du will give you a grand total of the space used:

 $ ​​du​​ ​​-c​​ ​​-h​​ ​​-d​​ ​​1​​ ​​Music​​ ​​Pictures​​ ​​Videos
 4.0K Music
 4.0K Pictures
 4.0K Videos
 12K total

You can also exclude certain types of files using --exclude. For example, you could filter out HTML files so you can see how much space the other files take up with du -h --exclude="*.html".

The du and df commands produce different output. Remember that df is looking at free space as reported by the metadata for the filesystem itself, while du is looking at the directories and files on the disk and counting up the bytes used. If you delete a file that’s in use by a running program and it hasn’t completely been removed yet, df might still report that the space hasn’t been freed up yet, but du doesn’t see the file, so it reports that the file isn’t there anymore. In most cases, you won’t notice these differences, but now you know why results may differ if you run into this later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.197.250