du: Estimate the disk usage of a directory and its subdirectories
August 9th, 2024 1:26 PM Mr. Q Categories: Command
Command: du
The du
(disk usage) command estimates and displays the disk space used by files and directories. It provides information on how much space is used by a specified directory and its subdirectories.
Sample Commands and Outputs:
du
: Displays the disk usage of the current directory and its subdirectories. Sample Command and Output:
Copy001 $ du
002 4 ./dir1
003 12 ./dir2/subdir
004 16 ./dir2
005 20 .
Description:
- Displays the disk usage of each directory and subdirectory. The final line shows the total disk usage of the current directory.
du -h
: Displays disk usage in human-readable format (e.g., KB, MB, GB). Sample Command and Output:
Copy001 $ du -h
002 4.0K ./dir1
003 12K ./dir2/subdir
004 16K ./dir2
005 20K .
Description:
-h
: Human-readable format, showing sizes in KB, MB, GB, etc.du -s
: Displays the total disk usage of the specified directory only. Sample Command and Output:
Copy001 $ du -s /path/to/dir
002 1234 /path/to/dir
Description:
-s
: Summarizes the total disk usage of the specified directory, without listing individual subdirectories.du -a
: Includes all files and directories in the output. Sample Command and Output:
Copy001 $ du -a
002 4 ./file1
003 12 ./dir2/subdir/file2
004 16 ./dir2/subdir
005 20 ./dir2
006 24 .
Description:
-a
: Displays the disk usage for all files and directories.du -c
: Displays a grand total at the end of the output. Sample Command and Output:
Copy001 $ du -c
002 4 ./dir1
003 12 ./dir2/subdir
004 16 ./dir2
005 32 total
Description:
-c
: Provides a grand total of the disk usage at the end of the output.du -x
: Excludes directories on different file systems. Sample Command and Output:
Copy001 $ du -x
002 4 ./dir1
003 12 ./dir2/subdir
004 16 ./dir2
005 20 .
Description:
-x
: Limits the report to the current file system only, excluding other file systems.du --max-depth=N
: Limits the output to directories up toN
levels deep. Sample Command and Output:
Copy001 $ du --max-depth=1
002 4 ./dir1
003 16 ./dir2
004 20 .
Description:
--max-depth=N
: Limits the depth of directory traversal toN
levels.
Note: The du
command is useful for monitoring disk space usage and managing storage. Using the appropriate options allows you to customize the output based on your needs.