Author: JackTian

Source: Public account “Jie Ge’s IT Journey”

ID: Jake_Internet

Please contact authorization for reprinting (wechat ID: Hc220088)

5 tools to replace the du command!

Hi, I’m Jack.

In my last article, I introduced you to 10 Linux Alternatives you didn’t know existed! It mainly includes bat, DUF, TLDR, HTOP, GLances, EXA, FD, AG, AXEL and PYDF. Later, I continued to explore and found some good substitutes and summarized them.

Well, I’m sure this article is based on native commands. As you know, the DU command is familiar in Linux, and it can calculate and summarize the usage of file and directory space in Linux.

If files occupy 80% of the disk space and the disk space cannot be expanded temporarily, you can locate the files in the directory that occupies a large portion of the disk space to clear the space. You only need to run the following command to view the space occupied by the folder.

# du -sh
Copy the code

Go to the folder that occupies large space. Run the following command to sort the files in the current directory from large to small.

# du -sh * |sort -rh
Copy the code

Check whether files that occupy large space can be deleted based on the actual situation to release disk space and observe the disk usage.

When files occupy a large amount of disk space, DU executes slowly and repeatedly to find large files, which is inefficient. In this case, NCDU can solve this problem.

A, ncdu

Ncdu is a simple and fast disk usage analyzer developed using C language and Ncurses interface. It can be used to check the disk space occupied by directories or files on local or remote systems.

Ubuntu 21.10 Install NCDU:

# sudo apt-get install ncdu
Copy the code

Ncdu parameter options:

  • -h: Help Information
  • -q: Silent mode, refresh interval is 2 seconds
  • -v: Printed version
  • -x: Same file system
  • -e: Enables extended information
  • -r: read only
  • -o FILE: Exports the scan directory as a file
  • -f FILE: Imports scan directories from files
  • - 0-1-2: UI used for scanning (0= None,2=full ncurses)
  • --si: Uses the base 10 (SI) prefix instead of base 2
  • --exclude PATTERN: Excludes files that match PATTERN
  • -X, --exclude-from FILE: Excludes files that match any pattern in the file
  • -L, --follow-symlinks: by symbolic links (excluding directories)
  • --exclude-caches: Exclude directories that contain cachedir.tag
  • --exclude-kernfs: Exclude Linux pseudo-file systems (procfs, sysfs, cgroup…)
  • --confirm-quit: Confirm exit
  • --color SCHEME: Sets the color scheme

If you do not use any parameter option, run the ncdu command in the directory that you want to analyze. After ncDU scanning is complete, an interactive terminal interface is displayed.

You can clearly see the size of each directory. Use the arrow key or Enter key to Enter or exit the selected directory.

If a directory or file occupies a large space, you can locate the file based on the preceding keyboard usage. If the directory or file can be deleted, you do not need to exit NCDU. Select the directory or file to be deleted and press D to delete it.

Second, the dust

Dust (DU + rust = DUST) is a free, open source, more intuitive DU tool written in Rust, as well as a tool that provides file types and metadata.

Ubuntu 21.10 Install dust:

Du-dust_0.7.1_amd64. deb is the latest version in the dust GitHub repository. You can run the following command to download and install the. Deb file from the dust publishing page.

Wget https://github.com/bootandy/dust/releases/du-dust_0.7.1_amd64.deb DPKG -i du - dust_0. 7.1 _amd64. DebCopy the code

For other systems, refer to other OPERATING system installation methods of DUST on GitHub.

Dust usage:

dust [FLAGS] [OPTIONS] [--] [inputs]...
Copy the code

Dust parameter options:

  • -f: Directory “size” is the number of subfiles/directories, not the disk size
  • -s: Use file lengths instead of blocks
  • -p: The path of the subdirectory is not shortened
  • -h: Help Information
  • -i: Do not display hidden files
  • -x: Only files and directories that reside on the same file system as the provided directory are counted
  • -b: Percentage bars or percentages are not displayed
  • -c: does not print colors (usually the largest directories are in color)
  • -r: Print tree inverted (the largest is the highest)
  • -t: Displays only these file types
  • -V: Prints the version information
  • -d: Display depth
  • -e: Contains only files that match this regular expression. For PNG file types: -e “.png$”
  • -X: Excludes any files or directories with this name
  • -v: Excludes files that match this regular expression. File type: -v “.png$”
  • -n: The number of output lines to display. This is the height, (but h helps) [default: 23]
  • -w: Specifies the automatic detection of the output width to cover the terminal width

Dust using:

To view the file sizes in the current directory and all subdirectories, run the following command:

# dust
Copy the code

The -p parameter can be displayed as the full directory starting from the current directory

# dust -p
Copy the code

If you only want to view the size of multiple directories, just list them together, separated by Spaces:

# dust /bin /etc
Copy the code

Display the length of the file:

# dust -s
Copy the code

Only 10 directories are displayed:

# dust -n 10
Copy the code

Third, duu

Duu stands for Directory Usage Utility. Duu is a Python Utility that allows you to view the size of a specified Directory. Duu can be used on Windows, Linux, and MacOS operating systems. It displays directory disk usage in kilobytes.

Ubuntu 21.10 Install duu:

Make sure Python 3 is installed on your system before installing the duu tool. If not, install it, and if not, ignore it, but Python 3 is now available in most Linux distributions as the default repository.

Gz is the latest source code package in the Duu GitHub repository. You can download the. Tar. gz file from the duU release page.

Wget # https://github.com/jftuga/duu/archive/refs/tags/v2.22.tar.gz # tar - ZXVF v2.22. Tar. GzCopy the code

After decompressing, go to du-2.22 and check whether the du. py file exists.

# ls | grep duu duu # 2.22 # CD duu - 2.22 / ls duu. Py LICENSE README. The mdCopy the code

Duu usage:

duu.py [-h] [-b] [-e] [-q] [-s STATUS] [-n] [-N] [-f] [-S] [-H] [-T THREADS] [-x EXCLUDE] [-X REGEXPR] [-o OUTPUT] [dname]
Copy the code

Duu parameter options:

  • -h: Displays this help information and exits
  • -b: Does not print summaries or statistics, used exclusively for sorting purposes
  • -e: Summary file extension
  • -q: Does not display a single directory
  • -s: Sends processing STATUS to STDERR, number of each STATUS directory
  • -n: Skip to “. Leading directory
  • -N: no recursion
  • -f: Displays the number of files in each directory
  • -S: Displays mean, median, mode, and standard deviation file statistics
  • -H: Displays numbers in a more readable format
  • -T: Number of concurrent threads, consider SAN
  • -x: Excludes colon-delimited lists of case-insensitive strings
  • -X: Excludes colon-delimited lists of case-insensitive regular expressions
  • -o: Output to a CSV file

Duu use:

To view the size of the current directory, run the following command:

# python3 duu.py
Copy the code

As you can see in the figure above, duu displays the number of files and directories in the current directory and the total size of these files in Bytes, KB, and MB, as well as the size of each file.

To check the size of a directory, add the absolute path to the directory:

# python3 duu.py /etc/init.d/
Copy the code

Fourth, the diskus

Diskus is a small, fast, open source tool written based on Rust that can replace the du-sh command. Diskus calculates the size of all files in the current directory. Running the diskus command has the same effect as du-sh or du-sh –bytes.

According to the developer of the Diskus GitHub repository, on his 8-core laptop, testing medium size folders (15GB, 100K directories, 400K files) with the Hyperfine command-line benchmark tool doesn’t actually make sense for smaller folders. Because everything completes in a reasonable amount of time without interrupting your workflow, diskus is about ten times faster than DU cold disk cache and more than three times faster than du hot disk cache.

The following is a comparison of cold disk cache and hot disk cache based on diskus, du-sh, sn p-d0-j8, and dust-d0 commands.

Cold disk cache

Command Mean [s] Min [s] Max [s] Relative
diskus 1.746 + / – 0.017 1.728 1.770 1.00
du -sh 17.776 + / – 0.549 17.139 18.413 10.18
sn p -d0 -j8 18.094 + / – 0.566 17.482 18.579 10.36
dust -d0 21.357 + / – 0.328 20.974 21.759 12.23

Hot disk cache

Command Mean [s] Min [s] Max [s] Relative
diskus 500.3 + / – 17.3 472.9 530.6 1.00
du -sh 1098.3 + / – 10.0 1087.8 1122.4 2.20
sn p -d0 -j8 1122.2 + / – 18.2 1107.3 1170.1 2.24
dust -d0 3532.1 + / – 26.4 3490.0 3563.1 7.06

Ubuntu 21.10 Installing diskus:

Deb diskus_0.7.0_amd64.deb is the latest version in the Diskus GitHub repository and can be downloaded from the Diskus release page. You can run the following command to download and install the.

# # wget https://github.com/sharkdp/diskus/releases/download/v0.7.0/diskus_0.7.0_amd64.deb DPKG -i Diskus_0. 7.0 _amd64. DebCopy the code

For other oss, refer to other OS installation methods of Diskus on GitHub.

Diskus usage:

diskus [OPTIONS] [path]...
Copy the code

Diskus parameter options:

  • -j: Set number of threads (default: 3 x num cores), output format of file size (decimal: MB, binary: MiB) [default: decimal] [possible values: decimal, binary]
  • -v: Does not hide file system errors
  • -b: Calculate the apparent size rather than disk usage
  • -h: Help Information
  • -v: Version information

Diskus use:

You can run the du -sh command to view the size of a directory. The -s parameter indicates the total size.

# du -sh /etc
16M	/etc
Copy the code

If you use diskus, you can directly display the total size of the current directory.

# diskus
Copy the code

If you want to see the size of a directory, you can add the absolute path after diskus.

# diskus /etc
16.66 MB (16,662,528 bytes)
Copy the code

Five, the tin – summer

Tin-summer (SN) is an open source tool written in Rust and an alternative to the du command. It can be used to find files that take up disk space for better output, clearer commands and default values, and because of multi-threading, it can even run faster than the DU command when calculating the size of large directories. The difference between tin-summer and du is that the former reads the file size, while the latter reads disk usage.

In addition, the developers of Tin-Summer made a sharp contrast between du and SN.

Reasons for using DU:

  • Read disk usage, not just file size
  • Optionally dereference symbolic links
  • It can be slightly faster on a small directory
  • Stable and well supported

Reasons for using SN:

  • Faster in large directories
  • Exclusion using regular expressions makes it much faster than du when used with the –exclude flag.
  • The default is readable output
  • Color output
  • By getting better help
  • Provide sorting output
  • Finding build artifacts
  • Read file size, not disk usage
  • Extensible in Rust

Benchmarks were also performed to compare sn and DU execution times for different directory sizes.

Directory Size Tool Command Time
600MB sn sn p 60.74 ms
600MB sn sn d 99.92 ms
600MB du du -hacd2 88.28 ms
4GB sn sn p 185.2 ms
4GB sn sn d 271.9 ms
4GB du du -hacd2 195.5 ms
700MB sn sn p 91.05 ms
700MB sn sn d 176.3 ms
700MB du du -hacd2 153.8 ms
7MB sn sn p 19.48 ms
7MB sn sn d 12.72 ms
7MB du du -hacd2 10.13 ms

Install the tin – summer:

# curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git vmchale/tin-summer
Copy the code

Tin-summer can also be installed using cargo package manager, provided Rust is installed on your system. If Rust is already installed, run the following command:

# cargo install tin-summer
Copy the code

If tin-Summer has not been successfully installed in either of the above installation methods, you can also manually install it by downloading the latest version binaries from the distribution area of the Tin-Summer GitHub repository.

Use of tin-summer:

You do not need to add any parameters to view the file size of the current directory. Tin-summer is also displayed in an easy-to-read format by default. You can run the following command:

# sn f
Copy the code

To view the file size of a specific directory, run the following command:

# sn f <path-to-the-directory>
Copy the code

To search the directory containing the build artifacts in the current directory, run the following command:

# sn ar
Copy the code

To search for an artifact that occupies more than 200MB of disk space or a directory containing the artifact, run the following command:

# sn ar -t200M
Copy the code

To get the sorted list of the 10 largest directories in $DIR, run the following command:

# sn sort $DIR -n12
Copy the code

Sn actually operates a large directory faster than sn operates a small directory slower.

reference

Dust:github.com/bootandy/du…

duu:github.com/jftuga/duu

Diskus:github.com/sharkdp/dis…

Tin-summer:github.com/vmchale/tin…

The last

Although most of us still use the default basic command tool in our daily work, it will be very useful in many scenarios if you replace it with the five DU alternatives described above. Of course, there are probably more Linux command-line alternatives, and you are welcome to add them in the comments section below.

And that’s all for today.

If you think this article is useful to you, please like it, leave a comment or forward it, so that more friends can see it, because this will be my strongest motivation to continue to output more high-quality articles!