LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-21-2005, 12:48 PM   #1
cboxall
LQ Newbie
 
Registered: Jun 2005
Location: Baltimore, MD
Distribution: Suse Enterprise Server 9.0
Posts: 7

Rep: Reputation: 0
Question Trying to do something wierd with the date command


Thanx to some help from Tink, I am now able to rename files using the date (format /data/openldaptemp/dump-yyyymmdd.ldif). However, I want to delete files older than seven days old based on the date stored in the file name. I don't want to use the date attributes of the files for a variety of reasons. Thus, this does NOT work for me -

find -name "dump*" -ctime +7 -exec rm {} \;

What I tried to do was something along these lines -

lookback=8
while [ -e /data/openldaptemp/dump-$(date --date='${lookback} days ago' +%Y%m%d).ldif ]
do
rm /data/openldaptemp/dump-$(date --date='${lookback} days ago' +%Y%m%d).ldif
lookback=$lookback+1
done

However, the date command doesn't allow variables (or I can't figure out how to get it to allow variables) in the --date='' option. How should I do this?



Colin

 
Old 07-22-2005, 07:55 AM   #2
ewt3y
Member
 
Registered: May 2005
Location: hanoi vietnam
Distribution: mandriva
Posts: 106

Rep: Reputation: 15
Please tell me what is tink.
What do not work for you ? Is it the command: find -name "dump*" -ctime +7 -exex rm {} \ ?
 
Old 07-22-2005, 10:38 AM   #3
cboxall
LQ Newbie
 
Registered: Jun 2005
Location: Baltimore, MD
Distribution: Suse Enterprise Server 9.0
Posts: 7

Original Poster
Rep: Reputation: 0
Post Tink and the trouble I'm having

First, Tink, or Tinker, is another member of LinuxQuestions.org that helped me get started using the date commmand in the first place.

As for what doesn't work, the solution Tink gave (find -name "dump*" -ctime +7 -exec rm {} \ relies on the date attribute of the files, which I do NOT want to use. I want to find files based on the date written in the file name. Is there someway to do this?
 
Old 07-22-2005, 05:24 PM   #4
hlyrad
Member
 
Registered: Jul 2005
Location: Ab Ca
Distribution: Redhat EL Sun Mac OSX FC 3.0 & 4.0
Posts: 44

Rep: Reputation: 15
You will need to use a scripting language like perl.
Basically you will readdir for files.
Split file names by "-" dump-yyyymmdd.ldif
again with "." so you end up with yyyymmdd referencing the dump-yyyymmdd.ldif

sorta like this

#!/usr/bin/perl


my ($source, @dir, $dir, $file, $inc);
my $div = "/";

my $date = `date +%Y%m%d`;
chomp($date);
my $source = shift; #Accepts search directory as ARG from command line
opendir(SD, "$source") or die ($!);
my @dir = grep { ! /^\./ or !-d} readdir SD; #filter out currentdirectory, 'dot' files and subdirectories
$inc = @dir;
exit(0) if $inc < 6; #If there are less than 7 files in this directory just exit
closedir(SD);
foreach $dir(@dir)
{
my $fname = $source.$div.$dir;
if ($dir =~ /\.ldif$/)
{
my @sep1 = split("-", $dir);
my @sep2 = split(/\./, $sep1[1]);
my $fdate = $sep2[0]; #Holds the numeric portion of file name;
if ($date - $fdate >= 7)
{
unlink($fname) or die ($!)
}
}
}

This works except when the month end rolls around.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
'date' command format Neorio Linux - General 3 01-18-2011 05:18 AM
Trying to do something wierd with the date command cboxall Linux - Newbie 2 07-22-2005 04:45 PM
date command aetucker1 Linux - General 3 01-21-2005 12:57 PM
date command and the use of %Z tag hq4ever Linux - Newbie 2 09-04-2004 12:15 PM
help with Date command in script icw_sec Programming 6 01-06-2004 11:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration