Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
639 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
[X]
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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

610 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