How to use the command 'gdu' (with examples)
The command ‘gdu’ is a disk usage analyzer with a console interface. It provides an interactive way to view the disk usage of a directory or multiple directories. It also offers options to ignore certain directories, show disk usage for all mounted disks, and perform non-interactive operations.
Use case 1: Interactively show the disk usage of the current directory
Code:
gdu
Motivation: This use case is helpful when you want to quickly see the disk usage of the current directory and its subdirectories.
Explanation: By running the command ‘gdu’ without any arguments, it analyzes the disk usage of the current directory and displays the results interactively. It provides a hierarchical view of the directory structure and shows the disk usage for each directory.
Example output:
Disk usage: 1.2 GB
.
├── dir1 (800 MB)
│ ├── file1.txt (500 MB)
│ └── file2.txt (300 MB)
├── dir2 (400 MB)
│ ├── file3.txt (200 MB)
│ └── file4.txt (200 MB)
└── file5.txt (1 KB)
Use case 2: Interactively show the disk usage of a given directory
Code:
gdu path/to/directory
Motivation: This use case allows you to analyze the disk usage of a specific directory and its subdirectories.
Explanation: By providing the path to a directory as an argument to the ‘gdu’ command, it analyzes the disk usage of that directory and displays the results interactively. It shows the directory structure and disk usage for each subdirectory within the given path.
Example output:
Disk usage: 800 MB
path/to/directory
├── subdir1 (500 MB)
│ ├── file1.txt (300 MB)
│ └── file2.txt (200 MB)
└── subdir2 (300 MB)
├── file3.txt (150 MB)
└── file4.txt (150 MB)
Use case 3: Interactively show the disk usage of all mounted disks
Code:
gdu --show-disks
Motivation: This use case is useful when you want to view the disk usage of all mounted disks on your system.
Explanation: By adding the ‘–show-disks’ option to the ‘gdu’ command, it analyzes and displays the disk usage of all mounted disks interactively. It provides a list of mounted disks along with their usage information.
Example output:
Disk usage:
- /dev/sda1 (100 GB used, 400 GB available)
- /dev/sdb1 (50 GB used, 250 GB available)
- /dev/sdc1 (200 GB used, 300 GB available)
Use case 4: Interactively show the disk usage of the current directory but ignore some sub-directories
Code:
gdu --ignore-dirs path/to/directory1,path/to/directory2,...
Motivation: This use case allows you to exclude certain subdirectories from the disk usage analysis of the current directory.
Explanation: By using the ‘–ignore-dirs’ option followed by a comma-separated list of directory paths, the ‘gdu’ command analyzes the disk usage of the current directory but excludes the specified subdirectories from the analysis. It displays the disk usage for the remaining directories in an interactive manner.
Example output:
Disk usage: 800 MB
.
├── dir1 (500 MB)
│ └── file1.txt (500 MB)
└── dir2 (300 MB)
└── file2.txt (300 MB)
Use case 5: Ignore paths by regular expression
Code:
gdu --ignore-dirs-pattern '.*[abc]+'
Motivation: This use case allows you to ignore directories based on a regular expression pattern.
Explanation: By using the ‘–ignore-dirs-pattern’ option followed by a regular expression pattern, the ‘gdu’ command excludes all directories matching the specified pattern from the disk usage analysis. It performs a regex-based filtering and displays the disk usage for the remaining directories interactively.
Example output:
Disk usage: 500 MB
.
└── dir1 (500 MB)
└── file1.txt (500 MB)
Use case 6: Ignore hidden directories
Code:
gdu --no-hidden
Motivation: This use case allows you to exclude hidden directories from the disk usage analysis.
Explanation: By adding the ‘–no-hidden’ option to the ‘gdu’ command, it ignores hidden directories (directories starting with a dot) during disk usage analysis. This ensures that hidden directories are not included in the results displayed interactively.
Example output:
Disk usage: 500 MB
.
└── dir1 (500 MB)
└── file1.txt (500 MB)
Use case 7: Only print the result, do not enter interactive mode
Code:
gdu --non-interactive path/to/directory
Motivation: This use case is helpful when you just need the disk usage information for a specific directory and prefer a non-interactive output.
Explanation: By using the ‘–non-interactive’ option followed by a directory path, the ‘gdu’ command performs a disk usage analysis of the specified directory but only prints the result without entering interactive mode. This allows you to obtain the disk usage information in a non-interactive format.
Example output:
Disk usage: 800 MB
Use case 8: Do not show the progress in non-interactive mode (useful in scripts)
Code:
gdu --no-progress path/to/directory
Motivation: This use case is useful when you want to suppress the progress indication while running the ‘gdu’ command in non-interactive mode, especially in scripts.
Explanation: By adding the ‘–no-progress’ option to the ‘gdu’ command, it performs the disk usage analysis without displaying the progress bar or any progress indications on the screen. This is particularly beneficial in non-interactive scenarios, such as running the command within scripts.
Example output: No progress bar or progress indications are shown during disk usage analysis in non-interactive mode.
Conclusion:
The command ‘gdu’ provides a convenient way to analyze disk usage with a console interface. It offers various options to customize the analysis, such as ignoring directories, filtering based on regular expressions, and performing non-interactive operations. By understanding the different use cases and their corresponding command examples, you can efficiently utilize ‘gdu’ to manage disk space and identify space-consuming directories or files.