Find all files other than first two files dates & last file date for month


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Find all files other than first two files dates & last file date for month
# 15  
Old 04-14-2014
Code:
XXXDIR=/abc/def/ghi
#not months first two & last one
function file_purge
{
    tradecheck=`pwd`
 echo "Files to purge...."
    $1| while read dm_file
    do   
  day=`perl -MPOSIX -le 'print strftime "%d", localtime((lstat)[9]) for @ARGV' "$dm_file"`
  mon=`perl -MPOSIX -le 'print strftime "%b", localtime((lstat)[9]) for @ARGV' "$dm_file"`
        year=`perl -MPOSIX -le 'print strftime "%Y ", localtime((lstat)[9]) for @ARGV' "$dm_file"`
          dday=`expr $day + 0`
    
    cntr=0
    cntr2=0
    flag1=0
    flag2=0
    flag3=0
        #This is list of files for same month & year of $dm_file for more than 180 days old
        ls -ltr * |nawk -v mon=$mon -v year=$year '{if ($8 == year && $6 == mon) {print $9}}'| while read inner_file
         do
            imfile=`find $tradecheck -name $inner_file`
            i_day=`perl -MPOSIX -le 'print strftime "%d", localtime((lstat)[9]) for @ARGV' "$imfile"`
            i_dday=`expr $i_day + 0`
            purge_first "$dday" "$i_dday"
            purge_last "$dday" "$i_dday"
         done
         
        if [ $flag1 -eq 0 ] && [ $flag2 -eq 0 ] && [ $flag3 -eq 0 ]; then
         echo $dm_file $day $mon $year    
        fi
  
 done
}
#For confirming file not the last file's date
function purge_last
{
if [ $1 -ge $2 ]; then
 flag3=1
else
 flag3=0
 break    
fi
}
#For confirming file not the first two file's date
function purge_first
{
if [ $1 -le $2 ]; then
 flag1=1
else
   
 if [ $cntr2 -eq 0 ]; then
  check_day=$2
  cntr2=`expr $cntr2 + 1`
 fi
 
 if [ ! $check_day -eq $2 ]; then
  cntr=`expr $cntr + 1`
 fi
fi
if [ $cntr -eq 0 ]; then
 flag2=1
else
 flag1=0
 flag2=0
fi
}
  
# purge the files which are 180 days older
cd $XXXDIR
var3="find . ! -name -prune -type f -mtime +180"
file_purge "$var3"

---------- Post updated at 10:43 AM ---------- Previous update was at 10:42 AM ----------

i want to find files for 6 months old.hence passing that to function file_purge

---------- Post updated at 10:55 AM ---------- Previous update was at 10:43 AM ----------

Code:
XXXDIR=/abc/def/ghi
#not months first two & last one
function file_purge
{
    tradecheck=`pwd`
 echo "Files to purge...."
    $1| while read dm_file
    do   
  day=`perl -MPOSIX -le 'print strftime "%d", localtime((lstat)[9]) for @ARGV' "$dm_file"`
  mon=`perl -MPOSIX -le 'print strftime "%b", localtime((lstat)[9]) for @ARGV' "$dm_file"`
        year=`perl -MPOSIX -le 'print strftime "%Y ", localtime((lstat)[9]) for @ARGV' "$dm_file"`
          dday=`expr $day + 0`
    
    cntr=0
    cntr2=0
    flag1=0
    flag2=0
    flag3=0
        #This is list of files for same month & year of $dm_file for more than 180 days old
        ls -ltr * |nawk -v mon=$mon -v year=$year '{if ($8 == year && $6 == mon) {print $9}}'| while read inner_file
         do
            imfile=`find $tradecheck -name $inner_file`
            i_day=`perl -MPOSIX -le 'print strftime "%d", localtime((lstat)[9]) for @ARGV' "$imfile"`
            i_dday=`expr $i_day + 0`
            purge_first "$dday" "$i_dday"
            purge_last "$dday" "$i_dday"
         done
         
        if [ $flag1 -eq 0 ] && [ $flag2 -eq 0 ] && [ $flag3 -eq 0 ]; then
         echo $dm_file $day $mon $year    
        fi
  
 done
}
#For confirming file not the last file's date
function purge_last
{
if [ $1 -ge $2 ]; then
 flag3=1
else
 flag3=0
 break    
fi
}
#For confirming file not the first two file's date
function purge_first
{
if [ $1 -le $2 ]; then
 flag1=1
else
   
 if [ $cntr2 -eq 0 ]; then
  check_day=$2
  cntr2=`expr $cntr2 + 1`
 fi
 
 if [ ! $check_day -eq $2 ]; then
  cntr=`expr $cntr + 1`
 fi
fi
if [ $cntr -eq 0 ]; then
 flag2=1
else
 flag1=0
 flag2=0
fi
}
  
# purge the files which are 180 days older
cd $XXXDIR
var3="find . ! -name -prune -type f -mtime +180"
file_purge "$var3"

# 16  
Old 04-14-2014
why dont you start with eliminating the old files more than 180 days?
I understand after all files to be processed will be less than 3 months, is that so? ( makes a change on posiible 12 months x X years...)
# 17  
Old 04-15-2014
I dont want to delete all files > 180Requirement is thispurge files > 180 days and file date should not be first two files dates & last file date for month
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find all files other than last two dates per month and year?

Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg 1596 Mar 22 2012 c.txt -rwxr-xr-x 1 user userg 1596 Mar 24 2012 d.txt... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

2. Shell Programming and Scripting

How to find all files other than first two dates & last date per month and year?

how to find all files other than first two dates & last date per month and year Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg ... (6 Replies)
Discussion started by: Makarand Dodmis
6 Replies

3. Shell Programming and Scripting

How to find all files for same month and year?

Hi All, I find all files for same month and year lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 c.txt -rwxr-xr-x 1 user userg 1596 Mar 21 2012 d.txt -rwxr-xr-x 1 user userg 1596 Mar 22 2012 f.txt -rwxr-xr-x 1... (8 Replies)
Discussion started by: Makarand Dodmis
8 Replies

4. UNIX for Advanced & Expert Users

How to find last two files for a month?

Hi All, I need to find last two files for the month. lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 15:43 c.txt -rwxr-xr-x 1 user userg 1596 Mar 21 15:43 d.txt -rwxr-xr-x 1 user userg 1596 Mar 22 15:43 f.txt -rwxr-xr-x 1... (14 Replies)
Discussion started by: Makarand Dodmis
14 Replies

5. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

6. UNIX for Dummies Questions & Answers

Reading the dates from a file & moving the files from a directory

Hi All, I am coding for a requirement where I need to read a file & get the values of SUB_DATE. Once the dates are found, i need to move the files based on these dates from one directory to another. ie, this is how it will be in the file, SUB_DATE = 20120608,20120607,20120606,20120606... (5 Replies)
Discussion started by: dsfreddie
5 Replies

7. Shell Programming and Scripting

How to find the create time of a file if current date is in next month

Hi All, I want to find the time diffrence between currnt time and "abc.txt" file create time. I have solve that but if the abc.txt file created last month then is there any process to find the difftent? Exp: Create time of abc.txt is "Apr 14 06:48" and currect date is "May 17 23:47".... (1 Reply)
Discussion started by: priyankak
1 Replies

8. UNIX for Advanced & Expert Users

find files modified in a specific month

hello i need a way to list files modified in a specific month and move them to a specific directry , i mean somthing like : find . -modifiedtime "May" -print -exec /usr/bin/mv newdirectory thank u (1 Reply)
Discussion started by: omer_ome
1 Replies

9. Solaris

find files modified in a specific month

hello i need a way to list files modified in a specific month and move them to a specific directry , i mean somthing like : find . -modifiedtime "May" -print -exec /usr/bin/mv newdirectory thank u (1 Reply)
Discussion started by: omer_ome
1 Replies

10. Shell Programming and Scripting

Find all files by month

Hi, I am trying to do achieving of files by months. find /test -name \*.* -mtime +30 will give me the result of all modified files after 30 days. But lets say i want to list all files that is modified in last months... what is the command to do it? Thanks! (13 Replies)
Discussion started by: maldini
13 Replies
Login or Register to Ask a Question