Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bash the script - Then start crying cos it wont wo
#1

I am a noob. I can do basic linux stuff but i am trying to write a script to make this easier. I have never written a script before and i am actually suprised that i have got this fat without rm'ing my entire system.

 

I have several directories that cameras upload images to all day. Basically i want this script to run as a cron job and run at midnight every night and delete all but the last 3 days worth of files.

 

I found the easeiest way to find the files was using the `find` command, so i part wrote and part found this command. If i type this in to the console it works :). It will delete all but the last 3 days worth of files.

 

find /path/to/directory/ -mtime +3 -daystart -exec rm {} \;

 

i cant celebrate too soon though. i need this command to run on 5 different directories in:

 

/path/to/directory2/

/path/to/directory3/

/path/to/directory4/

/path/to/directory5/

 

So i repeat the command 5 times and put it in to a .sh file and try and run it. :s it says

find: missing argument to `-exec' 4 times and the last line says:

rm: cannot remove `/path/to/directory4/': Is a directory

 

the script as it stands is:

 



Code:
cleanup.sh
find /path/to/directory/ -mtime +3 -daystart -exec rm {} \;
find /path/to/directory1/ -mtime +3 -daystart -exec rm {} \;
find /path/to/directory2/ -mtime +3 -daystart -exec rm {} \;
find /path/to/directory3/ -mtime +3 -daystart -exec rm {} \;
find /path/to/directory4/ -mtime +3 -daystart -exec rm {} \;




 

Please any help/advice is welcome

Thanks in advance

 

Jim

Reply
#2

pop into #linux-noob on EFNET (IRC) and ask someone there to look at this script issue,

 

i'm sure they'll be able to help you

 

cheers

 

anyweb

Reply
#3
What is the "cleanup.sh" supposed to do in there? Do you really have a script called like that in your $PATH? Does the first line of your script define the interpreter, i.e. "#!/bin/sh"?
Reply
#4

Quote:
Code:
cleanup.sh
find /path/to/directory/ -mtime +3 -daystart -exec rm {} \;
find /path/to/directory1/ -mtime +3 -daystart -exec rm {} \;
find /path/to/directory2/ -mtime +3 -daystart -exec rm {} \;
find /path/to/directory3/ -mtime +3 -daystart -exec rm {} \;
find /path/to/directory4/ -mtime +3 -daystart -exec rm {} \;

<div>









</div>
 

Ok... the script can shorten to:



Code:
#!/bin/sh
find /path/to/directory{,1,2,3,4}/ -mtime +3 -daystart -exec rm {} \;




 

Don't forget about shell expansion ;)

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)