du sort by size linux with code examples

The du command in Linux is a utility that allows users to estimate the space used by a directory and its subdirectories. The du command provides information about the space used by a directory and its contents in kilobytes (KB), megabytes (MB), or gigabytes (GB). The du command can also be used to sort the output by size to get a clearer picture of which directories are taking up the most space. In this article, we will look at how to sort du output by size in Linux with code examples.

To sort the output of du by size, we can use the sort command in combination with the du command. The sort command is used to sort the output of a command in a specified order. The following command sorts the output of du by size in descending order:

du -sh * | sort -hr

Here's what each option in the command does:

  • du: The du command is used to estimate the space used by a directory and its subdirectories.

  • -s: The -s option is used to summarize the total size of each directory and its subdirectories.

  • -h: The -h option is used to display the output in a human-readable format, such as KB, MB, or GB.

  • *: The * wildcard is used to specify that all directories and subdirectories should be included in the output.

  • |: The | symbol is used to pipe the output of the du command to the sort command.

  • sort: The sort command is used to sort the output of a command in a specified order.

  • -r: The -r option is used to sort the output in descending order.

  • -h: The -h option is used to sort the output based on the size of the directories, rather than their names.

The output of the above command will show the total size of each directory and its subdirectories, sorted by size in descending order. The following is an example of the output of the command:

1.1G	/path/to/directory
744M	/path/to/directory2
512M	/path/to/directory3
256M	/path/to/directory4

In this example, the du command is used to estimate the space used by the directories and their subdirectories. The sort command is used to sort the output in descending order based on the size of the directories. The -h option is used to display the output in a human-readable format, such as KB, MB, or GB.

In conclusion, the du command in Linux provides a convenient way to estimate the space used by a directory and its subdirectories. By combining the du command with the sort command, we can sort the output by size to get a clearer picture of which directories are taking up the most space. With the code examples in this article, you should be able to sort du output by size in Linux with ease.
In addition to sorting the output of the du command by size, there are several other options that can be used to customize the output. Some of the most commonly used options are described below:

  • -a: The -a option is used to show the size of each file, as well as the size of each directory. This can be useful if you want to see the size of individual files, as well as the size of the directories that contain them.

  • --exclude: The --exclude option is used to exclude specific directories from the output of the du command. For example, if you want to exclude the .git directory from the output, you can use the following command:

du -sh --exclude=.git * | sort -hr
  • --max-depth: The --max-depth option is used to limit the depth of the directory tree that is displayed in the output. For example, if you only want to see the size of the top-level directories, you can use the following command:
du -sh --max-depth=1 * | sort -hr

Another useful tool that can be used in combination with the du command is the df command. The df command is used to display information about the disk space usage on a system. The df command provides information about the total size of the file system, the amount of free space, and the percentage of the file system that is used. The following command displays information about the disk space usage on the root file system:

df -h

The -h option is used to display the output in a human-readable format, such as KB, MB, or GB. The following is an example of the output of the df command:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G  9.7G  9.8G  50% /

In this example, the df command is used to display information about the disk space usage on the root file system. The -h option is used to display the output in a human-readable format. The output shows that the root file system has a total size of 20 GB, with 9.7 GB used and 9.8 GB available.

In conclusion, the du and df commands are useful tools for managing disk space on a Linux system. By using the options described in this article, you can customize the output of the du and df commands to get the information that you need about disk space usage on your system.

Popular questions

  1. What is the du command used for in Linux?

The du command is used to estimate the disk space usage of files and directories in a Linux system. The command provides information about the size of each file and directory, including the size of the subdirectories.

  1. How can you sort the output of the du command by size in Linux?

To sort the output of the du command by size, you can pipe the output of the du command to the sort command, and use the -hr option to sort the output in reverse order by size. For example:

du -sh * | sort -hr
  1. How can you exclude specific directories from the output of the du command?

To exclude specific directories from the output of the du command, you can use the --exclude option. For example, to exclude the .git directory from the output, you can use the following command:

du -sh --exclude=.git * | sort -hr
  1. What is the df command used for in Linux?

The df command is used to display information about the disk space usage on a Linux system. The command provides information about the total size of the file system, the amount of free space, and the percentage of the file system that is used.

  1. What is the -h option used for in the df command?

The -h option is used in the df command to display the output in a human-readable format, such as KB, MB, or GB. The -h option makes it easier to interpret the output of the df command, as it provides information about disk space usage in a format that is easy to understand.

Tag

Filesystem

Posts created 2498

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top