I face disk space full issue in Linux. When checked with df command I found the '/' directory is occupying 100%. So to check which folders consume much space I ran cd / and du -sh. But it takes forever to run the command. But ultimately I want to get the details on which top immediate sub folders of '/' folder are consuming huge disk space. So can any one tell the command for the same.
6 Answers
du -h --max-depth=1 / | sort -h -r
This will show each folder in / including / itself.
Mind that this could take a long time to scan through all the files.
If you require any specific sizes of the subfolders in a folder, specify the exact path of the folder instead of / or just skip / if you're already in that folder.
-hoptions shows sizes in human friendly format--max-depth=1instructs command to go only 1 directory deep inside/sort -h -rsorts results using human friendly sizes and-rinstructs command to show results in reverse order (from largest to smallest directories)
-
1Here is osx version:
du -h -d 1 / | sort -h -rcrollywood– crollywood2022-03-23 15:37:19 +00:00Commented Mar 23, 2022 at 15:37
This command will list the 15 largest in order:
du -xhS | sort -h | tail -n 15
We use the -x flag to skip directories on separate file systems.
The -h on the du gives the output in human-readable format, sort -h can then arrange this in order.
The -S on the du command means the size of subdirectories is excluded.
You can change the number of the tail to see less or more. Super handy command.
Two other open source command line tools, that display top disk space used, are:
ncdu
Available in the repo of most Linux distributions.
Top Disk Usage (tdu)
A single static binary with no dependencies, written in Golang.
You want to know what is using all your disk space ? This command-line tool estimates the disk space occupied by all files in a given path. It displays a sorted list of the biggest items. The estimation method is similar to the 'du -skx' command from GNU Coreutils package.
-
Wow, this is so much easier. Thanks so much for sharing this.jonathanking– jonathanking2020-06-06 15:57:44 +00:00Commented Jun 6, 2020 at 15:57
I allways use
cd /
du -sch ./*
This will show you all folders from the root folder and their used disk space.
After getting the usage from all parent directories, change in the next child folder and repeat the 'du' command from above. Repeat that stepts to find your big files and folders.
You need to go from folder to folder to find out whats going on.
Edit: May be you want the "x" flag as well if you have multiple partions mounted. The 'x' will only count usage on one filesystem and exclude other mounts.
du -schx ./*

