LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2003, 12:09 PM   #1
IanChristie
Member
 
Registered: Oct 2003
Posts: 39

Rep: Reputation: 15
Comparing file sizes using a bash script.


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?
 
Old 12-16-2003, 12:22 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
1 members found this post helpful.
Old 12-19-2003, 11:00 AM   #3
IanChristie
Member
 
Registered: Oct 2003
Posts: 39

Original Poster
Rep: Reputation: 15
Quote:
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.
 
Old 12-19-2003, 11:36 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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:

$ du -h -s /home/
539M /home

$ du -h -s /home/ | awk -F"[GKM]" '{ print $1 }'
539

Maybe there's an easier way to do this, this just came to mind first.
 
1 members found this post helpful.
Old 12-19-2003, 01:24 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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"
 
Old 12-19-2003, 10:14 PM   #6
IanChristie
Member
 
Registered: Oct 2003
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks for the help, those are both great ways, unSpawn's one worked out easier in my circumstance.

Thanks again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing a File in a Bash Script TGWDNGHN Programming 4 12-02-2005 02:38 PM
making a bash script file Berticus Linux - General 1 10-15-2005 11:14 PM
Can't get lines of a file with a Bash script.. barisdemiray Programming 2 08-11-2004 12:42 PM
bash - comparing a variable to several values davee Programming 3 05-05-2003 07:26 AM
file editing in a bash script Harpune Programming 4 11-22-2002 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration