linux directory size

By | August 25, 2019

This post may contain affiliate links. Please read my disclosure for more info.

This guide will teach you several commands I use frequently on how to show the linux directory size in Centos or Redhat and what folders are using the most space all listed in MB.

Here is a list of common commands I use that you may find helpful.

Search current Centos or RedHat Folder

You search the current folder sizes down in the command using this command:

du -h --max-depth=1  |grep -v -e [0-9].[0-9]K -e [0-9][0-9]K -e ^0 | sort -n

Find top 10 largest files in a folder in linux

You search a given folder for the largest top 10 files in a given folder using this command:

find /home/simon/Downloads -xdev -type f -exec du -sk {} \; |sort -rn | head -10

Show drive using high percentage in linux

You can you do a quick search to find which drives are using a higher percentage of used space, can be useful if running a script over ssh keys just change the number on what threshold to show using this command:

df -P | awk ‘0+$5 >= 70 {print}’
Example of changing the limit to 10%

Install a package to check linux directory size

Ncdu is an acronym of NCurses Disk Usage. It is a fast way to see what directories are using the disk space. It is simple and fast disk usage analyzer which is used to find which directories or files are taking up more space.

To install on RHEL, CentOS

yum install ncdu

Once installed, just type ncdu to get going:

ncdu Keyboard Shortcuts

up, down j, k – Cycle through the items
right, enter, l – Open selected directory
left, <, h – Go to parent directory
n – Order by filename (press again for descending order)
s – Order by filesize (press again for descending order)
C – Order by number of items (press again for descending order)
a – Toggle between showing disk usage and showing apparent size.
M – Order by latest child mtime, or modified time. (press again for descending order) Requires the -e flag.
d – Delete the selected file or directory. An error message will be shown when the contents of the directory do not match or do not exist anymore on the filesystem.
g – Toggle between showing percentage, graph, both, or none. Percentage is relative to the size of the current directory, graph is relative to the largest item in the current directory.
c – Toggle display of child item counts.
m – Toggle display of latest child mtime, or modified time. Requires the -e flag.
e – Show/hide ‘hidden’ or ‘excluded’ files and directories. Please note that even though you can’t see the hidden files and directories, they are still there and they are still included in the directory sizes.
i – Show highlighted item info about the usage and size
r – Recalculate the current directory.
b – Spawn bash shell in current directory.
q – Quit ncdu.

NCDU Examples

To scan and browse the directory you’re currently in, all you need is a simple:

ncdu
If you want to scan a full filesystem, your root filesystem, for example, then you’ll want to use -x:

ncdu -x /

Since scanning a large directory may take a while, you can scan a directory and export the results for later viewing:

ncdu -1xo- / | gzip >export.gz

…some time later:

zcat export.gz | ncdu -f-

To export from a cron job, make sure to replace -1 with -0 to suppress any unnecessary output.

You can also export a directory and browse it once scanning is done:

ncdu -o- | tee export.file | ./ncdu -f-


The same is possible with gzip compression, but is a bit kludgey:

ncdu -o- | gzip | tee export.gz | gunzip | ./ncdu -f-

To scan a system remotely, but browse through the files locally:

ssh -C [email protected] ncdu -o- / | ./ncdu -f-

The -C option to ssh enables compression, which will be very useful over slow links. Remote scanning and local viewing has two major advantages when compared to running ncdu directly on the remote system: You can browse through the scanned directory on the local system without any network latency, and ncdu does not keep the entire directory structure in memory when exporting, so you won’t consume much memory on the remote system.

Summary

As you can see linux directory size can be determined with multiple different ways, if you have any that you use frequently please add into the comments below.

For other Linux / Unix related tutorials on my website please click here.

Also please have a look at my YouTube Channel and subscribe for video tutorials as they are released, and if you have any advice for others, requests or questions please leave a comment below.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.