Requirement is to list the files older than 365 days from multiple directories and delete them and log the list of files which are deleted to a log file.
so 1 script should only list files older than 365 days for each directory separately to a folder
The other script should read these files and delete them and log the list of items which are deleted.
I am attaching the script i have for reference and the listing script and the output of the first script.
It is only listing for first directory not rest of the directories and i don't know how to keep write delete script.
ls -ld /home/ /db/ /db/backup/ /staging/ /staging/codedrops/ /db/*/db2dump/
You do realize that everything that will be in the output from the paths /db/backup/ and /db/*/db2dump/ will also be in the output from the path /db/ and that everything in the output from the path /staging/codedrops/ will also be in the output from the path /staging/ , don't you?
I was expecting to see a diagnostic for a non-existent pathname, or an unreadable or unsearchable directory to explain the error message you reported on the earlier find command. But, if you need separate lists for the files in /db , /home , and /staging maybe it doesn't matter.
What operating system and shell are you using?
See if:
for dir in /db /home /staging
do find "$dir" -type f -mtime +365 > "${dir}_files.txt" &
done
wait
##This script finds, logs and deletes files and folders older than 365 days #####
#/bin/ksh
DIR="/home/, /db/, /staging/"
DIR_LOG_FILE="/staging/retain_for_2years/unix_clean_up_Test/logs/Trail_list_to_be_deleted.log"
TIMESTAMP=`date +%Y:%m:%d`
DAYS_OLD="365" # This will find and delete folders and files older than 365 days.
IFS=$','
echo "....................................." >> $DIR_LOG_FILE
echo "Starting Generation job on : $TIMESTAMP" >> $DIR_LOG_FILE
for dir in /db /home /staging
do
find "$dir" -type f -mtime +$DAYS_OLD > "${dir}_files.txt" &
done
wait
echo "Looking for Files in $DIR" >> $DIR_LOG_FILE
echo "Generated list of Deleted files" >> $DIR_LOG_FILE
unset IFS
echo "Generation of files $NOW completed" >> $DIR_LOG_FILE
============== The Output of the Execution ==================
.....................................
Starting Generation job on : 2015:02:28
.....................................
Starting Generation job on : 2015:02:28
.....................................
Starting Generation job on : 2015:02:28
.....................................
Starting Generation job on : 2015:02:28
Looking for Files in /home/, /db/, /staging/
Generated list of Deleted files
.....................................
Starting Generation job on : 2015:02:28
Looking for Files in /home/, /db/, /staging/
Generated list of Deleted files
Generation of files completed
How did you invoke this script? It appears that you invoked it with something like:
ksh -x scriptname
or:
sh -x scriptname
I see trace output from the -x , but I don't see any errors. What errors do you see?
What output do you get from the command:
ls -l /home_files.txt /db_files.txt /staging_files.txt
If you were hoping that the 2nd line of your script ( #!/bin/ksh ) would determine what shell runs this script, it won't. Specifying the command to be used to interpret a script using #! only works if #! are the 1st two characters on the 1st line in the file, the file containing the script is executable, and the script is invoked by something like:
scriptname
or
./scriptname
Why are you setting and unsetting IFS in your script?
The comment on the first line of your script says it deletes files, but that is a lie; nothing in this script removes any files.