Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

Rep: Reputation: 0
script needed

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

thnx & best regards
Old 09-21-2011, 03:51 AM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,642

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

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

Rep: Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004

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:
rm -f /path/to/directory/*
Of course, you would put that in a script.


Old 09-21-2011, 04:14 AM   #5
Registered: Sep 2011
Location: Scotland
Distribution: Debian
Posts: 98

Rep: Reputation: 116Reputation: 116
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, 04:18 AM   #6
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)
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:

into something like this:
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
note the '/*'
that will preserve your folder but delete everything inside it.

so your script has something like:

# comment - cleaning my folder (or use the other snippet
then create a crontab entry to run this script whenever you wish, see:

crontab -e
* */3 * * * /root/scripts/clear_myFolder
if you find confusing to setup crontab (i do) look at something like this:

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, 05:38 AM   #7
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 05:39 AM.


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 09:58 AM
serious script help needed bino25 Linux - Newbie 8 02-18-2011 05:15 PM
Script help needed! Atif_Khan Linux - Software 25 09-03-2007 01:51 AM
Script needed ganeshinforum Programming 2 01-05-2007 08:29 AM
little script help needed abisko00 Programming 7 07-11-2005 09:37 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration