Sort files and directories by size in Bash

If you want to sort files and directories by size in Bash, you can use the "du" command to get a list of all files and directories, along with their sizes. Then, you can pipe the output to the "sort" command and specify the "-n" option to sort by numerical value.

Here's an example command:

du -h | sort -n

This will display a list of all files and directories, sorted by size in ascending order. The "-h" option for "du" specifies human-readable output, while the "-n" option for "sort" specifies numerical sorting.

If you want to sort the list in descending order (i.e. largest files and directories first), you can add the "-r" option to the "sort" command:

du -h | sort -nr

This will display a list of all files and directories, sorted by size in descending order.

In summary, to sort files and directories by size in Bash, you can use the "du" and "sort" commands in combination, with options for human-readable output and numerical sorting.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

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

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information