Find file older than given number in bash (every file contains 6 digit number in the file name)

Hi,

I have lot of files which contains a 6 digit number in file name.. for example:

abc.080701.gz
abc.080702.gz
abc.080703.gz
abc.080704.gz
abc.080705.gz
abc.080706.gz
abc.080707.gz
abc.080708.gz
abc.080709.gz
abc.080710.gz
abc.080711.gz

and so on....

Basically the number is generated using the "date +%y%m%d" and added to the file name via a shell script. Since this script runs daily, every day a new file is generated.

Now i want a shell script to find files older than the 6 digit number provide by me to the script and delete them. For example if i give 081201 as a input to script then the script should look into all filenames in that directory, find files which has older 6 digit number than 081201 in filename and then delete those files...

Important Info:

OS :- Solaris 9 Sparc Version
Shell :- bash 2.05
LVL 1
kvjajooAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
Hi,
instead of comparing each filename you could use a feature of 'find' for your purpose.
The trick is to touch a reference file, so that it gets the desired timestamp, and then delete files older than that file.
It could look like this -

#!/bin/ksh
myref=$1     # a 6 digit numer "yymmdd"
myref_file=/tmp/abc.ref.$$
mydir=/my/dir/to_cleanup
touch -t "${myref}0000" $myref_file
find $mydir -type f -name abc\*.gz ! -newer $myref_file | xargs rm
rm $myref_file
exit
wmp
0
ghostdog74Commented:
no need to create extra files


printf "Enter your input: "
read input
ls *gz | awk -v userinput="$input" 'BEGIN{FS="."; }
 $2+0<userinput+0 {
  cmd="rm "$0
  print cmd
  #system(cmd) #uncomment to use
}' 

Open in new window

0
kvjajooAuthor Commented:
Hi woolmilkpro and ghostdog74... Frist of all sorry for my short description of the situation.. I should have given more details...I want to clear few things which give u guys more insight to solve my problem...

1. I dont want any user input in the script...I will provide the 6 digit number to the script on static basis.
2. Creation and modification time stamps of the files are not going to help because the due to transfer of this files from one server to another the timestamps of the files are not accurate..The only reliable information is to use the 6 digit number in the file name.

So can you guys please modify ur solution according to this above two conditions...  
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

ghostdog74Commented:
>>So can you guys please modify ur solution according to this above two conditions...
i am giving you a possible solution , the rest is up to you to learn and apply. so far, i have not seen any effort in your part..
0
kvjajooAuthor Commented:
hi ghostdog74..

Well i just asked you guys for the solution because i am a total newbee in bash scripting... Also i executed the codes you gave me via a bash script and it gaved me following error :-

bash-2.05# ./test2.sh
Enter your input: 080710
awk: syntax error near line 1
awk: bailing out near line 1

Following is the script i wrote:

bash-2.05# vi test2.sh
"test2.sh" 10 lines, 185 characters
#!/bin/bash

printf "Enter your input: "
read input
ls * | awk -v userinput="$input" 'BEGIN{FS="."; }
 $2+0<userinput+0 {
  cmd="rm "$0
  print cmd
  #system(cmd) #uncomment to use
}'

Please help...
0
ghostdog74Commented:
use nawk on solaris
0
kvjajooAuthor Commented:
Hi ghostdog74,

Sorry to trouble u again.. I used nawk as u said... but the script then deleted all the files with extension gz in that particular directory...

Following are the codes i used...

#!/bin/bash

printf "Enter your input: "
read input
ls *gz | nawk -v userinput="$input" 'BEGIN{FS="."; }
 $2+0<userinput+0 {
  cmd="rm "$0
  print cmd
  #system(cmd) #uncomment to use
}'

Please tell me what am i doing wrong here...
0
ghostdog74Commented:
no, it doesn't delete anything, since there is a # in front of the system(cmd) statement. If you uncomment that, it will delete files. I left the print cmd statement there so that you can see what's going to be deleted before you uncomment the system(cmd) line. Are you sure you are running your script correctly.
0
kvjajooAuthor Commented:
I agree ghostdog74... It only prints whats going to be deleted... but the printcmd command output itself printed the list of all the files with gz extension in following manner...(my input was 080710)

rm abc.080701.gz
rm abc.080702.gz
rm abc.080703.gz
rm abc.080704.gz
.
.
.
.
rm abc.090701.gz
rm abc.090702.gz

(the empty lines above represents all the files from 080705 to 090630)
0
ghostdog74Commented:
i can show you my output, and it does work for me.
here the input is 080705 and as you can see , it does output those files less than 080705.
If all else fails, you can try changing  $2+0<userinput+0 to

int($2) < int(userinput)

and see how it goes


# ls -1
abc.080701.gz
abc.080702.gz
abc.080703.gz
abc.080704.gz
abc.080705.gz
abc.080706.gz
abc.080707.gz
abc.080708.gz
abc.080709.gz
abc.080710.gz
abc.080711.gz
# ../test.sh
Enter your input: 080705
rm abc.080701.gz
rm abc.080702.gz
rm abc.080703.gz
rm abc.080704.gz

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kvjajooAuthor Commented:
Thanks ghostdog74... it finally worked... thanks for you kind support
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.