The output of du -sh folder is some dimension and folder; how can I use grep -o '*G' and similar to just get the dimension, namely get rid of folder name?

  • 1
    Let's split those two separate questions into two separate Questions – Jeff Schaller Mar 5 '18 at 14:55
  • 1
    If your goal is to sum up the disk usage then you probably don't want to pass -h (human-readable) to du: you want the sizes to be listed in bytes for everything, to make it easier to combine the values. – Daniel Pryden Mar 5 '18 at 19:20
  • @DanielPryden "you want the sizes to be listed in bytes" or -k or -m. – RonJohn Mar 5 '18 at 19:21

I can offer a simple cut solution :

du -sh . | cut -f1

The standard delimiter in cut is tab, so no need for any additional options. Simply print field 1.

From your comment, it seems you are concerned with resources/speed so to quote Gilles from another answer:

"Generally speaking, the more specialized a tool is, the faster it is. So in most cases, you can expect cut and grep to be faster than sed, and sed to be faster than awk."

Quoted from here

The ouput of time for both commands shows:

time du -sh /folder | awk '{print $1}'

real    0m0.005s
user    0m0.002s
sys     0m0.004s

time du -sh /folder | cut -f1

real    0m0.003s
user    0m0.000s
sys     0m0.004s

I believe you would need to repeat that multiple times, and take the average to make it a fair test, but either way, not much in it. Technically cut should be "faster".

  • +1 for recommending an oft-overlooked, and quite useful, command. – RonJohn Mar 5 '18 at 19:19

You could use awk to only print the first column:

du -sh folder | awk '{print $1}'

For the file, also using awk:

awk '!($3="")' file
  • Thanks. Compared to grep, is it more resource draining? I plan to put this inside a script that is run every second to read my Dropbox folder size. – jj_p Mar 5 '18 at 15:00
  • 1
    If you can find a way to do it with grep try to use time to compare the performance of both. – Jesse_b Mar 5 '18 at 15:01
  • 2
    @jj_p If you are running this on a dropbox folder every second, the "resource drain" is not going to be in the milliseconds differences in runtime between grep or awk or cut, it's going to be in running du on a remote filesystem. that'd be like worrying about a single drop of rain when your entire town is flooded. – cas Mar 6 '18 at 3:04
  • @cas well I'm running on local folder.. – jj_p Mar 6 '18 at 3:22
  • @jj_p even with a local folder, the disk I/O is going to consume far more resources than the negligible differences in CPU usage between the commands. Network I/O for dropbox or any other remote fs just makes it worse. – cas Mar 6 '18 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.