Can you sort an ls listing by name?
If you add the -X option, ls will sort files by name within each extension category. For example, it will list files without extensions first (in alphanumeric order) followed by files with extensions like . 1, . bz2, .
As we already mentioned, by default, the ls command is listing the files in alphabetical order. The --sort option allows you to sort the output by extension, size, time and version: --sort=extension (or -X ) - sort alphabetically by extension. --sort=size (or -S ) - sort by file size.
The sort command sorts the contents of a file, in numeric or alphabetic order, and prints the results to standard output (usually the terminal screen). The original file is unaffected. The output of the sort command will then be stored in a file named newfilename in the current directory.
To list all files and sort them by size, use the -S option. By default, it displays output in descending order (biggest to smallest in size). You can output the file sizes in human-readable format by adding the -h option as shown. And to sort in reverse order, add the -r flag as follows.
My ls sorts by name by default. What are you seeing?
man ls
states:
List information about the FILEs (the current directory by default). Sort entries alpha‐betically if none of -cftuvSUX nor --sort is specified.
:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With