Re: Shell Script to Remove Old Files



On Mar 7, 11:39 pm, "KAKA" <kaka....@xxxxxxxxx> 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

.



Relevant Pages

  • Re: Errors returned by System()function
    ... specified command succesfully and /that/ command ... For example ('echo $?' ... Seems that bash exits with a exit code of 1 when it can't find the ... php$ wget http://www.baddomain.net/index.html ...
    (comp.lang.php)
  • Re: bash (sh?) scripting question
    ... command and verify the exit code: ... contains the result of the echo command. ... for param in $* ...
    (comp.unix.shell)
  • Re: bash (sh?) scripting question
    ... I am trying to implement a type of "try" function, that will execute a command and verify the exit code: ... trap 'echo failed' ERR ... echo "Param: $param" ...
    (comp.unix.shell)
  • Re: Batch file
    ... You can use the "exit" command in a batch file if you ... @echo off ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... more about batch files as you go on, ... echo The time is %time::=.% ... But the piece de resistance was the "start" command, ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)