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?
Asked
Active
Viewed 472 times
1

Rui F Ribeiro
- 56,709
- 26
- 150
- 232

jj_p
- 283
2 Answers
7
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
andgrep
to be faster thansed
, andsed
to be faster thanawk
."
Quoted from here
The ouput of time
for both commands shows:
time du -sh /folder | awk '{print $1}'
60K
real 0m0.005s
user 0m0.002s
sys 0m0.004s
time du -sh /folder | cut -f1
60k
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".

alpha
- 1,994
3
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

jesse_b
- 37,005
-
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 05 '18 at 15:00
-
1If you can find a way to do it with grep try to use
time
to compare the performance of both. – jesse_b Mar 05 '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 06 '18 at 03:04 -
-
@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 06 '18 at 03:32
-h
(human-readable) todu
: you want the sizes to be listed in bytes for everything, to make it easier to combine the values. – Daniel Pryden Mar 05 '18 at 19:20