Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rm files by staggered dates

Status
Not open for further replies.

investmentbnker75

Technical User
Aug 22, 2008
3
US
Hi Everyone,

I need to remove files by specific dates. So for example, if i have files from a two month period of time, i want to keep files from the last 14 days and every Sunday beyond that only.


Thanks
 
find using -mtime is the 'easy' answer, but probably wouldn't suit your Sunday requirement. Have you anything so far?

I want to be good, is that not enough?
 
Is it possible the app can write the files with a format that is parseable? That would make the Sunday part easier.
 
[tt][red]find . -mtime +14 -ls[/red] | [green]awk '[blue]$7[/blue] != "Sun" { system("rm " $NF) }'[/green][/tt]

The red part finds files older than 14 days, and produces a long listing of them.
The green part looks days which are not "Sun", then removes them.

The blue $7 is something you'll need to determine from looking at the output of the find command. You just need to count the fields to figure out which field number contains the day.

[tt][red]find . -mtime +14 -ls[/red] | [green]awk '{ print $7 "\n" }'[/green][/tt]
Would be a safe command to experiment with.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Thanks Salem, But that command (find . -mtime +14 -ls | awk '$7 != "Sun" { system("rm " $NF) }') will remove everything older than 14 days. I still need to keep every sunday as well.

The second command you gave shows all zeros
 
Did you check that $7 does indeed correspond to the appropriate part of the ls listing in your particular flavour of *nix? If you get it working I'd also include the /full/path/to/directory rather than the . (period) in the find - safer in my view.

I want to be good, is that not enough?
 
Hmm. Doing a litlle test here points to the absence of a 'day' entry in any of my ls listings!

I want to be good, is that not enough?
 
This is find's version of ls. Maybe try to exec /bin/ls but also see if there is a formatted date option like --time-style or equivalent.
 
Hopefully someone can give some insight to this, id really like to get it working.
 
If your ls command has the --time-style option (usually found on GNU/Linux ls commands) as elgrandeperro mentioned, the problem should be quite easy to solve by adapting Salem's solution. Can you check that? Also let us know exactly what operating system you are using.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top