How to find and sort files by size in Linux Ubuntu. Here is the command to order files by size in Linux and sort files in directory by size Linux.
How To Find And Sort Files By Size In Linux
You can use one of the following commands to find and sort files by size in Linux Ubuntu:
$ ls -lhS
$ ls -l
$ ls -lS
$ ls -lhS
Note that the command option ‘-h’ or ‘–human-readable’ is used to get the result in human readable format. It is used to append a size letter to each size, such as ‘M’ for mebibytes. Powers of 1024 are used, not 1000; ‘M’ stands for 1,048,576 bytes. This option is equivalent to ‘–block-size=human-readable’. You can also use the ‘–si’ option if you prefer powers of 1000.
List files by their size in reverse order
If you want to display the result in ascending order and show bigger files at the bottom and smaller files at the top, you can use the command option -r with the ls -lhSr command.
$ ls -lhSr
If you want to get the list of 10 biggest files in a directory, you can use the following command:
$ ls -lhS | head -11
Using the ls and sort Commands
$ ls -lhR | grep '^-' | sort -k 5 -h
The following command options are used for ls and sort command:
- -l: uses a long listing format. The file size is shown in the fifth column
- -h: reports size in a human-readable format like Kilobytes (K), Megabytes (M), Gigabytes (G), and so on
- -R: recurs through the sub-directories
- -k: performs the sort using a key. In this example, the fifth column is used as the sort key
Using the du and sort Commands
$ du -ah --max-depth=1 | sort -h
The following command options are used for the du command:
- -a: reports size of all files and not just of directories
- -h: reports size in human-readable formats like Kilobytes (K), Megabytes (M), Gigabytes (G), and so on
- –max-depth: reports total size for the first-level directories
If you want to list the result in descending order, use the -r option with the sort command.
Using the find and sort Commands
$ find . -type f -ls | sort -n -k7
The following command options are used for the find and sort command:
- -type f -ls: uses a long listing format of ls command for each file. The size of a file is shown in the seventh column.
- -n -k7: sorts files in ascending order according to their size. In this example, we use the seventh column as a sort key
Hope you learned how to find and sort files by size in Linux Ubuntu.