9

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.

0

2 Answers 2

19

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!

3
  • What is 100? What does it mean? 100 kb? Oct 31, 2014 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? Oct 31, 2014 at 10:06
  • @c023 Have you noted that is for memory buffer not for file size?
    – Pandya
    Oct 31, 2014 at 10:13
6

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 .