Linux ls Command Sort Files By Size

‮:sptth‬//www.lautturi.com
Linux ls Command Sort Files By Size

To sort files by size using the ls command in Linux, you can use the -S option. This option causes the ls command to sort the files by size, with the largest files listed first.

For example, to list the files in the current directory sorted by size, you can use the following command:

$ ls -S

This will list the files in the current directory, with the largest files listed first.

You can also use the --sort option to specify the sort criteria. For example, to sort the files by size in reverse order (smallest files first), you can use the following command:

$ ls --sort=size -r

This will list the files in the current directory, with the smallest files listed first.

You can also use the -l option to display the files in a long format, which includes the size of each file.

For example, to list the files in the current directory in long format, sorted by size, you can use the following command:

$ ls -lS

This will display the files in the current directory in long format, with the size of each file and the largest files listed first.

Note that the ls command sorts files by size in units of blocks, with each block being 512 bytes. This means that files with sizes that are not a multiple of 512 may not be sorted correctly.

To sort files by size in units of bytes, you can use the find command with the -ls option and pipe the output to the sort command with the -k7,7 option.

For example, to list the files in the current directory sorted by size in units of bytes, you can use the following command:

$ find . -ls | sort -k7,7 -n

This will list the files in the current directory, sorted by size in units of bytes, with the smallest files listed first.

Created Time:2017-10-30 10:17:41  Author:lautturi