8

ncdu - Analyze Disk usage in Linux with ncdu | ComputingForGeeks

 2 years ago
source link: https://computingforgeeks.com/ncdu-analyze-disk-usage-in-linux-with-ncdu/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Analyze Disk usage in Linux with ncdu

Are you having a hard time trying to audit disk utilization on your Linux system?. I know most Linux sysadmins are accustomed to using du command to check files consuming more disk space or to find actual file size. I always run the command below to find files in specified directory and sort them by size – Largest to smallest:

$ find /dir -type f -exec du -Sh {} + | sort -rh

You can further limit output results by piping it to head

$ find /dir -type f -exec du -Sh {} + | sort -rh | head -n 10

Which will only print top 10 files

Or by using du command only:

$ du -sh /dir/* | sort -hr

In this guide,  I’ll introduce you to Ncdu – A disk usage analyzer with an ncurses interface. Ncdu was designed to ease finding space hogs on a remote server where you don’t have an entire graphical setup available, but it is a useful tool even on regular desktop systems.

Ncdu aims to be fast, simple and easy to use, and should be able to run in any minimal POSIX-like environment with ncurses installed. By default, it will show files/directories disk usage sorted from large to small:

Installing Ncdu on Linux

Ncdu package is available on most Linux distributions from upstream repositories. Let’s see how to install ncdu package on CentOS, Debian family distributions, and on Arch Linux.

Install Ncdu on Arch Linux

Use the commands:

$ sudo pacman -S ncdu

resolving dependencies...
looking for conflicting packages...

Packages (1) ncdu-1.13-1

Total Installed Size: 0.11 MiB

:: Proceed with installation? [Y/n] y
(1/1) checking keys in keyring [################] 100%
(1/1) checking package integrity [################] 100%
(1/1) loading package files [################] 100%
(1/1) checking for file conflicts [################] 100%
(1/1) checking available disk space [################] 100%
:: Processing package changes...
(1/1) installing ncdu [################] 100%
:: Running post-transaction hooks...
(1/1) Arming ConditionNeedsUpdate...

Install Ncdu on Ubuntu / Debian

On Ubuntu and all other Debian family distributions, install ncdu using the apt package manager.

$ sudo apt-get install ncdu

Install Ncdu on CentOS / Fedora

On CentOS and Fedora, the package is available from EPEL repository. Add it first then install ncdu using yum.

$ sudo yum install epel-release
$ sudo yum provides ncdu 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centosmirror.netcup.net
* epel: mirror.imt-systems.com
* extras: mirror.netcologne.de
* updates: mirror.softaculous.com
ncdu-1.13-1.el7.x86_64 : Text-based disk usage viewer
Repo : epel
$ sudo yum install ncdu
$ sudo dnf install ncdu  # For Fedora

Using Ncdu to Audit disk usage

Once the package is installed on your OS, simply run:

$ ncdu /dir/

Ncdu also provide a number of options to manipulate files and folders – Navigation, sorting, and even deleting:

  • up, k – used to move the cursor up
  • down, j – used to move the cursor down
  • right, enter, l > – Open selected directory
  • left, <,h  – This opens parent directory
  • n – order by name (press again for descending order)
  • s – Order by file size (press again for descending order)
  • d – Delete selected file or directory
  • g – Show percentage and/or graph
  • – Toggle dirs before files when sorting.
  • – Toggle display of child item counts.
  • – Spawn shell in the current directory.
  • i  – Show information about the selected item
  • r – Refresh/recalculate the current directory.
  • q – Quit ncdu

Example 1: Navigate using >

Example 2: Show percentage and/or graph using g

Example 2: Delete file using d

Example 3: Scan a full filesystem

# 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-

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 user@system ncdu -o- / | ./ncdu -f-

Ncdu Scan Options

These options affect the scanning progress and have no effect when importing directory information from a file.

-x
Do not cross filesystem boundaries, i.e. only count files and directories on the same filesystem as the directory being scanned.

–exclude PATTERN
Exclude files that match PATTERN. The files will still be displayed by default, but are not counted towards the disk usage statistics. This argument can be added multiple times to add more patterns.

-X FILE, –exclude-from FILE
Exclude files that match any pattern in FILE. Patterns should be separated by a newline.

–exclude-caches
Exclude directories containing CACHEDIR.TAG. The directories will still be displayed, but not their content, and they are not counted towards the disk usage statistics. See http://www.brynosaurus.com/cachedir/

Conclusion

This tool will save you lots of time when working on disk usage audit for both remote servers and local Desktop systems. Enjoy using Ncdu!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK