From du to json
Given du's output format ...
14M someFile
6.6M anotherFile
576K yetAnotherFile
0 MyEmptyFile
... you can use sed to convert into json:
Here we assume that you don't have to quote special symbols in the file names, for instance ". You can quote them by inserting sed commands like s/"/\\"/g. If you also have to deal with linebreaks inside filenames check out du -0 and sed -z.
... | sed -E '1i {
s/(.*)\t(.*)/"\2": "\1",/
$s/,$//
$a }'
Filtering du's output
Please note that du -sh *| grep [MG] | sort -r may not do what you expect. With the example files from above you would get
6.6M anotherFile
14M someFile
0 MyEmptyFile
- I assume you want to show only files with sizes > 1MB and < 1TB. However,
grep [MG] also selects files which contain M or G in their name. If the current directory contains a file named just M or G you might even end up with just grep M or grep G as the unquoted [MG] is a glob (like *) that can be expanded by bash.
Use grep '^[0-9.]*[MG]' to safely select files with sizes specified in MB and GB.
- With
sort -r you probably want to sort by file size. But this does not work because sort -r sorts alphabetically, not numerically (i.e. 9 > 11). But even with numerical sort you would end up with the wrong order, as the suffixes M and G are not interpreted (i.e. 2M > 1G).
Use sort -hr to sort by file sizes.
Putting everything together
du -sh * | grep '^[0-9.]*[MG]' | sort -hr | sed -E '1i {
s/(.*)\t(.*)/"\2": "\1",/
$s/,$//
$a }'