LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-21-2011, 02:43 AM   #1
thetiger2003
LQ Newbie
 
Registered: Apr 2009
Posts: 23

Rep: Reputation: 0
script needed


hello
i need a script that delete all files in a specific directory every 3 hours

thnx & best regards
 
Old 09-21-2011, 02:51 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sounds good ... what have you tried?
 
Old 09-21-2011, 03:03 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I guess you need a crontab entry, not really a script. Please, provide more details.
 
Old 09-21-2011, 03:05 AM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Hello,

As far as running it every three hours, you can use cron for that. I highly suggest you read the man page and search around on how to use cron, as it is all over the internet. As far as your script, using basic linux commands, you can rm files as such:
Code:
rm -f /path/to/directory/*
Of course, you would put that in a script.

Cheers,

Josh
 
Old 09-21-2011, 03:14 AM   #5
Pastychomper
Member
 
Registered: Sep 2011
Location: Scotland
Distribution: Slackware, Devuan, Android
Posts: 132

Rep: Reputation: 243Reputation: 243Reputation: 243
I agree, cron would be the standard (and probably best) way.

One alternative might be the watch command, like

watch -n 10800 rm /foo/bar/*
 
Old 09-21-2011, 03:18 AM   #6
nicolasdiogo
Member
 
Registered: Oct 2003
Location: UK
Distribution: debian lenny x64
Posts: 130

Rep: Reputation: 20
you will be better off creating a script that is run by cron every 3 hours.

firstly create your script, i choose root's own home folder (remember to back it up)
and set permissions to enable execution
the commands below are done as 'root' to ensure that files can be deleted (remember that these commands will run as root so it can cause damage if you do not know what/when you are deleting)
Code:
touch /root/scripts/clear_myFolder
chmod +x /root/scripts/clear_myFolder
then add the command required to be run.
when you say 'delete all files'

do you want to leave the directory structure intact?

if you want to keep the structure, you can amend this code snippet:
http://www.brainpowered.net/cms/cont...l-bash-snipets

into something like this:
Code:
find <PATH TO ROOT FOLDER> -type f -print | while read i; do rm $FILE; done
or, if you just want to delete everything in that folder
Code:
rm -R <PATH TO ROOT FOLDER>/*
note the '/*'
that will preserve your folder but delete everything inside it.

so your script has something like:
Code:
#!/bin/bash

# comment - cleaning my folder (or use the other snippet
rm -R <PATH TO ROOT FOLDER>/*
then create a crontab entry to run this script whenever you wish, see:
http://crontab.org/
http://corenominal.org/howto-setup-a-crontab-file/

Code:
crontab -e
* */3 * * * /root/scripts/clear_myFolder
if you find confusing to setup crontab (i do) look at something like this:
http://www.corntab.com/pages/crontab-gui

you can just as well replace the path to your script with the actual command - but having the command outside the crontab gives you more flexibility - in my humble opnion.

hope this helps
 
Old 09-21-2011, 04:38 AM   #7
thetiger2003
LQ Newbie
 
Registered: Apr 2009
Posts: 23

Original Poster
Rep: Reputation: 0
thnx alot for ur effort

Last edited by thetiger2003; 09-21-2011 at 04:39 AM.
 
  


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
Start-Up Script & Shutdown/Kill Script needed guggilamsandeep Red Hat 1 05-11-2011 08:58 AM
serious script help needed bino25 Linux - Newbie 8 02-18-2011 04:15 PM
Script help needed! Atif_Khan Linux - Software 25 09-03-2007 12:51 AM
Script needed ganeshinforum Programming 2 01-05-2007 07:29 AM
little script help needed abisko00 Programming 7 07-11-2005 08:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:25 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