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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

basing on dir names, find those "older" than 10 days

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

hi,
I have many of dirs named like in example:
31DEC15
01JAN16
...
09JAN16
...

and want to delete those whose name is older than 10 days

I have no gnu sort on a system (no -M), is there any possibility to get reliable solution in such case?
 
Using find with -exec should work

find /path/to/parent/directory/* -type d -ctime +10 -exec rm -rf {} \;






Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
That's not actually deleting based on the "name". That's deleting based on the actual creation date of the directory. While that may be what the OP needs, it's not what he asked for. There may be a requirement for deleting based on the date in the name, and not actual creation date.

 
Hi

Given the lack of GNU [tt]find[/tt], I assume GNU Awk is also on short supply. So I would go with a standard Awk script which filters its input using its parameter :
Code:
[b]BEGIN[/b] [teal]{[/teal]
    [b]split[/b][teal]([/teal][i][green]"JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC"[/green][/i][teal],[/teal] m[teal])[/teal]
    [b]for[/b] [teal]([/teal]i [b]in[/b] m[teal])[/teal]
        m[teal][[/teal]m[teal][[/teal]i[teal]]] =[/teal] i

    ARGC [teal]=[/teal] [purple]0[/purple]
[teal]}[/teal]

[b]sprintf[/b][teal]([/teal][i][green]"%02d%02d%02d[/green][green]"[/green][/i][teal],[/teal] [b]substr[/b][teal]([/teal][navy]$0[/navy][teal],[/teal] [purple]6[/purple][teal],[/teal] [purple]2[/purple][teal]),[/teal] m[teal][[/teal][b]substr[/b][teal]([/teal][navy]$0[/navy][teal],[/teal] [purple]3[/purple][teal],[/teal] [purple]3[/purple][teal])],[/teal] [b]substr[/b][teal]([/teal][navy]$0[/navy][teal],[/teal] [purple]1[/purple][teal],[/teal] [purple]2[/purple][teal])) <[/teal] ARGV[teal][[/teal][purple]1[/purple][teal]][/teal]

Which should be executed as :
Code:
[blue]master #[/blue] ls -1 | awk -f name_date_older_than.awk 160118

The output being the list of filenames older than what was specified as parameter.


Feherke.
feherke.ga
 
And of course the OP is welcome to modify the command line to suit his or her needs .... Thus learning a bit more about shell scripting than being spoon fed an instant solution, it simply needs the addition of a -regex predicate with a fairly simple regular expression.


such as

[0-9]{2}[A-Z]{2}[0-9]{2}

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top