Shell : How to organize and prioritize disk usage data with ‘du’ and ‘sort’

Shell Scripting @ Freshers.in

You can sort the results of the du (disk usage) command in the shell using the sort command. By default, du will display the disk usage of directories in ascending order, but you can use sort to customize the sorting behavior.

Here’s how you can sort the du output:

To sort by directory size in ascending order (smallest to largest), you can use the following command:

du | sort -n

This command pipes the output of du to sort, using the -n flag to perform a numeric sort. This will give you the smallest directories at the top.

To sort by directory size in descending order (largest to smallest), you can use the following command:

du | sort -nr

This command uses the -nr flags with sort to reverse the order, so the largest directories will be at the top.

If you want to sort the output based on the directory names in alphabetical order, you can use:

du | sort

This will sort the directories alphabetically based on their names.

Other urls to refer
Author: user