How to see the disk space usage for a file or folder using Linux terminal

Users who have switched recently to Linux or still learning it might be curious to know how to see the disk space usage for a file or folder using the terminal application in Linux. While it is easy to view the size with any graphical file manager app, it isn't so obvious via the command line for someone who hasn't used Linux before. Here is how you see the size.

Linux comes with a number of commands which can show you detailed information about files and folders. The first command you can use to see the size of files is ls. "ls" serves the same purpose as the good old DOS command "dir", which lists a directory's contents. To make "ls" show you file sizes in bytes, type it as follows:

ls -l

The marked value in the output is the file size in bytes:

You can make it show the size in a more user friendly format. Add the -h switch and execute ls as follows:

ls -lh

This will produce the following result:

The size will be in kilobytes, megabytes and gigabytes, which gives you a better idea of how much space a file occupies.

The ls command does not show you the size of a directory. For that, you should use the command du. The syntax is as follows:

du -s /etc

This will show you the total size of the /etc directory in bytes. Again, you can use the -h switch to get the result in a human readable format:

Note that you can use du to view the size of files too. This can be useful to see the size of a single file, as follows:

du -h /path/filename

By default, du shows the space which the file consumes on the disk drive, whereas ls shows the actual file size, i.e. the file size regardless of the disk sector size. This can cause differences in ls and du results. You can make du show the actual file size by using the following switch:

du --apparent-size -h /path/filename

The result will be as follows:

That's it.

Support us

Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:

If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!

Leave a Reply

Your email address will not be published.