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? :scratch: Colin :newbie: |
Please tell me what is tink.
What do not work for you ? Is it the command: find -name "dump*" -ctime +7 -exex rm {} \ ? |
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? |
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. |
All times are GMT -5. The time now is 04:52 PM. |