Advice required shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Advice required shell script
# 1  
Old 09-10-2011
Bug Advice required shell script

Hi all, this is my first post, so please be gentle...

I have a situation wherby I need a script that traverses known paths. Check for the modified date (n days) and then deletes all subdirs.
I have come up with this hotch potch, but as far as I can tell it seems to work.
What I am wondering is there any other way to make this cleaner?
Any help would be appreciated.
Oh this is on a QNAP NAS (busybox I think)


Create 2 files
  1. Includes-file (this contains all the path statements.i.e. /path1/subdir /path2/subdir/subdir etc)
  2. The script itself (del.sh)
Here is the script:

Code:
#!/opt/bin/bash
# cat all paths to cd
for i in `cat includes-file`;do
# find all directories in paths that have been modified more than 70 days ago and then delete them
cd $i; for path in `/opt/bin/find . -maxdepth 1 -mtime +70` ; do rm -rf $path
done
done

Cheers in advance.

Last edited by pludi; 09-10-2011 at 02:15 PM..
# 2  
Old 09-10-2011
Code:
for i in `cat includes-file`;do

is wrong. See: BashPitfalls - Greg's Wiki
This is cleaner:
Code:
while read p; do
  find "$p" -type d -mtime +70 | xargs printf "rm -rf %s\n"
done <includes-file

After checking that everything is ok, change "xargs ..." to "xargs rm -rf"
# 3  
Old 09-10-2011
a little more cleaner... Smilie
Code:
while read p; do   find "$p" -type d -mtime +70 -delete
done <includes-file

--ahamed
# 4  
Old 09-10-2011
And how '-delete' works with non-empty directories? Smilie
# 5  
Old 09-12-2011
Thank you both Yazu and Ahamed101.

I have put in Yazu's code, tested and works. I had a programmer at work explain the finder points of your script. I have never used "read" before.
Final code:
Code:
#!/opt/bin/bash
while read p; do
  find "$p" -maxdepth 1 -type d -mtime +60 | xargs "rm -rf %s\n"
done <includes-file

Once again, thank you for taking the time to look at this issue. It will be a big help to our organisations rolling backup regime.

Last edited by Franklin52; 09-12-2011 at 08:28 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script required

Hi, I need shell script for getting the date in format from below text output IP IS 10.238.52.65 pun-ras-bng-mhs-01#show conf port 2/4 Building configuration... Current configuration: ! card ge3-4-port 2 ! port ethernet 2/4 no shutdown encapsulation dot1q (7 Replies)
Discussion started by: surender reddy
7 Replies

2. UNIX for Advanced & Expert Users

Expertise advice required <<URGENT>>

:eek:i hav a shell script in my linux server, i want to execute it everyday once automatically without using cron tabs as i dont hav permission to create one. So wht sld i do??:confused: (1 Reply)
Discussion started by: Jay Thakkar
1 Replies

3. Shell Programming and Scripting

Help! Basic shell script advice

##### (2 Replies)
Discussion started by: AidoPotato
2 Replies

4. Programming

System + Network Programming, your advice required???

Dear friends, Before putting my questions forward, I would like to put some data infront of you, hope you will help me at the end. This website Cray-Cyber - Welcome provides free access to many supercomputers and mainframe computers. When you login through ssh, they provide you with a screen,... (5 Replies)
Discussion started by: gabam
5 Replies

5. UNIX for Advanced & Expert Users

System + Network Programming, your advice required???

Dear friends, Before putting my questions forward, I would like to put some data infront of you, hope you will help me at the end. This website Cray-Cyber - Welcome provides free access to many supercomputers and mainframe computers. When you login through ssh, they provide you with a screen,... (0 Replies)
Discussion started by: gabam
0 Replies

6. UNIX for Advanced & Expert Users

Career advice for experienced shell script coder, need help.

I have four years of shell scripting experience in AIX and HP-UX and have worked in perl scripts as well, the good part is i love scripting and so far i have been getting job offers as well. The bad part is , shell scripting is all i know , so the kind of jobs i am getting is mostly production... (2 Replies)
Discussion started by: harishrao
2 Replies

7. Shell Programming and Scripting

advice on shell script

Hello, I have this script running on cron every 20 minutes. By 12pm daily, our system is expecting all input files to be uploaded by the script. After this cutoff time, the script would still be running though, but i need some kind of alerts/logs to know which input files weren't received for... (1 Reply)
Discussion started by: gholdbhurg
1 Replies

8. Shell Programming and Scripting

Shell script help required

Hi, Can someone help me with this small piece of code. DIRNAME=$(dirname $0) BASENAME=$(basename $0) DATA="${DIRNAME}/${BASENAME}.data" && . $DATA whats is meant by && . $DATA here... Regards, Abhishek (2 Replies)
Discussion started by: max29583
2 Replies

9. Shell Programming and Scripting

Shell Script Required

I have following information in one file. ObjID: 004ee4e4-0d92-71dd-1512-9887a1f10000 Address: 152.135.0.61 PingState: Ping Responding ----------------Management Address--------------------- ++++++++++++++++Interface+++++++++++++++++++++ IFName: dall00r1.mis.amat.com ] ObjID:... (3 Replies)
Discussion started by: ntgobinath
3 Replies

10. Shell Programming and Scripting

c-shell script advice please.

Hi, I have the following script running in my cron. -------------------------------------------------------------------- #!/bin/csh bnstat -p GPD_VSLinux | grep pg | grep varcon | awk '{print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10}' > /tmp/LX_xbatch.log bnstat -p GPD_VSLinux_test | grep pg... (2 Replies)
Discussion started by: killerserv
2 Replies
Login or Register to Ask a Question