The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multiple Files deletion in perl pulkit Shell Programming and Scripting 1 02-12-2008 05:55 AM
conditional deletion of log files sonali007 UNIX for Dummies Questions & Answers 3 10-03-2007 11:56 AM
Script for automatic deletion of trash file of mail server crown2100bd SUN Solaris 1 09-20-2007 08:01 AM
Deletion of log files. Geeta UNIX for Dummies Questions & Answers 1 06-23-2006 07:04 AM
Regarding deletion of old files Chidvilas Shell Programming and Scripting 3 12-27-2005 10:05 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-21-2005
vivek_scv vivek_scv is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 4
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 (permalink)  
Old 03-21-2005
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
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}'`



jerardfjay
  #3 (permalink)  
Old 03-21-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,206
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 (permalink)  
Old 03-21-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
A little faster...

find . ! -user user1 ! -user user2 ! -user user3 ! -user user4 -mtime +30 | xargs rm -f
  #5 (permalink)  
Old 09-09-2007
nachazo nachazo is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 1
Wink 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!.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0