Reputation: 179
I need to get the size of my folder, for comparison. But I get the folder size with the path. How to get only the size of my folder? I use the following command in bash
:
size=`du -b --max-depth=0 ./main_folder/data`
Output:
12260550 ./main_folder/data
Expected:
12260550
Upvotes: 5
Views: 18300
Reputation: 443
Outdated! The most sensical answer to me is the following
du -b --max-depth=0 ./main_folder/data | awk '{print $1;}'
Returns first word of output. Enjoy.
New! Six years later, du
has changed its options. In 2025, now recommending the simpler,
du -s ./main_folder/data | awk '{print $1;}'
Upvotes: -1
Reputation: 204
I like this one liner:
myvar=$(du -b --max-depth=0 ./main_folder/data|cut -f1 -d$'\t')
Upvotes: 0
Reputation: 696
du -sh ./main_folder/data | awk '{print $1;}'
s -> summarizes all the contents of the folder
h -> return output in human readable form
Upvotes: -1
Reputation: 28300
cut
is a dedicated command for that type of manipulation on strings.
size=`du -b --max-depth=0 ./main_folder/data | cut -f1`
Here -f1
means "cut the first field" (on each line of piped input, but there is only one line).
Upvotes: 2
Reputation: 785991
Since you're trying to store size in a variable, you can just take first value and ignore rest as:
read size _ < <(du -b --max-depth=0 main_folder/data)
echo "size=$size"
Upvotes: 2