• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Mister PKI

SSL Certificates * SSL Tools * Certificate Decoder

  • Buy SSL Certificates
  • Blog
  • OpenSSL
  • Keytool
  • SSL Tools
  • Donate

Show linux directory size

February 9, 2023 by Mister PKI Leave a Comment

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:

3.0G    /var
  • -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.

Conclusion

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.

linux

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Popular Posts

PKCS12

openssl s_client

Keytool

Keytool list

ECDSA vs RSA

OpenSSL

PKCS7

Certificate Decoder

Training Courses

Top online courses in IT & Software

Cyber Security Training

Udemy - The Complete Internet Security Privacy Course icon

Buy SSL Certificates

The SSL Store

Comodo Store

Sectigo Store

RapidSSL

Recent Posts

  • Show linux directory size
  • Sendmail vs Postfix – Mail Transfer Agent Comparison
  • DNS Powershell
  • How to Install Apache for Linux
  • Firewall Redhat

Footer

  • Twitter
  • YouTube

Pages

  • About Mister PKI
  • Blog
  • Compare and Buy Affordable PKI Certificates
  • Contact Us
  • Full Disclosure
  • Privacy Policy
  • SSL Tools – Certificate Decoder and Certificate Checker

Copyright © 2023