r/commandline•
K4lim

Monitoring disc usage

Hi,

I want to monitor the disc usage of a folder. I need something similar to this:

https://stackoverflow.com/questions/25481040/monitoring-disk-usage-in-centos-using-du-to-output-json

But I can't get it to work. I created a .sh file with following content:

du -sh -c --time ./* | sed '1 i\
{"folder": [
s/\([^[:space:]]*\)([[:space:]]*\([^[:space:]]*\)[[:space:]]*\([^[:space:]]*\)/  {\
    "size" : "\1",\
    "modified": "\2",\
    "path": "\3"}/
$ a\^J]}' test.txt

But it gives me this error when I execute it:

sed: -e Expression #1, character 8: Unknown command: >>"<<

What would be the correct format?

Posted Jul 14, 2018, 2:33 PM UTC

27
truedays•

It works for me. Are you on OSX? Maybe add --posix.

sed --posix -e ...
1
K4limOP•

sed --posix -e

I'm on Ubuntu and it didn't fix it :/ Same error.

1
AndiPersti•

But it gives me this error when I execute it:

sed: -e Expression #1, character 8: Unknown command: >>"<<

What would be the correct format?

Make sure that there is no space following the backslash at the end of the first line.

1
K4limOP•

There is none.

1
AndiPersti•

Interesting, because that's how I could reproduce the error you get.

What happens if you join the first and second line?:

du -sh -c --time ./* | sed '1 i\{"folder": ['
1
anthropoid•

Why not use awk instead? I personally favor clarity over eye-watering sed scripts:

du -sh -c --time ./* | awk -F$'\t' '
  BEGIN { print "{\"httpdocs\": [" }
        {
          if (NR > 1) {print ","} ;
          print "{\"size\": \"" $1 "\", \"modified\": \"" $2 "\", \"path\": \"" $3 "\"}"
        }
  END   { print "]}" }
' | jq > test.txt

Handles filenames with embedded spaces quite nicely, and has the correct number of commas in between. That last pipe to jq is optional; it just pretty-prints the final JSON.

1
FatFingerHelperBot•

It seems that your comment contains 1 or more links that are hard to tap for mobile users. I will extend those so they're easier for our sausage fingers to click!

Here is link number 1 - Previous text "jq"


^Please ^PM ^/u/eganwall ^with ^issues ^or ^feedback! ^| ^Delete

1