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.
Leave a Reply
Related posts