ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'm learning bash scripting (the hard way, diving right in.) I'm making a script that compresses files and when the directory reaches a capacity (around 650-700MB) burn the data to cd.
My question is , how do I check the size of a directory in a bash script?
Originally posted by druuna Take a look at the du command.
$ du -h -s /home/
484M /home
The above command will count all subdirs too. The following command does not:
$ du -S -s -h /home
4.0K /home
See the manpage for more options.
Thanks, that's the first part that I need now but I'm still stuck, how do I get the numerical value into a variable. I can get the entire output into a variable.
Using your first example
484M /home
how do I get the 484 by itself so I can compare? I've been to several sites with great looking tutorials, but they all seem to lack clarity, I'm finding them confusing and they don't show examples useful to me.
Thanks for the help.
Last edited by IanChristie; 12-19-2003 at 11:04 AM.
The fieldseperator option is the key to this problem, some of the tools support this (cut, awk to name 2). When you don't set the fieldseperator a space (and sometimes a tab) are default.
The seperator needed in this case isn't one specific character:
484M /home <= The M would be the seperator, but there could also be a G or a K.
awk supports regular expressions in the fieldseperator field (-F). So here's the solution:
Here's another example, w/o awk. Only things it depends on are AFAIK a "regular" IFS and the flags for "df" cuz it only cuts the last char:
s=( $(du -h -s /home) ); let i=${#s[0]}-1; s=${s[0]:0:$i}
echo "Size is $s megs"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.