check the size of directory or file on linux
We often need to check the size of a file or subdirectories on linux. This post shows how to use du
command to check the size of a file or directory on linux system. We also show how to sort folders or files by size.
The two most useful options of du commands are: -s and -h,
The meaning of the options are:
1 2 3 4 |
-s, --summarize display only a total for each argument -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) |
Get the human readable size of a file
We can simply use du -h
command to get human readable size of a file:
du -h <file_name>
Get the size of a folder
To get the size of a folder only, we can use the -s
option, which display the total size of a folder.
du -sh <folder_name>
Find the top 10 largest file or folder on linux:
The following command list the top 10 largest file or folder under your home directory:
du -a ~ | sort -n -r | head -n 10
List the size of all subfolders under a directory
By using the max-depth
option, we can get the size of all subfolders under a directory:du -h --max-depth=1 <folder_name>
List only the files or folders of which the size is larger than 10,000
We can use the awk command to filter out files that are larger than the threshold
du | awk '{$1 > 10000}'