Help - Search - Members - Calendar
Full Version: Using Cron To Remove Specific Files
Linuxhelp > Support > Technical Support
feef
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
hughesjr
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
feef
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!
hughesjr
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
feef
Thanks!
Works perfectly! laugh.gif
You've been a great help.
hughesjr
you're welcome smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.