Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
614 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.

828 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