Check Disk Usage of Files and Directories
Basic Methods
In Linux, you can use the following commands to check the disk space used by a file or directory:
-
ducommand (disk usage):-
Check the space used by a directory:
du -sh /path/to/directoryOption explanations:
-s: Show only the total-h: Display in human-readable format (e.g., KB, MB, GB)
-
Check the space used by a file:
du -sh /path/to/file
-
-
lscommand (list directory contents):- Check the size of a single file:
Option explanations:
ls -lh /path/to/file-l: Use long format to list file information-h: Display file size in human-readable format
- Check the size of a single file:
-
statcommand (display file or file system status):- View detailed information about a file or directory, including size:
stat /path/to/file_or_directory
- View detailed information about a file or directory, including size:
Additional du Usage
-
Check the total size of a directory:
du -sh /path/to/directoryHere,
-smeans summary and-hmeans display in human-readable format (e.g., KB, MB). -
Check the size of a directory and its subdirectories:
du -h /path/to/directoryThis will list the size of each subdirectory under the specified directory.
-
Check the total size of a directory and its subdirectories, sorted by size:
du -ah /path/to/directory | sort -hHere,
-ameans show all files and directories, andsort -hmeans sort in human-readable format. -
Show only the top N largest subdirectories and files:
du -ah /path/to/directory | sort -rh | head -n NHere,
-rmeans reverse order andhead -n Nmeans take the first N lines.