7

This question already has an answer here:

When I run

ls | sort -S

I get

sort : option requires an argument -- ´S´

Why I can't sort the list of my files with the sort option by size? I know that I can use only the ls command alone.

marked as duplicate by Jeff Schaller, Archemar, Satō Katsura, GAD3R, kenorb Dec 21 '16 at 12:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

13

First of all command ls has option -S

From man ls

-S     sort by file size

So proper command is:

ls -S

sort command is for sorting lines of text file:

From man sort:

-S, --buffer-size=SIZE
              use SIZE for main memory buffer

SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

That's why you are getting error: sort : option requires an argument -- ´S´. Use ls -S for sorting file by size!

  • What is 100? What does it mean? 100 kb? – c023 Oct 31 '14 at 9:53
  • Thanks for the answer, but I still dont understand how it sort. If I take 100K then it will sort... how? The first is going to be the biggest file? – c023 Oct 31 '14 at 10:06
  • @c023 Have you noted that is for memory buffer not for file size? – Pandya Oct 31 '14 at 10:13
2

You can also use du command with some arguments and use sort

I use the following:

$ du -hsc /path/to/file

From man du

-h, --human-readable
      print sizes in human readable format (e.g., 1K 234M 2G)
-s, --summarize
      display only a total for each argument

-c, --total (I USE IT FOR EXTRA INFO)
      produce a grand total

To sort

$ du -hsc /path/to/file | sort -h

From man sort

-h, --human-numeric-sort
      compare human readable numbers (e.g., 2K 1G)

Not the answer you're looking for? Browse other questions tagged or ask your own question.