Solved

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

Posted on 2009-07-06
11
606 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:kvjajoo
  • 5
  • 5
11 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24785983
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
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24790883
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
 
LVL 1

Author Comment

by:kvjajoo
ID: 24791686
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
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24791947
>>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
 
LVL 1

Author Comment

by:kvjajoo
ID: 24792414
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Expert Comment

by:ghostdog74
ID: 24794066
use nawk on solaris
0
 
LVL 1

Author Comment

by:kvjajoo
ID: 24794628
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
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24799917
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
 
LVL 1

Author Comment

by:kvjajoo
ID: 24800620
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
 
LVL 9

Accepted Solution

by:
ghostdog74 earned 125 total points
ID: 24800762
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
 
LVL 1

Author Closing Comment

by:kvjajoo
ID: 31601022
Thanks ghostdog74... it finally worked... thanks for you kind support
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
exchange 2007 3 49
changeXy challenge 13 58
noX challenge 17 76
Sed question 2 49
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now