Need Help in ksh Script to list files older than 365 days from specified directories

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.

What didn't work with what was suggested in your other thread?

Why do you need a separate file for each directory? Why not just include full pathnames of all of the files you want to delete in one file?

Why do you need two scripts when you can create the list and remove the files in a single find command?

1 Like

We need separate log file for each directory is - these logs files will be reviewed and if no file is needed we will be removing it.

The suggesting by clx in my other tread is giving below error

+ find  /home/ /db/ /db/backup/ /staging/ /staging/codedrops/ /db/*/db2dump/ -type f -mtime +365
+ 1>> /staging/retain_for_2years/unix_clean_up_Test/logs/list_to_be_deleted.log
find: 0652-010 The starting directory is not valid

We need a time to review the generated logs - 2 or 3 days so we want to have a delete script sepeartely

---------- Post updated 02-28-15 at 08:04 AM ---------- Previous update was 02-27-15 at 04:45 PM ----------

Please kindly help me out

What is the output from the command:

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?

Thanks very much Don for replying.

The below is the output

drwxrwxrwx   31 root     db2cadm        4096 Feb 27 02:21 /db/
drwxrwxrwx   26 root     system        12288 Feb 19 04:27 /db/backup/
drwxr-xr-x    6 srpsdba1 db2cadm        4096 Dec 22 01:08 /db/srpsdba1/db2dump/
drwxrwxr-x    2 srpsdba1 db2cadm         256 Mar 26 2014  /db/srpsdba1_2/db2dump/
drwxrwxrwx    6 srpsdba2 db2cadm        4096 Jan 13 22:00 /db/srpsdba2/db2dump/
drwxr-xr-x    4 srpsdba7 db2cadm        4096 Dec 18 15:12 /db/srpsdba7/db2dump/
drwxrwxrwx    4 srpsdbc  db2cadm        4096 Feb 23 03:03 /db/srpsdbc/db2dump/
drwxrwxrwx    4 srpsh1   db2cadm        8192 Feb 17 05:57 /db/srpsh1/db2dump/
drwxr-xr-x    2 srpssq1  db2cadm         256 Mar 12 2014  /db/srpsq1/db2dump/
drwxrwxrwx    4 srpssd3  db2cadm        4096 Feb 23 09:20 /db/srpssd3/db2dump/
drwxrwxrwx    4 srpssd5  db2cadm        4096 Feb 27 14:41 /db/srpssd5/db2dump/
drwxr-xr-x    2 srpsh1   db2cadm         256 Feb 27 2014  /db/srpssh1/db2dump/
drwxrwxrwx    4 srpssq1  db2cadm        8192 Feb 26 11:46 /db/srpssq1/db2dump/
drwxrwxrwx    5 srpssq2  db2cadm        8192 Feb 26 03:35 /db/srpssq2/db2dump/
drwxr-xr-x  397 bin      bin           16384 Jan 15 11:06 /home/
drwxrwxrwx   16 root     system         4096 Jan 12 17:34 /staging/
drwxrwxrwx   51 dba_srps ezcssgrp       8192 Feb 27 12:24 /staging/codedrops/

Yes you are correct if the script list's the older files from the sub directories also then only /home/ /db/ /staging/ should be fine.

Can you please kindly help me out.

Thanks once again

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

works for you.

Don Sir,

I am getting this error when executing your code .

+ DIR=/home/, /db/, /staging/
+ DIR_LOG_FILE=/staging/retain_for_2years/unix_clean_up_Test/logs/Trail_list_to_be_deleted.log
+ + date +%Y:%m:%d
TIMESTAMP=2015:02:28
+ DAYS_OLD=365
+ IFS=$,
+ echo .....................................
+ 1>> /staging/retain_for_2years/unix_clean_up_Test/logs/Trail_list_to_be_deleted.log
+ echo Starting Generation job on : 2015:02:28
+ 1>> /staging/retain_for_2years/unix_clean_up_Test/logs/Trail_list_to_be_deleted.log
+ find /db -type f -mtime +365
+ + wait
+ find /home -type f -mtime +365
+ + find /staging -type f -mtime +365
+ 1> /db_files.txt
1> /staging_files.txt
1> /home_files.txt
+ echo Looking for Files in /home/, /db/, /staging/
+ 1>> /staging/retain_for_2years/unix_clean_up_Test/logs/Trail_list_to_be_deleted.log
+ echo Generated list of Deleted files
+ 1>> /staging/retain_for_2years/unix_clean_up_Test/logs/Trail_list_to_be_deleted.log
+ unset IFS
+ echo Generation of files  completed
+ 1>> /staging/retain_for_2years/unix_clean_up_Test/logs/Trail_list_to_be_deleted.log

=======================The Script Executed ================

##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

=======================================

OS Aix 7.1.0.0 and

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.