0

This question already has an answer here:

I tried to sort files using sort -h

algorithms@algorithms:~$ ls -lh /boot | sort -h
drwxr-xr-x 5 root root 4.0K Dec 28 16:19 grub
-rw------- 1 root root 4.2M Dec  5 13:10 System.map-4.18.0-13-generic
-rw------- 1 root root 4.2M Nov 14 21:30 System.map-4.18.0-12-generic
-rw------- 1 root root 8.2M Dec  5 13:11 vmlinuz-4.18.0-13-generic
-rw------- 1 root root 8.2M Nov 14 21:50 vmlinuz-4.18.0-12-generic
-rw-r--r-- 1 root root 1.5M Nov 14 21:30 abi-4.18.0-12-generic
-rw-r--r-- 1 root root 179K Jan 28  2016 memtest86+.bin
-rw-r--r-- 1 root root   17 Nov 14 21:30 retpoline-4.18.0-12-generic
-rw-r--r-- 1 root root 181K Jan 28  2016 memtest86+.elf
-rw-r--r-- 1 root root 181K Jan 28  2016 memtest86+_multiboot.bin
-rw-r--r-- 1 root root 212K Dec  5 13:10 config-4.18.0-13-generic
-rw-r--r-- 1 root root 212K Nov 14 21:30 config-4.18.0-12-generic
-rw-r--r-- 1 root root  38M Dec 18 15:47 initrd.img-4.18.0-12-generic
-rw-r--r-- 1 root root  38M Dec 28 16:25 initrd.img-4.18.0-13-generic

However, it does not sort reasonably.

Reference to the manual:

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

What's the problem with my usage?

marked as duplicate by roaima, Kusalananda, Jeff Schaller, RalfFriedl, elbarna Jan 3 at 22:15

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.

  • @roaima OP wants to know how to use of h option in sort. – Prvt_Yadv Jan 3 at 8:29
  • 2
    Unclear: It is unknown what you mean by "reasonably". – Kusalananda Jan 3 at 8:31
  • 2
    @P_Yadav it looks like an XY problem. There's little point solving Y if X is wrong. – roaima Jan 3 at 9:47
6

According to your question, you have to provide the column number also i.e.

ls -lh /boot | sort -hk5

Now it will sort the output based upon the column 5 i.e. column which represents size.

Here the use of h is, if you use the command ls -lh /boot | sort -nk5, then it will sort according to the numbers without considering K,M,G, and if you use h option, then it will consider K,M,G.

If you want to sort then better option is ls -lhS /bootas suggested by roaima.

  • 3
    But ls -lhS /boot will avoid the need to pipe into sort at all – roaima Jan 3 at 8:25

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