0

Q1. I have a command and I'm confused why it's sorting the filesizes by Kilobytes then Megabytes?

$ find ~ -maxdepth 1 -size +1024c -type f -exec du -h {} \; | sort -nr

80K /home/sl6/S3401636/Lab 4- Reporting Overview in IBM RQM(2).pdf
20K /home/sl6/S3401636/.bash_history
8.5M    /home/sl6/S3401636/Software-Testing-Chapter-4-Test Design Techniques.pdf
7.7M    /home/sl6/S3401636/Software-Testing-Chapter-2 Software Lifecycle.pdf
6.2M    /home/sl6/S3401636/Week5b.ppt
5.1M    /home/sl6/S3401636/Software-Testing-Chapter-5-Test Management.pdf
4.7M    /home/sl6/S3401636/Week 9 - Mobile devices.ppt
4.0K    /home/sl6/S3401636/.viminfo
3.4M    /home/sl6/S3401636/Software-Testing-Chapter-3-Static Techniques.pdf
3.2M    /home/sl6/S3401636/Software-Testing-Chapter-6.pdf
1.9M    /home/sl6/S3401636/Week5a.ppt
1.8M    /home/sl6/S3401636/Lab 8 - Results and Report Analysis(2).pdf
1.6M    /home/sl6/S3401636/Lab 7 - Running a Schedule in IBM's RPT(2).pdf
1.4M    /home/sl6/S3401636/lab01(3).pdf
1.3M    /home/sl6/S3401636/Week 10 Human Capabilities.pdf
1.2M    /home/sl6/S3401636/week05.pdf

If I pipe the "head" command to the end

$ find ~ -maxdepth 1 -size +1024c -type f -exec du -h {} \; | sort -nr | head -14

1.2M    /home/sl6/S3401636/week05.pdf
1.3M    /home/sl6/S3401636/Week 10 Human Capabilities.pdf
1.4M    /home/sl6/S3401636/lab01(3).pdf
1.6M    /home/sl6/S3401636/Lab 7 - Running a Schedule in IBM's RPT(2).pdf
1.8M    /home/sl6/S3401636/Lab 8 - Results and Report Analysis(2).pdf
1.9M    /home/sl6/S3401636/Week5a.ppt
3.2M    /home/sl6/S3401636/Software-Testing-Chapter-6.pdf
3.4M    /home/sl6/S3401636/Software-Testing-Chapter-3-Static Techniques.pdf
4.0K    /home/sl6/S3401636/.viminfo
4.7M    /home/sl6/S3401636/Week 9 - Mobile devices.ppt
5.1M    /home/sl6/S3401636/Software-Testing-Chapter-5-Test Management.pdf
6.2M    /home/sl6/S3401636/Week5b.ppt
7.7M    /home/sl6/S3401636/Software-Testing-Chapter-2 Software Lifecycle.pdf
8.5M    /home/sl6/S3401636/Software-Testing-Chapter-4-Test Design Techniques.pdf

Q2.I'm wondering how I can get just the filenames without the full path. I've tried awk with basename but couldn't get it to work.

Q3.Also how do I go about listing the highest filesizes (highest to lowest AND lowest to highest)

eg 8.5M, 7.7M, 6.2M, 5.1M (highest to lowest) 5.1M, 6.2M, 7.7M, 8.5M

instead of it starting from the lowest filesize 1.2M.

I think I can use "tail" for this situation

Q4.Finally if I remove the -h option for "du" in the above commands I get listed a different set of results. Why?

4   /home/sl6/S3401636/.viminfo
20  /home/sl6/S3401636/.bash_history
80  /home/sl6/S3401636/Lab 4- Reporting Overview in IBM RQM(2).pdf
1132    /home/sl6/S3401636/week05.pdf
1300    /home/sl6/S3401636/Week 10 Human Capabilities.pdf
1424    /home/sl6/S3401636/lab01(3).pdf
1628    /home/sl6/S3401636/Lab 7 - Running a Schedule in IBM's RPT(2).pdf
1812    /home/sl6/S3401636/Lab 8 - Results and Report Analysis(2).pdf
1924    /home/sl6/S3401636/Week5a.ppt
3208    /home/sl6/S3401636/Software-Testing-Chapter-6.pdf

2 Answers 2

1
  1. This is unrelated to KB and MB in your example. You are performing a numerical sort in reverse order. 80 and 20 are both bigger than 8.5, so they get shown first. The letters are ignored for the sorting. See also 4.0K which is right in the middle of your list.

  2. find ~ -maxdepth 1 -size +1024c -type f -exec du -h {} \; | sort -nr | head -14 | awk '{print $2}' | xargs -l basename

  3. The easiest way is to use sort -hr if your version of sort supports it. I have sort (GNU coreutils) 8.22 and it works like a charm.

  4. Is that the complete list? You should see the same amount of files as before, maybe different files due to sorting and cutting off a part. Please provide the exact command line you used.

3
  • The argument for head is different. But now since you explained the fact that it is disregarding the KB and MB notations (80 is bigger than 8.5) I now understand why removing the -h gives different results. Thanks for the helpful reply.
    – bigubosu
    Apr 15, 2014 at 0:48
  • awk '{print $2}' only grabbed the first word of the filename
    – bigubosu
    Apr 15, 2014 at 3:48
  • Found out you can use awk '{print %NF}'
    – bigubosu
    Apr 15, 2014 at 9:21
0

Instead of using awk '{print $2}' | args -l basename, I've used

-printf "%s %f\n"

like so:

find ~ -maxdepth 1 -size +1024c -type f -printf "%s %f\n" | sort -nr | head -14

%f - prints the filename %s - prints the size

removed du, awk and xargs commands

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .