View Single Post

  #1 (permalink)  
Old 05-26-2008, 07:05 PM
cypher82 cypher82 is offline
Junior Member
User
 
Join Date: May 2008
My distro: Aix 5.3
Posts: 1
Rep Power: 0
cypher82 is on a distinguished road
Question Comparing filename-substrings and remove unnecessary files

hi folks...
i have to write a sript that removes unnecessary backup-files.
iam new to shell scripting so please be patient with me. and no its not homework
these files look like "javacore303330.1209029863.txt" where the first number is the PID and the second is the timestamp. so there can be a lot javacores with the same PID but i need only the 2 latest ones.
before:
Code:
javacore479430.1208522264.txt
javacore479430.1208522097.txt
javacore479430.1208521935.txt
javacore479430.1208521772.txt
javacore540754.1209554863.txt
javacore540754.1209551357.txt
javacore540754.1209551130.txt
after:
Code:
javacore479430.1208522264.txt
javacore479430.1208522097.txt
javacore540754.1209554863.txt
javacore540754.1209551357.txt
My basic approach looks like that:
Code:
#comparing every file with every file
#and inc. the counter
for ((  i = 1;  i <= ${number_of_files};  i++  ))
do     for ((  j = 1;   j<= ${number_of_files-1};  j++  ))
         do
             if (pid_file.i = pid_file.j+1)
            and if counter<=2
            do counter++
            else remove_file file.j
my attempt:
Code:
#pid >> list
ls -t|grep javacore |cut -c 9-14  >> /home/user/list.txt
chmod a+x /home/user/list.txt

number_of_files=$(cat "/home/de151738/liste.txt" | wc -w)

#now the tough part
for ((  i = 1;  i <= ${number_of_files};  i++  ))
do     for ((  j = 1;   j<= ${number_of_files-1};  j++  ))
         do
 #?        if {list.line.i = list.line.j}
 #           and if counter<=2 
 #          do   counter++
 #        else rm file.j
If anybody has some advice I would be glad. Maybe my basic approach could be already wrong
Reply With Quote