Jump to content
Sign in to follow this  
jimpy90

Bash the script - Then start crying cos it wont wo

Recommended Posts

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 :D. 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:

 

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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"?

Share this post


Link to post
Share on other sites
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 {} \;

 

Ok... the script can shorten to:

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

 

Don't forget about shell expansion ;)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...