1

I have this command to find files larger than 2 KB and sort by size:

find . -size +2k -name *.log -printf "%p \t%k kb\n" | ls -lS

but the ls -lS gives the files that are less than 2 KB also.

How can I display the names and sizes of the files that are larger than 2 KB, sorted by size?

3

Try

 find . -size +2k  -printf "%p \t%k kb\n" | sort -k2n
  • what does k2n do? – Curious Aug 22 '18 at 6:47
  • 1
    Assuming none of the file names contain blanks or newline characters. – Stéphane Chazelas Aug 22 '18 at 8:13
  • Note that strictly speaking, that sorts by disk usage, not by size, though that's likely what the OP wants anyway. – Stéphane Chazelas Aug 22 '18 at 8:14
  • -k2n sets the sort key to the second field, to be interpreted numerically. – RudiC Aug 22 '18 at 8:41
1

The ls command will list the current directory, not read something from stdin.

Use

find . -size +2k -name *.log -printf "%s %p \t%k kb\n" | sort -n | sed -e '/^[0-9]* //'
1

With zsh:

zmodload zsh/stat
printf '%s\n' **/*.log(DLK+2oLe'[
  stat -A blk +block -- $REPLY && REPLY+=" $((blk / 2)) KiB"]')

That reports the disk usage of the log files whose size is greater that 2048, ordered by size.

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.