简体   繁体   中英

Using output from du, how can I display lines that correspond to files above or below a given size in bash?

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.

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.

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}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM