Ubuntu find out which folders are taking most of hard disk space

If your hard disk is getting full on Ubuntu and you want to find out which folders are taking most of the hard disk space, you can use the following commands:

1. du -Sh | sort -rh | head -10

This command will list the top 10 directories consuming most of the space on your hard disk on Ubuntu. The du command estimates file space usage, the sort command sorts the output, and the head command prints the first 10 lines of the output. The -S option in the du command tells it to show only the total size of each directory, and the -h option tells it to display the sizes in human-readable format.

2. du -hsx — * | sort -rh | head -10

This command is similar to the previous one, but it uses the -x option in the du command to exclude the size of subdirectories. This can be useful if you want to see the size of each directory individually, without the size of its subdirectories.

3. find . -type d -exec du -sh {} + | sort -nr | uniq | head -n 10

This command is more complex, but it is more powerful. It uses the find command to find all directories in the current directory and its subdirectories, and then uses the exec command to execute the du -sh command on each directory. The sort command sorts the output, the uniq command removes duplicate lines, and the head command prints the first 10 lines of the output.

Example output

The following is an example of the output of the first command:

40G     azure-agent
106M    vsts-agent-linux-x64-3.214.0.tar.gz
84M     google-chrome-stable_current_amd64.deb
83M     vsts-agent-linux-x64-2.202.1.tar.gz
4.0K    tmp
4.0K    packages-microsoft-prod.deb

This output shows that the azure-agent directory is the largest directory on the hard disk, using 40GB of space. The next largest directories are vsts-agent-linux-x64-3.214.0.tar.gz and google-chrome-stable_current_amd64.deb, both using over 100MB of space.

How to use this information

Once you know which folders are taking most of the space on your hard disk, you can decide what to do with them. You may want to delete some of the files in the folders, or you may want to move the folders to a different disk. If you are not sure what to do, you can consult a system administrator or other computer expert.

Delete command example

To delete a directory on Ubuntu, you can use the rm command. The rm command removes files and directories from the filesystem. To delete a directory, you must use the -r option to specify that you want to delete the directory recursively, including all of its subdirectories and contents.

Example:

rm -r /path/to/directory

This command will delete the directory /path/to/directory and all of its contents.

Warning:

The rm command is very powerful and can be dangerous if used incorrectly. It is important to make sure that you are deleting the correct directory before you run the command. Once you delete a file or directory, it is permanently gone and cannot be recovered.

Example of deleting one of the directories from the previous example:

rm -r /path/to/azure-agent

This command will delete the directory /path/to/azure-agent and all of its contents.

Please note: It is important to back up any important data before deleting any files or directories.