Click to See Complete Forum and Search --> : Bash Script


nunder
05-09-2001, 10:49 AM
I've read the man pages for bash, but can't figure this one out, and I know that I'm gonna get flamed for this, but I have to ask (because I can't figure it out). I've got a directory that gets filled up really quick, and daily I have to delete the files from the previous day. At any rate, the filenames are based on date (todays first one is 0509200101). I'd like to write a script that will delete yesterdays files (and any that were piped in late). The thing that I've got in mind would be something like ls | grep -v <todays date> | rm {}. Now, the {} is what I can't seem to figure out. I've seen it before, but can't remember what it is. Any help would be greatly appreciated.

thor4linux
05-09-2001, 01:14 PM
If its just a bunch of files that start with day as in 05092001*.*, all you have to do is type "rm 05092001*.*" and bash will rm all files starting with what you specify. so every day, you login and type "rm date*.*" and walla, they're gone.

b

demian
05-09-2001, 01:30 PM
How does
find . -mtime 1 -exec rm -f {} \;
sound to you?

nunder
05-09-2001, 02:32 PM
Actually, that (find . -mtime 1 -exec rm -f {} \ ;) sounds good, but I need to be able to specify the <date>. The directive that I was given (by a non-technical person) wasn't really what they needed, but I can't make them understand it until I can show them. Once I show them, I'll need to incorporate this little script into a more complicated one (i.e. the <date> will be replaced with a variable, but I'm not sure what the variable will be yet). I know it sounds kinda dumb, but I'm in a bit of a quandry. :confused:

demian
05-09-2001, 03:38 PM
If the files are always named datesomething you could use find with pattern matching. Say you variable is called today (you might want to format it with sed so that it matches the filenames) you can do something like
find . -not -name $today'*' -exec rm -f {} \;
in a script. This, of course, deletes every file whose name doesn't start with today so if there are files other than those called datesomething in the directory you need to elaborate a bit on the above command.

nunder
05-09-2001, 03:55 PM
I think that I may have asked the question the wrong way <sorry>. I just wrote a little script (that I could probably setup as a function in the new script <when my supervisor asks for it> that accomplishes this task. Here is the script. What I'd be looking for would be a way to do this on one line, without creating the $GO variable:

#!/bin/bash
GO=$(ls | grep -v 050)
rm -rf $GO

Sorry if this sounds stupid.

demian
05-09-2001, 04:09 PM
I think I finally got it:
ls |grep -v 050|xargs -t rm

[ 09 May 2001: Message edited by: demian ]

Coral Sea
05-09-2001, 08:45 PM
You can use cron to schedule a job to run periodically to clean out a directory. Put a line in /etc/crontab that looks like:

0 1 * * * find /dir -atime -atime 1 -exec ls -l {} \;

and make sure the cron daemon is running by issuing

chkconfig crond on

You'll need to be root for all of the above.

Now, every morning at 1:00 a.m., cron will run and delete all files out of /dir that have not been accessed for one day.

Coral Sea
05-09-2001, 08:46 PM
Whoops, one too many -atimes.

Coral Sea
05-09-2001, 09:00 PM
Ignore the bit about editing /etc/crontab. Use crontab -e to create the crontab entries. Assuming it launches vi (or vim) by default on your system, remember to :wq to save your work. You can check it by issuing crontab -l.

S0larfluX
05-09-2001, 09:04 PM
http://www.linuxdoc.org/HOWTO/Adv-Bash-Scr-HOWTO/

Coral Sea
05-09-2001, 09:22 PM
Must be dozy today. The command I've shown will list the files, not delete them. Replacing ls -l with rm -f should delete them.

nunder
05-10-2001, 09:23 AM
Outstanding, the line that demian gave me worked like a champ. Thank all of you guys for all of your help!!!!!! It's not much, but if I can be of any assistance, definately don't hesitate to let me know.