Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
 
Reply to this topicStart new topic
> Using Cron To Remove Specific Files
feef
post Jun 10 2004, 09:58 PM
Post #1


Whats this Lie-nix Thing?
*

Group: Members
Posts: 15
Joined: 10-June 04
Member No.: 3,135



Hi there.

I'm trying to use Cron to remove some files that are generated automatically on a daily basis.
There are these files (created on different days) in my temp folder, . .. 20040609 20040610 20040611 adj.txt counter.txt.
I need to keep adj.txt, counter.txt, and the file with today's date (20040611). What command should I use to only remove all files, except for adj.txt, counter.txt, and the file with today's date (20040611)?

Thanks! smile.gif
Go to the top of the page
 
+Quote Post
hughesjr
post Jun 11 2004, 06:00 PM
Post #2


Its GNU/Linuxhelp.net
*******

Group: Admin
Posts: 3,433
Joined: 25-July 03
From: Corpus Chrsiti, TX, USA
Member No.: 1,151



This file will remove all files that begin with the current year and are not today....it won't remove the files for 20041231 on 20050101 .... but it should work for everyday except YYYY1231.

It requires that the user running the cleanup script can write in the directory that is cleaned up.

CODE
#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
###############################
#replace /root/test with the path of the directory you
#want to clean
CLEANUP_DIR=/root/test
##############################
cd $CLEANUP_DIR
ls `date +'%Y'`* | grep -v `date +'%Y%m%d'` > remove_files

for file in `cat remove_files`
do
 rm -rf $file
done

rm -rf remove_files


There is basically 6 parts to the file:

1. CLEANUP_DIR=/root/test - the directory where we are going to erase files (replace /root/test with the path to the directory you want to clean).

2. ls `date +'%Y'`* - lists all files that begin with the current year in the current directory {which is why it won't work for YYYY0101 to delete (YYYY-1)1231}

3. | grep -v `date +'%Y%m%d'` - Remove today's date from the list of files.

4. > remove_files - Write the names of the files to be removed to a temp file (in the current directory) names remove_files

5. Section begining with for and ending with done ... For every line item in the remove_files, issue the command rm -rf filename.

6. rm -rf remove_files - Remove the temp file named remove_files.
-----------------
I named it /usr/local/bin/rm_old_files (paste the text in the code box into a text editor and save it to /usr/local/bin/rm_old_files)

the make it executable with the command:

chmod 755 /usr/local/bin/rm_old_files

test it by running it with the command:

/usr/local/bin/rm_old_files

If everything works, schedule it to run as a cron with the command:

crontab -e

press i to enter insert mode in vi

insert the following line (it will run at 0305 am everyday ... substitute the minute you want for 05 and the hour you want for 03)

05 03 * * * /usr/local/bin/rm_old_files

after you enter the line in insert mode ... press Esc to exit insert mode ... the press:

:wq! to write and exit the cron file.

the command:

crontab -l

should show the line:

05 03 * * * /usr/local/bin/rm_old_files


--------------------
Johnny Hughes
hughesjr@linuxhelp.net
Enterprise Alternatives: CentOS, WhiteBoxEL
Favorite Workstation Distros (in order): CentOS, Gentoo, Debian Sarge, Ubuntu, Mandrake, FedoraCore, Slackware, SUSE
Favorite Server Distros (in order): CentOS, WhiteBoxEL, Debian Sarge, Slackware, Mandrake, FedoraCore, Gentoo, SUSE
Go to the top of the page
 
+Quote Post
feef
post Jun 13 2004, 09:10 PM
Post #3


Whats this Lie-nix Thing?
*

Group: Members
Posts: 15
Joined: 10-June 04
Member No.: 3,135



Hi. Sorry, but I think I made a mistake in my eariler post. In my directory, there are these files.

drwxrwxrwx 3 webadmin webadmin 1.0K Jun 14 09:17 .
drwxrwxr-x 4 webadmin webadmin 1.0K Jun 11 17:33 ..
-rwxrwxr-- 1 root root 8 Jun 09 09:28 abc
-rwxrwxr-- 1 root root 8 Jun 10 09:28 def
-rwxrwxr-- 1 root root 8 Jun 11 09:28 ghi
-rw-r--r-- 1 root root 19K Jun 11 15:48 adj.txt
-rwxrwx--- 1 www www 1 Jun 14 09:17 counter.txt

I need to remove the files that are
  • not created on today
  • not adj.txt

I found out that the filenames that are generated are pretty random, so I guess I will have to remove them based on the day they were created, and not based on the filenames. Is there a way to do this?

I'm sorry I didn't get that in the first post. sad.gif

Thanks in advance!
Go to the top of the page
 
+Quote Post
hughesjr
post Jun 15 2004, 10:32 PM
Post #4


Its GNU/Linuxhelp.net
*******

Group: Admin
Posts: 3,433
Joined: 25-July 03
From: Corpus Chrsiti, TX, USA
Member No.: 1,151



OK use this one instead:

CODE
#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
###############################
#replace /root/test with the path of the directory you
#want to clean
CLEANUP_DIR=/root/test
##############################
cd $CLEANUP_DIR
ls -al | grep -v "`date +"%b %e "`" | grep -v adj.txt | awk '{ print $9 }' > remove_files

for file in `cat remove_files`
do
 if [ -f $file ]; then
   rm -rf $file
 fi
done

rm -rf remove_files


test it first (in a non-critical directory) ... but it worked for me.

you can copy files, but keep the their old date/time stamp with this command:

cp -p file_names

into a test directory and make sure it deletes what you want.
(if you don't want to delete files that start with a . then change ls -al to ls -l


--------------------
Johnny Hughes
hughesjr@linuxhelp.net
Enterprise Alternatives: CentOS, WhiteBoxEL
Favorite Workstation Distros (in order): CentOS, Gentoo, Debian Sarge, Ubuntu, Mandrake, FedoraCore, Slackware, SUSE
Favorite Server Distros (in order): CentOS, WhiteBoxEL, Debian Sarge, Slackware, Mandrake, FedoraCore, Gentoo, SUSE
Go to the top of the page
 
+Quote Post
feef
post Jun 15 2004, 11:40 PM
Post #5


Whats this Lie-nix Thing?
*

Group: Members
Posts: 15
Joined: 10-June 04
Member No.: 3,135



Thanks!
Works perfectly! laugh.gif
You've been a great help.
Go to the top of the page
 
+Quote Post
hughesjr
post Jun 16 2004, 06:35 AM
Post #6


Its GNU/Linuxhelp.net
*******

Group: Admin
Posts: 3,433
Joined: 25-July 03
From: Corpus Chrsiti, TX, USA
Member No.: 1,151



you're welcome smile.gif


--------------------
Johnny Hughes
hughesjr@linuxhelp.net
Enterprise Alternatives: CentOS, WhiteBoxEL
Favorite Workstation Distros (in order): CentOS, Gentoo, Debian Sarge, Ubuntu, Mandrake, FedoraCore, Slackware, SUSE
Favorite Server Distros (in order): CentOS, WhiteBoxEL, Debian Sarge, Slackware, Mandrake, FedoraCore, Gentoo, SUSE
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 16th October 2017 - 05:18 PM