My assignment is: Find files (in a specific directory that is given) , show only the ones above 100 megabytes, sort them by size, print them in ls -lh format.

This is what I execute:

find /home/it21366 -size +10M | sort -h | ls -lh

This is what I get

-rwxr-xr-x+ 1 it21366 unixusers  12K Απρ  28  2014 a.out
-rwxr-xr-x+ 1 it21366 unixusers  471 Νοέ  29 10:51 askisi.sh
-rw-r--r--+ 1 it21366 unixusers 3,0K Απρ  28  2014 code.c
-rw-r--r--+ 1 it21366 unixusers 3,0K Απρ  28  2014 code.c~
-rw-r--r--+ 1 it21366 unixusers 6,2K Απρ  28  2014 CODE.txt
-rw-------+ 1 it21366 unixusers 2,0M Απρ  28  2014 core
-rwxr-xr-x+ 1 it21366 unixusers   66 Δεκ   6 14:39 it21366_ex_1_1
-rwxr-xr-x+ 1 it21366 unixusers  190 Δεκ   6 14:35 it21366_ex_1_4.sh
-rwxr-xr-x+ 1 it21366 unixusers  190 Δεκ   6 14:27 it21366_ex_1_4.sh~
-rwxr-xr-x+ 1 it21366 unixusers  546 Δεκ   5 17:46 it21366_ex_1_5
-rw-r--r--+ 1 it21366 unixusers 509K Οκτ  23  2014 java01.pdf
-rw-r--r--+ 1 it21366 unixusers 506K Οκτ  23  2014 java02.pdf
-rw-r--r--+ 1 it21366 unixusers 249K Οκτ  11 10:03 lab01_2018-19.pdf
-rw-r--r--+ 1 it21366 unixusers 1,2K Οκτ  24 16:27 MyClock.java
-rw-r--r--+ 1 it21366 unixusers 1,3K Οκτ  30  2013 starthere.desktop
-rw-r--r--+ 1 it21366 unixusers    3 Οκτ  24 12:45 test
-rw-r--r--+ 1 it21366 unixusers   12 Οκτ  24 12:45 TK.txt
-rw-r--r--+ 1 it21366 unixusers  14K Μάι  30  2018 Untitled 1.odt

I can't imagine why LS seems to ignore the other methods, and simply shows the contents of the files unsorted and unfiltered.

  • ls does not use anything you're trying to pipe to it through standard input. Since its command line arguments are just the options -lh, it's listing the full contents of the current directory, in long format with human-optimized file sizes, in default alphanumeric sort order. You would need different options for ls to sort in different ways, e.g. by size, or to display the directory list unsorted. – telcoM Dec 6 '18 at 12:56
  • 1
    Related: Pipe to ls Not Working as Expected – steeldriver Dec 6 '18 at 13:00
  • Related: Sort the output of find -exec ls – Jeff Schaller Dec 6 '18 at 13:39

use xargs:

find /home/it21366 -size +10M | xargs ls -lhsSr 
  • On the assumption that there are no files named with spaces or tabs in them – Jeff Schaller Dec 6 '18 at 13:38

Another option, if the number of files is limited enough to fit in one (long) call to ls, would be to use find to find the large files and ls -S to do the sorting:

find /home/it21366 -type f -size +10M -exec ls -lS {} + 2>/dev/null

The -exec ... {} + tells find to replace the "found" files into the call to ls, fitting as many as it can. If there are too many files, you'll get multiple calls to ls and thus un-sorted results (they'll be sorted in groups). I also added -type f to restrict the matches to regular files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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