Results 1 to 10 of 11
Warning, I am still a Newbie
My web server is running Linux ( don't know what flavor ) and I have set up a Cron job to delete all files ...
- 08-03-2007 #1Just Joined!
- Join Date
- Aug 2007
- Location
- Utah, USA
- Posts
- 4
Bash Script for a Cron Job > Deleting files in a directory.
Warning, I am still a Newbie
My web server is running Linux (don't know what flavor) and I have set up a Cron job to delete all files in my "files" directory.
This is what I have right now:
However if there are no files, I keep getting "No files found in "files/" directory.Code:rm /home/user/upload/files/*.*
So I am looking for a bash command that does this:
(pseudo code)
Simple enough right?Code:if(FilesExist) { if(Files != ".htaccess") { rm /home/user/upload/files/*.* EXCEPT .htaccess } }
This is where it gets tricky.
My web server (long story) only allows me to do this on ONE LINE and I cannot have multiple lines.
Does anyone know how to do this?
Second Thought...
Is there a way that I could create a Bash script that would run the above code and I could just run the script in my Cron job instead?
I am very new at writing bash scripts and dont really know how to write them, so as much help you can provide, the better.
Thank you in advance!!!
- 08-03-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Hi,
The rm comand don't removes files if their names are started with a dot.
To suppress the error messages you can redirect them to /dev/null.
Here you can read how to run a command as a cron job:Code:rm /home/user/upload/files/* 2>/dev/null
crontab - Wikipedia, the free encyclopedia
Because the PATH variable is not always set in cronjobs you must use the absolute path:
Code:/bin/rm /home/user/upload/files/* 2>/dev/null
Regards
- 08-03-2007 #3
Hint: user -not option with find command and -maxdepth 1 option too...for deleting files at the current dir level only. if you use -not option, then give -name "*<thefilename>*" or the correct pattern of the file that you don't wana delete...consists of.
- 08-03-2007 #4
- 08-03-2007 #5Just Joined!
- Join Date
- Aug 2007
- Location
- Utah, USA
- Posts
- 4
This is what I ended up coming up with...
Does it look sound to you or do you see any potential problems?
I will most likely take into consideration what you have also noted.
(i have the cron job execute this like every 15 minutes) [lots of traffic].
Code:#!/bin/sh DIR="/home/username/public_html/upload/files/" LIST=`ls -l $DIR | grep -v "total 0"` FILES=`ls $DIR` cd $DIR if [ -z "$LIST" ] then exit else echo "Files Delete:" echo $FILES rm -f * fi
- 08-03-2007 #6Just Joined!
- Join Date
- Aug 2007
- Location
- Utah, USA
- Posts
- 4
However
The said method above does not address the .htaccess issue.
- 08-03-2007 #7Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
To remove dot files you can do something like this:
RegardsCode:rm .[a-zA-Z0-9]*
- 08-03-2007 #8Just Joined!
- Join Date
- Aug 2007
- Location
- Utah, USA
- Posts
- 4
- 08-03-2007 #9Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
- 08-04-2007 #10Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
rm -f .??* is much easier!


Reply With Quote
