Shell Script to Remove Old Files
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix administration > Shell Script to Remove Old Files




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Shell Script to Remove Old Files  
KAKA


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-07 06:26 AM

How do I write a shell script to remove files (recursively under a
directory) which is older than 6 months old?  Thank you!






[ Post a follow-up to this message ]



    Re: Shell Script to Remove Old Files  
Dave Hinz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-07 12:26 PM

On 7 Mar 2007 21:39:46 -0800, KAKA <kaka.hui@gmail.com> wrote:
> How do I write a shell script to remove files (recursively under a
> directory) which is older than 6 months old?  Thank you!

man find
man xargs






[ Post a follow-up to this message ]



    Re: Shell Script to Remove Old Files  
Lew Pitcher


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-07 06:20 PM

On Mar 8, 12:39 am, "KAKA" <kaka....@gmail.com> wrote:
> How do I write a shell script to remove files (recursively under a
> directory) which is older than 6 months old?  Thank you!

Dave Hinz has given you an excellent hint on /how/ to do this.

I, on the other hand, will caution you to well define what you mean by
"older than 6 months old". Remember, Unix does not capture the file /
creation/ date, so (from the file metadata) you cannot determine which
files were /created/ more than 6 months ago. The best you will be able
to do is determine which files were /last read/ or /last updated/ more
than 6 months ago.

HTH
--
Lew






[ Post a follow-up to this message ]



    Re: Shell Script to Remove Old Files  
Miles


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-08-07 06:20 PM

On Mar 7, 11:39 pm, "KAKA" <kaka....@gmail.com> wrote:
> How do I write a shell script to remove files (recursively under a
> directory) which is older than 6 months old?  Thank you!

Here's the long answer. Note it needs a TSM server:

#!/usr/bin/ksh93
#
# Name: rm_old_files
#
# Purpose: To remove files from a directory that are more than x days
old
#
# Type: Permenant
#
# Date: Dec 1998
#
# Required Parameters:
#
-------------------------------------------------------------------------
# Directory: the directory to search, and its sub-directories
# Days: files older than the number of days to be deleted
#
# Optional Parameters:
#
-------------------------------------------------------------------------
# None.
#
# Change History:
#
# Date          Name            Comments
#
 ________________________________________
_________________________________
# Apr 1999      purdym          added backup to ADSM
# 14 jul 2003   purdym          added quotes at begining and end of
file name
# 2 Sep 2003    purdym          changed find command from:
#                               find $STARTDIR -fstype jfs -type f -
xdev -mtime +${DAYSOLD} -exec ls -1 {} \;
#                               to:
#                               find $STARTDIR \( -fstype jfs -o -
fstype jfs2 \) -type f -xdev -mtime +${DAYSOLD} -exec ls -1 {} \;
# 21 jun 2004   purdym          started to standardize the script
#
# 27 may 2005   purdym          added check for free space
#
#
 ________________________________________
_________________________________

 ########################################
####################################
#######
# print_instructions
 ########################################
####################################
#######
print_instructions () {
cat <<EOF

Syntax: $0 directory days

This script will remove files from 'directory' that are more
than 'days' days old.


This script does the following:
1. Backups the files up to TSM
2. Deletes them

EOF
}

 ########################################
####################################
#######
# Verify command line arguments
 ########################################
####################################
#######
if [[ $# -ne 2 ]]
then
print_instructions
exit -1
fi

 ########################################
####################################
#######
# command line args
 ########################################
####################################
#######
STARTDIR=$1
DAYSOLD=$2

 ########################################
####################################
#######
# Variables
 ########################################
####################################
#######
DEBUG=0                         # 0=false; 1=true
PROGRAM_NAME=$0
set_variables

 ########################################
####################################
#######
# Check for system type
 ########################################
####################################
#######
SYS_NAME=$(uname -s);
(( $DEBUG == $TRUE )) && print "System type is: $SYS_NAME";

if [[ $SYS_NAME == "AIX" ]]
then
SYS_TYPE=1

elif [[ $SYS_NAME == "HP-UX" ]]
then
SYS_TYPE=2

elif [[ $SYS_NAME == "Linux" ]]
then
SYS_TYPE=3

else
print "Unknown system: $SYS_NAME.";
error_exit
fi

 ########################################
####################################
#######
# Variables
 ########################################
####################################
#######
RETCODE1=$?; if (( $RETCODE == 0 )); then RETCODE=$RETCODE1; fi
(( $DEBUG == $TRUE )) && print "DEBUG: "

LOG_FILE1=$WORK_DIR/${0##*/}.1.$$.tmp
LOG_FILE2=$WORK_DIR/${0##*/}.2.$$.tmp

FILELIST=$WORK_DIR/${0##*/}.filelist.$$.tmp
COMMAND_FILE=$WORK_DIR/${0##*/}.command_file.$$

 ########################################
####################################
#######
#
 ########################################
####################################
#######
echo "\n\n$0 started at $(date)."
echo $SEP1 | cut -c -${COLUMNS}

 ########################################
####################################
#######
# Checks
 ########################################
####################################
#######
if [[ ! -d $STARTDIR ]]
then
echo "\nError: $STARTDIR does not exist.\n"
echo "The 1st parameter must specify the starting directory.\n"
RETCODE=-3
error_exit
exit -3
fi

if [[ $STARTDIR = '/' || $STARTDIR = '/usr' || $STARTDIR = '/etc' ||
$STARTDIR = '/var' || $STARTDIR = '/home' || $STARTDIR = '/spdata' ||
$STARTDIR = '/proc' ]]
then
echo "\nERROR: Can't use $STARTDIR.\n"
RETCODE=-4
error_exit
exit -4
fi

if [[ $DAYSOLD != [0-9]* ]]
then
echo "\nERROR: The 2nd parameter must be the minimum age in days,
in the form: [0-9]*.\n"
RETCODE=-5
error_exit
exit -5
fi

 ########################################
####################################
#######
# check for free working space
 ########################################
####################################
#######
$UNIX_SYSTEM_DIRECTORY/bin2/check_fs_for_free_megabytes $WORK_DIR 5

 ########################################
####################################
#######
# list the files first
 ########################################
####################################
#######
echo "\n$SEP" | cut -c -$COLUMNS
print "STEP: Files selected for removal:"
find $STARTDIR \( -fstype jfs -o -fstype jfs2 \) -type f -xdev -mtime +
${DAYSOLD} | sed 's/^\(.*\)$/"\1"/' | tee $FILELIST
RETCODE1=$?; if (( $RETCODE == 0 )); then RETCODE=$RETCODE1; fi
print "Return code before backing up of files using dsmc: $RETCODE"

 ########################################
####################################
#######
# backup the files
 ########################################
####################################
#######
echo "\n$SEP" | cut -c -$COLUMNS
print "STEP: Backing up the files (if they need to be)..."
dsmc inc -filelist=$FILELIST | egrep -vi "Tivoli Storage Manager|
Command Line|Copyright IBM|Copyright by IBM|Session established|Client
Version|Client date|Server Version|Server date|ANS1891W"
DSM_RETURN_CODE=$?                                              # save
this value for further processing
print "\nDone.\nReturn code of the dsmc command was: $DSM_RETURN_CODE"

if (( $DSM_RETURN_CODE == 8 ))
then
print "\nINFORMATION: No files met the specifications to be backed
up. No files needed to be backed up. Delete will still proceed."

elif (( $DSM_RETURN_CODE != 0 ))
then
print "\n$SEP\nWARNING: BACKUP FAILED: Error $DSM_RETURN_CODE
\nRetrying...\n$SEP\n"
dsmc inc -filelist=$FILELIST | egrep -vi "Tivoli Storage Manager|
Command Line|Copyright IBM|Copyright by IBM|Session established|Client
Version|Client date|Server Version|Server date|ANS1891W"
DSM_RETURN_CODE=$?                                           # save
this value for further processing
fi

 ########################################
####################################
#######
# remove the files?
 ########################################
####################################
#######
if (( $DSM_RETURN_CODE == 0 || $DSM_RETURN_CODE == 8 ))
then
# Actually remove the files
echo "\n$SEP" | cut -c -$COLUMNS
print "STEP: Removing the files now...\c"
xargs rm < $FILELIST
RETCODE1=$?; if (( $RETCODE == 0 )); then RETCODE=$RETCODE1; fi
echo "Done.\nReturn code of rm command was: $RETCODE"

else
print "\nWARNING: It doesn't look like the backup worked. No files
will be deleted."
RETCODE=-5
error_exit
fi

 ########################################
####################################
#######
# clean up
 ########################################
####################################
#######
remove_file $FILELIST
remove_file $COMMAND_FILE

 ########################################
####################################
#######
# exit
 ########################################
####################################
#######
echo "\n$SEP1" | cut -c -${COLUMNS}
print "$0 Ended at $(date)"
echo $SEP1 | cut -c -${COLUMNS}

exit $RETCODE






[ Post a follow-up to this message ]



    Re: Shell Script to Remove Old Files  
Bill Vermillion


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-25-07 06:21 PM

In article <1173361315.519225.136230@t69g2000cwt.googlegroups.com>,
Lew Pitcher <lpitcher@sympatico.ca> wrote:
>On Mar 8, 12:39 am, "KAKA" <kaka....@gmail.com> wrote: 
>
>Dave Hinz has given you an excellent hint on /how/ to do this.
>
>I, on the other hand, will caution you to well define what you mean by
>"older than 6 months old". Remember, Unix does not capture the file /
>creation/ date, so (from the file metadata) you cannot determine which
>files were /created/ more than 6 months ago. The best you will be able
>to do is determine which files were /last read/ or /last updated/ more
>than 6 months ago.
>
>HTH
>--
>Lew

Using the 'ctime' - inode change time - would be the only way to
get close to being correct.  The atime shows last access time and
backup programs often [improperly IMO] update that.  mtime
would show when the file was last updated, so the ctime [which some
*i*x documentation improperly documents as 'creation' time] is
probably the best bet.

The UFS2 in FreeBSD with it's inodes being twice as large as the
original Unix inodes, is supposed to have a 'creation' time in
addition to change, modify and access.  But I don't see
documentation on that.

BIll


--
Bill Vermillion - bv @ wjv . com





[ Post a follow-up to this message ]



    Re: Shell Script to Remove Old Files  
Frank Cusack


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-26-07 06:36 AM

On Sun, 25 Mar 2007 17:55:01 GMT bv@wjv.com (Bill Vermillion) wrote:
> The UFS2 in FreeBSD with it's inodes being twice as large as the
> original Unix inodes, is supposed to have a 'creation' time in
> addition to change, modify and access.  But I don't see
> documentation on that.

It's called birth time, maybe that term is documented?
-frank





[ Post a follow-up to this message ]



    Re: Shell Script to Remove Old Files  
Bill Vermillion


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-26-07 06:21 PM

In article <m23b3t3wvh.fsf@sucksless.local>,
Frank Cusack  <fcusack@fcusack.com> wrote:
>On Sun, 25 Mar 2007 17:55:01 GMT bv@wjv.com (Bill Vermillion) wrote: 
>
>It's called birth time, maybe that term is documented?
>-frank

I don't find documentation, but looking at the ufs sources I
see it in dinode.h  and in ufs_vnops.c, and it's part
of   "struct ufs2_dinode"

It follows in order after access time, modified time,
and change time.

Thanks for the name - as I was wondering what it was called.

Now I can go learn some more.

Bill


--
Bill Vermillion - bv @ wjv . com





[ Post a follow-up to this message ]



    Re: Shell Script to Remove Old Files  
Michael Tosch


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-28-07 12:26 AM

Bill Vermillion wrote:
> In article <1173361315.519225.136230@t69g2000cwt.googlegroups.com>,
> Lew Pitcher <lpitcher@sympatico.ca> wrote: 
>
> Using the 'ctime' - inode change time - would be the only way to
> get close to being correct.  The atime shows last access time and
> backup programs often [improperly IMO] update that.  mtime
> would show when the file was last updated, so the ctime [which some
> *i*x documentation improperly documents as 'creation' time] is
> probably the best bet.
>

It depends.

If your backup program (or a virus scanner) updates atime, an
ls -laut
would show that all your files have been read during the last backup.
Then I suggest to take the ctime, too.

If your backup program and virus scanner have an option to reset
the atime to the original version, then choose it!
So you can see by the atime when a file was last opened (for reading
or for writing).
Check that the atime changes on your file system:
ls -lu file
cat file
ls -lu file
Unfortunately, resetting the atime will update the ctime.
To decide about an old document, you can use both atime and mtime,
or best, or combination of the two.

--
Michael Tosch @ hp : com





[ Post a follow-up to this message ]



    Re: Shell Script to Remove Old Files  
Bill Vermillion


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-02-07 06:20 AM

In article <eubvg0$a1o$1@aken.eed.ericsson.se>,
Michael Tosch  <eedmit@NO.eed.SPAM.ericsson.PLS.se> wrote:
>Bill Vermillion wrote: 
>
>It depends.
>
>If your backup program (or a virus scanner) updates atime, an
>ls -laut
>would show that all your files have been read during the last backup.
>Then I suggest to take the ctime, too.

However - others could have accessed that file from the time it was
created - not just a backup program.  Good backup programs don't
diddle the times - the commercial ones I've used in
SCO/Linux/FreeBSD.

>If your backup program and virus scanner have an option to reset
>the atime to the original version, then choose it!

But as above, it could only reset the atime to the last time
someone accessed the file - which could be far from the creation
date.

>So you can see by the atime when a file was last opened (for reading
>or for writing).
>Check that the atime changes on your file system:
>ls -lu file
>cat file
>ls -lu file
>Unfortunately, resetting the atime will update the ctime.
>To decide about an old document, you can use both atime and mtime,
>or best, or combination of the two.

Or move to FreeBSD 6.x and above where the -U flag to 'ls' will
actually show the creation time.  -u shows last access according to
the new 'ls' docs.

Bill

>
>--
>Michael Tosch @ hp : com


--
Bill Vermillion - bv @ wjv . com





[ Post a follow-up to this message ]



    Re: Shell Script to Remove Old Files  
ganadeva@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-04-07 12:17 PM

Hi,
Maybe u can use

find <path> -mtime +(no. of days)|xargs rm -f

or some similar variation over the find command.

regards,
Ganadeva B

On Apr 2, 6:15 am, b...@wjv.com (Bill Vermillion) wrote:
> In article <eubvg0$a1...@aken.eed.ericsson.se>,
> Michael Tosch  <eed...@NO.eed.SPAM.ericsson.PLS.se> wrote:
>
>
> 
> 
> 
> 
> 
> 
>
> However - others could have accessed that file from the time it was
> created - not just a backup program.  Good backup programs don't
> diddle the times - the commercial ones I've used in
> SCO/Linux/FreeBSD.
> 
>
> But as above, it could only reset the atime to the last time
> someone accessed the file - which could be far from the creation
> date.
> 
>
> Or move to FreeBSD 6.x and above where the -U flag to 'ls' will
> actually show the creation time.  -u shows last access according to
> the new 'ls' docs.
>
> Bill
>
>
> 
>
> --
> Bill Vermillion - bv @ wjv . com







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:54 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register