0

I have du redirect into a file named stdout.txt, and the file's contents read as follows(for example):

4.0K    ./Makefile.am
20K     ./dfasearch.c
8.0K    ./dosbuf.c
4.0K    ./egrep.sh
84K     ./grep.c
4.0K    ./grep.h
8.0K    ./kwsearch.c
36K     ./kwset.c
4.0K    ./kwset.h
12K     ./pcresearch.c
4.0K    ./search.h
4.0K    ./searchutils.c
4.0K    ./system.h

From that file, I would like to be able to display only lines above or below a given size value. "sort -h" gets me part of the way there, I think, but I'm not sure how to go about culling the lines I wouldn't need. For instance, let's say I only wanted to print lines that represented a 12K or less file, my output should look something like:

4.0K    ./Makefile.am
8.0K    ./dosbuf.c
4.0K    ./egrep.sh
4.0K    ./grep.h
8.0K    ./kwsearch.c
4.0K    ./kwset.h
12K     ./pcresearch.c
4.0K    ./search.h
4.0K    ./searchutils.c
4.0K    ./system.h

Is there a common tool that naturally sorts by human-readable sizes and displays only the lines below (in this case) a given size? Ideally, I'd like to have some bash code that could be used to generate this output above or below a user-provided number that could be denoted by K, MB, or GB, etc.

Dominique
  • 16,450
  • 15
  • 56
  • 112
Volumetricsteve
  • 213
  • 1
  • 7
  • You can use the `find` tool to find files over a certain size and pass this file list to `ls` (or `du` if you insist) instead. See e.g. http://stackoverflow.com/a/64699/421705 – Holger Just Nov 25 '16 at 18:21
  • That's pretty good, thanks. – Volumetricsteve Nov 25 '16 at 18:30
  • On second glance, it's still not quite what I'm looking for, I misunderstood your response and thought you meant 'find' could look inside my file. – Volumetricsteve Nov 25 '16 at 18:39
  • What I mean was, use something like `find . -type f -size -12k -exec du -h {} \;` This command gathers all the files which are smaller than 12K and only passes those to `du`. – Holger Just Nov 25 '16 at 18:49
  • I see that, and thanks again, but I am reliant on the intermediate file because I have more going on in my script than I could fit into this question. – Volumetricsteve Nov 25 '16 at 18:56
  • 1
    Save yourself a ton of trouble and drop `-h` from `du`. It's much easier to format for humans at the end when you don't have to process it anymore. – that other guy Nov 25 '16 at 19:00
  • Hmmm...it looks like I might have to. Thanks. – Volumetricsteve Nov 25 '16 at 19:05

2 Answers2

0

You can use sort with option -h (at least with sort from the GNU coreutils version 8.25). With this option it sorts human-readable numbers like you have them here with suffixes k, M, etc. or without a suffix.

After sorting it is just a question of finding the place where to cut.

Alfe
  • 56,346
  • 20
  • 107
  • 159
0

First thing I'd advise you is to drop the -h, just use du -k. Once this is done, you might refer to awk just to see the entries, being larger than your desired value, as explained in this post, hereby an example:

du -k . | sort -n | awk '{if($1>1000000) print $1 $2}'
Dominique
  • 16,450
  • 15
  • 56
  • 112