Script for automatic deletion of old files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for automatic deletion of old files
# 1  
Old 03-21-2005
Script for automatic deletion of old files

Hi,
I have a folder with limited space. I do not have provisions to increase the space for this folder.
So i have to delete files which are more than 1 month old automatically.
But, i need to maintain the files created by 4 users and delete all the other files automatically which is more than 1 month.
So my script should be like i find all the files that do not belong to the 4 users and delete them if it is more than 1 month old.
Can someone help me to generate a script for this.
Regards,
Vivek
# 2  
Old 03-21-2005
auto delete of files

You could probably try something like the following. However be very wary of where you run this comman due to the "-f" option of the rm command.

cd destination_directory
rm -f `find * -mtime +30 | xargs ls -l | grep -v user1 | grep -v user2 | grep -v user3 | grep -v user4 | awk -F " " '{print $9}'`

Smilie

jerardfjay
# 3  
Old 03-21-2005
A little tidier:
Use with caution if running as root !!!

Code:
#!/bin/ksh
#
#removeFiles.sh
#
# Args $1 is the base directory to remove files from.
#

KEEPUSERS="user1|user2|user3|user4"

find ${1} -type f -mtime +30 -ls | egrep -v ${KEEPUSERS} | awk '{print $9}' | xargs rm -f

# 4  
Old 03-21-2005
A little faster...

find . ! -user user1 ! -user user2 ! -user user3 ! -user user4 -mtime +30 | xargs rm -f
# 5  
Old 09-09-2007
Hammer & Screwdriver thanks

thank you so much...

with this post i learn the power of xargs....

i make an script and maybe could be useful for you...

itīs checks the size of the directory and if pass the limit, then check the oldest file and delete it...

This procces is in a loop.... when the spaces fit the limits that you previusly input, the scripts stops...

here is the code:


#bin/bash
#valor 0 es ok
#valor 1 es demasiado grande
. /arwebmin/variables/variables.conf
estado=0
LIMIT=1
limite=$syslogsize

while [ "$estado" -lt "$LIMIT" ]
do

size=`du $dir_incoming | cut -f 1`

echo "$size"
if [ $size -ge $espacio_maximo ] ; then
echo "es mayor"
echo "borrando: $dir_incoming$archivo"
ls -tb $dir_incoming | tail -1 | xargs rm -f

else
echo "es menor"
estado="1"

fi

done


---------------------------------

regards!.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Debian

Problem with files/dirs deletion

Hi, The other day i installed a PHP based CMS (modx) on my shell account and noticed that i couldn't delete any of files/dirs it created after. Also, i noticed that all that stuff is owned by username-www instead of username. I tried chown, chmod and using a PHP script to do the same wti... (4 Replies)
Discussion started by: pentago
4 Replies

2. Shell Programming and Scripting

deletion of multiple occurring files

Hi all, In the given file, what I need to do is to select double (or triple) occurring files and delete the one having smaller file size. And would like to keep the one with the big file size. 518t-rw-r--r-- 1 emily us_cms 101348458 Oct 8 16:43 vgtree_518_0_LHB.root 518t-rw-r--r-- 1... (9 Replies)
Discussion started by: emily
9 Replies

3. UNIX for Dummies Questions & Answers

Periodical Deletion of files and folders

Hi All, how can Periodical Delete files and folders using shell script. I.e after 1 min i want to delete files and folderns from my home directory. Thanks, Arun (9 Replies)
Discussion started by: arun508.gatike
9 Replies

4. Shell Programming and Scripting

Files Deletion After 20 Minutes

Hi, everyone. Could you help me with deletion of files, which are 20 minutes old. I found out how to make deletion for files in that way : find <dir> -mtime n -exec rm -rf "{}" Could you offer your suggestions. Many thanks in advance. (5 Replies)
Discussion started by: KReoN
5 Replies

5. Shell Programming and Scripting

Fake deletion of files

Hi, This is possibly an odd request to do with permissions as I seem to have tied myself up with these! I have the following directory (see below) that contains files that the 'usergrp' user needs to be able to 'delete' files from. drwxr-s--- 2 usergrp usergrp 512 16 Feb 14:37... (2 Replies)
Discussion started by: Peejay
2 Replies

6. Solaris

Script for automatic deletion of old folder

Hi, I have a folder with limited space. So i have to delete folder which are more than 5 days old automatically. So my script should be like delete the folder more than 5 days old. Can someone help me to generate a script for this. Thank you... Cheer Summer (5 Replies)
Discussion started by: summerpeh
5 Replies

7. UNIX for Dummies Questions & Answers

conditional deletion of log files

i am a newbie and learning the ropes.........want to know how can i include a piece of code in a script (which redirects log files to a company standard out file) to delete the log files which are empty but should retain only those which has some process information in it......this should happen... (3 Replies)
Discussion started by: sonali007
3 Replies

8. Solaris

Script for automatic deletion of trash file of mail server

Hi, I have a mail server with limited space and operating system is sun solaris 8 (sparc). I do not have provisions to increase the space for home directory. So i have to delete files from /home/username/mail/trash which are more than 10 days old automatically. So my script should be like... (1 Reply)
Discussion started by: crown2100bd
1 Replies

9. UNIX for Dummies Questions & Answers

Deletion of log files.

We have log files dating back to 2004. I need to write an interative script that will request the user for how many months he needs to keep the log files and remove all the remaing log files after that month. Supposing we are now in June 2006 , if teh user request to keep log file for the last 3... (1 Reply)
Discussion started by: Geeta
1 Replies

10. Shell Programming and Scripting

Regarding deletion of old files

Hi, I have a list of directories which contain old files that are to be deleted. I put the list of all directories in a txt file and it is being read by a script Iam searching for the files older than 60 days using mtime and then deleting it But all the files are getting deleted... (3 Replies)
Discussion started by: Chidvilas
3 Replies
Login or Register to Ask a Question