1. Basic usage
1. Check the total size of a specific directory
To view a specific directory (for example, nameddata
The total size of the directory) can be used with the following command:
du -sh /path/to/data
-
Parameter explanation:
-
-s
: Summary mode, only displays the total size of the directory, and does not list the size of the subdirectories. -
-h
: Displays sizes in an easy-to-read format, such as KB (kilobyte), MB (megabyte), or GB (gigabyte). -
/path/to/data
: Replace with the actual directory path.
-
For example, ifdata
The path to the directory is/home/user/data
, the command is:
du -sh /home/user/data
The output of this command will be displayeddata
The total size of the directory, such as2.3G
, indicating that the directory has a size of 2.3 GB.
2. View the detailed size of the directory and its subdirectories
If you wish to viewdata
The size of the directory and all its subdirectories and files can be used:
du -h /path/to/data
- This command will be listed
data
The size of each file and subdirectory in the directory is convenient for analyzing which files occupy more space.
For example:
du -h /home/user/data
Possible outputs include:
1.5M /home/user/data/ 2.0M /home/user/data/subdir1 3.5G /home/user/data/subdir2
2. View recursively and sort by size
In some cases, we may need to analyze the size of the directory more carefully, and even want to sort the results by size. You can use the following command to implement it:
du -ah /path/to/data | sort -h
-
Parameter explanation:
-
-a
: Displays the size of all files and subdirectories. -
sort -h
: Sort in a human-readable format, from small to large.
-
This method allows you to quickly find the largest file or subdirectory in the directory. For example:
du -ah /home/user/data | sort -h
This command will be listeddata
All files and subdirectories in the directory are arranged in order from small to large.
3. Sorting from large to small
In some cases, we care more about files or directories that take up the most space. Can be usedsort
of-r
The options are sorted in reverse, and the command is as follows:
du -ah /path/to/data | sort -hr
-
Parameter explanation:
-
-r
: means reverse sorting, that is, from large to small.
-
For example:
du -ah /home/user/data | sort -hr
After execution, you will see that the largest file or directory is at the top, which is very helpful for quickly freeing up disk space.
4. Use sudo to obtain permissions
You may experience permission issues when viewing the size of certain directories. In this case, you can prepend the commandsudo
To obtain super user permissions. For example:
sudo du -sh /path/to/protected_directory
This ensures that you have access to all required directories.
5. Summary
By using the du command, we can easily view the size of a specific directory in the Linux system, as well as the details of files and subdirectories under that directory. Whether it is aggregating the total size or listing details recursively, du provides flexible options to meet different needs.
Mastering these commands not only helps us monitor disk usage, but also quickly identify and clean unnecessary files when needed and free up disk space. During file management and maintenance, it is a good habit to check disk usage regularly and can effectively prevent system performance from degrading.
This is the article about how to view directory size in Linux system. For more related content on Linux, please search for my previous articles or continue browsing the related articles below. I hope you support me in the future!