LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-20-2008, 09:21 AM   #1
Twelveone
LQ Newbie
 
Registered: May 2006
Location: Dunfermline, Scotland
Distribution: Ubuntu 8.04 LTS
Posts: 10

Rep: Reputation: 0
Question Using a directory listing as the argument/variable in a script


Hi,

I have written a simple script to check the size of a specific directory and if it is above a limit delete the oldest file.

********** Script ***********

dir="/var/log/gassensor/" # Directory to be checked for size
maxsize=100000 # Max allowed size of directory
size=0 # Will be updated by get_size_of_dir
delfile="" # Will be updated by delete_oldest_file

delete_oldest_file(){
delfile=$( ls -tr1 $dir | head --lines 1) # Get oldest file
delfile="${dir}${delfile}" # Add path to file
rm $delfile # Delete file
}

get_size_of_dir(){
size=$( du -s $dir | awk '{print $1}' ) # Get size of dir
}

get_size_of_dir # Read the total size of dir into variable size

while [ "${size}" -gt "${maxsize}" ] # If $dir is too big then do ...
do
delete_oldest_file # Function - Identifies and deletes oldest file
get_size_of_dir # Read the total size of dir into variable size
done

************ End Script *****************

I would now like to extend it to deal with multiple directories under a parent directory. I need to read the directory /var/log/gassensor/ then check each directory within that for size and delete the oldest files if necessary. e.g.

# ls /var/log/gassensor/

drwxr-xr-x 3 admin32 admin32 4.0K 2008-06-20 13:52 StackA
drwxr-xr-x 3 admin32 admin32 4.0K 2008-06-20 13:52 StackB

Then run the script with $dir = "/var/log/gassensor/StackA"
and then $dir = "/var/log/gassensor/StackB"

Does anyone have any ideas how this might be done?

Thanks
 
Old 06-20-2008, 12:19 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,431

Rep: Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824
In your original script,
Code:
dir="$1" # Directory to be checked for size
Then to run it on all directories in /var/log/gassensor/:
Code:
find /var/log/gassensor/ -maxdepth 1 -type -d -exec /path/to/your/script '{}' \;
 
Old 06-23-2008, 04:53 AM   #3
Twelveone
LQ Newbie
 
Registered: May 2006
Location: Dunfermline, Scotland
Distribution: Ubuntu 8.04 LTS
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks for the reply, you suggest using
Code:
find /var/log/gassensor/ -maxdepth 1 -type -d -exec /path/to/your/script '{}' \;
This however outputs the parent directory and then the subdirectories. This causes the script to try to delete the subdirectories which it can't. Is there any way of preventing the parent directory from being included in the output?
 
Old 06-23-2008, 08:51 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,431

Rep: Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824
Ah, sorry, forgot about that detail.
Code:
find /var/log/gassensor/ -maxdepth 1 -mindepth 1 -type -d -exec /path/to/your/script '{}' \;
 
  


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
need help with a ruby script listing directory contents Joseph Schiller Programming 4 12-31-2006 11:46 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM
Need shell script to concatenate a string and a variable into a directory name AwesomeMachine Linux - Newbie 2 05-07-2006 03:42 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
directory listing script linuxzouk Programming 2 06-04-2004 12:07 PM


All times are GMT -5. The time now is 11:35 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration