If you are attempting to learn how to show the linux directory size of a directory on your linux OS, you have found the right article. Below we will demonstrate a few examples of how to show the directory size instead of just the file size. The examples in this article we performed using WSL2 on Windows using Ubuntu and also Redhat, but are true of any linux OS.
The most basic example we can show is how to output the directory size of a single directory of interest.
Show linux directory size of a single directory
To show the size of a single directory in linux, run the following command:
sudo du -sh /var
Note that you must use sudo if showing the size of a directory owned by root. For any directory owned by your non-root user, you can omit the sudo.
The command above will give an output similar to the following:
- -s – Display the single directory size sum, which includes the size of subdirectories but does not show them individually.
- -h – Show the human readable size in GB, MB, and K instead of bytes.
Find largest file in linux directory
Now that you have shown the size of a given directory, you may want to find the largest file within that directory. While you could always change to the directory and run an ls -la to find the largest file, you may want to search recursively, which this example will demonstrate with the same du utility.
Perhaps your root partition has ran out of space and you are searching for the largest file in the root / directory. You can find the largest files in a given directory by running the following command.
sudo du -a / | sort -n -r | head -n 20
The output will be a list of files in sorted order from largest to smallest.
- -a – Show the counts for all files and not just the directories
- sort – sort the output of the previous command
- -n – sort by the numeric value. Default is ascending, meaning smallest to largest
- -r – reverse the order of the sort, meaning descending or largest to smallest.
- head – Print the lines from the previous command
- -n 20 – The number of lines to print. In other words, do not print all of the output but just the first 20 results from the previous sort.
No space left on device for yum command
If you are trying to free up space on your linux server you may have thought to clear out old dependencies using yum. You may try to run yum clean all to no avail with the process hanging. This means there isn’t enough space for yum to run. Counter intuitive as it may be, this is a clear sign that the / directory is full. One of the first places to check is the /var/log directory and delete any excessively large files that may have been created unexpectedly. Then of course fix the errors that are causing the large log files in the first place.
The same goes for dnf clean all.
Running yum clean all and/or dnf clean all will also clear up space.
This article has demonstrated how to show a linux directory size, how to show the largest files in a directory recursively, and went over a scenario that may have caused the unexpected running out of space in the / directory. Let us know in the comments if you would like to see more examples or check out our other articles.
Leave a Reply