LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2014, 05:54 PM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 93

Rep: Reputation: 0
Question How to find and delete all files with specific name


Hey Guys

We have files with the following format

Quote:
name1.20130101-0000323.gz
name2.20130501-12300012.gz
...
name3.20140503-12322.gz
Basically a filename.date-number.gz

We want to create a script to delete all files with date more than a year old.. Was trying this..

Code:
find /file/location/ -type f ! \( -iname "*$(date -d -1year +'%Y%m*')" \) -exec echo {} \;
But is not coming out right.. Something to do with trying to capture * on filename before the date..

Can someone please help out and tell me what will be the correct syntax?

Cheers

Last edited by khandu; 06-16-2014 at 06:54 PM.
 
Old 06-16-2014, 06:19 PM   #2
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
So basically want to delete all files with datestamp older than 1 year from today..

the -mtime won't work in this case.. have to capture via the filename..
 
Old 06-16-2014, 06:40 PM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
What do you mean -mtime won't work? I don't understand what you mean capture via the filename. Typically the find command would look like this...

Code:
#test
find /location -type f -mtime +365
#remove the files
find /location -type f -mtime +365 -exec rm {} \;
Did something happen to the files where you can't use the timestamp? If you need to do it based off of the date in the filename then you would need to use a more advanced loop like the following... Note, this assumes *all* files end with [0-9]{8}.gz.

Code:
find . -type f -name '*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz' | while read x;do
filedate="$(echo $x | sed 's/.*\.\([0-9]\{8\}\).gz/\1/')"
if [ "$(date -d "$filedate" +%s)" -lt "$(date -d -1year +%s)" ];then
  echo "$x is older than 1 year from now.  Created on $(date -d "$filedate")"
fi
done
It's best to explicitly state the format you expect the files to be otherwise this method would likely fail. You should check the date in the file name against the output of the date -d "$filedate" command. On my system, date command automatically interprets the date format to be YYYYMMDD.

Last edited by sag47; 06-16-2014 at 06:47 PM.
 
Old 06-16-2014, 06:50 PM   #4
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sag47 View Post
What do you mean -mtime won't work? I don't understand what you mean capture via the filename. Typically the find command would look like this...

Code:
#test
find /location -type f -mtime +365
#remove the files
find /location -type f -mtime +365 -exec rm {} \;
Did something happen to the files where you can't use the timestamp? If you need to do it based off of the date in the filename then you would need to use a more advanced loop like the following... Note, this assumes *all* files end with [0-9]{8}.gz.

Code:
find . -type f -name '*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz' | while read x;do
filedate="$(echo $x | sed 's/.*\.\([0-9]\{8\}\).gz/\1/')"
if [ "$(date -d "$filedate" +%s)" -lt "$(date -d -1year +%s)" ];then
  echo "$x is older than 1 year from now.  Created on $(date -d "$filedate")"
fi
done
It's best to explicitly state the format you expect the files to be otherwise this method would likely fail. You should check the date in the file name against the output of the date -d "$filedate" command. On my system, date command automatically interprets the date format to be YYYYMMDD.
Yeah the modify timestamp is screwed up

actually format is this (modified first post).. sorry had given the wrong format earlier

Quote:
filename.date-*.gz
the "date" is in format "YYYYMMDD" and that is what we want to act on to delete all files older than one year

Last edited by khandu; 06-16-2014 at 06:55 PM.
 
Old 06-16-2014, 07:18 PM   #5
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
I am trying this

Quote:
filedate="$(echo $x | sed 's/.*\.\([0-9]{8}\)-.*.gz/\1/')"
echo $filedate
But the result is spitting out the whole filename instead of just the group captured..

So

name1.20130101-0000323.gz

instead of

20130101
 
Old 06-16-2014, 10:09 PM   #6
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
I am trying to do this but the array is not working.. any ideas

Quote:
#!/bin/bash
find /files/ -type f -name '*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-*.gz' | while read x;
do
filedate="$(echo $x | grep -Eo '[0-9]{8}')"
if [ "$filedate" -lt "$(date -d -1year +%Y%m%d)" ];then
array1=$x

fi
done
echo ${#array1[@]}
The result is coming 0
 
Old 06-16-2014, 10:30 PM   #7
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
I realise that it is due to the | symbol opening a subshell..

But basically want to store all results in a array which then I can
1) email via mailx command in one email like a report (if i currently will put it in loop it will send 100 emails individually for each find)and
2) delete using -exec rm command
 
Old 06-16-2014, 11:02 PM   #8
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Perhaps, you should step back from the problem and figure out what it is you actually want. Currently your last post is entirely different from your original request. Please organize your thoughts and properly express what you're trying to do from start to finish.

The original answer I gave you seemed sufficient in that it could have been lightly modified by you to accomplish removing files older than a year when the date timestamp is part of the file name. One could update the expression or change how they're obtaining the date from the file name with a small modification. If that's not what you want then see my former statement in this post.

If you need to email the output before removing then don't mess with an array. Simply pipe stdout from the while loop in my original posted program to stdin of the mail program of your choosing.

Last edited by sag47; 06-16-2014 at 11:09 PM.
 
Old 06-16-2014, 11:07 PM   #9
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Sorry.. Will try to explain again..

Have a directory with the following file structure

Quote:
name1.20130101-0000323.gz
name2.20130501-12300012.gz
...
name3.20140503-12322.gz
FORMAT: filename.DATE-TIME.gz

Also modified timestamp is not proper for these files so cannot rely on that

I want to

1) Find all the files for the DATE more than a year old from today

2) Delete all those found files

3) Send a report email of all the files matched above and deleted.
 
Old 06-16-2014, 11:27 PM   #10
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by khandu View Post
FORMAT: filename.DATE-TIME.gz
Your format for time does not make sense and is not a standard time format. Since you only care about things older than a year then the day should be precise enough (i.e. we don't care about the time).

Quote:
Originally Posted by khandu View Post
Also modified timestamp is not proper for these files so cannot rely on that
Understood.

Quote:
Originally Posted by khandu View Post
I want to

1) Find all the files for the DATE more than a year old from today

2) Delete all those found files

3) Send a report email of all the files matched above and deleted.
Okay then you can lightly modify my original program. You should break it down into two steps. The first is a for loop which will mail you what *would* be deleted. Then execute the same program slightly modified to actually delete it. Here is my last post pretty much literal. Considering you don't seem to understand the original program I wrote I highly recommend against you running any code on your system until you fully understand what it does. Use at your own risk.

Here's what I talked about breaking it into two steps.

Code:
find . -type f -name '*.gz' | while read x;do
filedate="$(echo $x | sed 's/.*\.\([0-9]\{8\}\)-[0-9]\+.gz/\1/')"
if [ "$(date -d "$filedate" +%s)" -lt "$(date -d -1year +%s)" ];then
  echo "$x is older than 1 year from now.  Created on $(date -d "$filedate")"
fi
done | mail -s "files to be deleted" user@email.example
Code:
find . -type f -name '*.gz' | while read x;do
filedate="$(echo $x | sed 's/.*\.\([0-9]\{8\}\)-[0-9]\+.gz/\1/')"
if [ "$(date -d "$filedate" +%s)" -lt "$(date -d -1year +%s)" ];then
  \rm "$x"
fi
done
You should learn bash, read the date man page to understand what I'm doing, and backup any files before you attempt this yourself.

Last edited by sag47; 06-17-2014 at 05:07 PM.
 
1 members found this post helpful.
Old 06-16-2014, 11:52 PM   #11
khandu
Member
 
Registered: Sep 2003
Posts: 93

Original Poster
Rep: Reputation: 0
Thanks

I did understand your first code.. i just didn't realise that i can pipe the mail after the done..

I have a similar script running for another use where I am doing two step (one for mail and one for actual delete) but the difference was that it was in a single line command and thus mailx command was working at the end of it.. this had loop and therefore I didn't realize the mailx had to be outside the done with a |

I am testing it in dev..

thanks
 
  


Reply

Tags
date, delete, find, shell scripting



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
[SOLVED] List specific files and delete results niiiro Programming 2 07-08-2011 09:08 AM
How to delete all files with specific word in filename? moviecarpet Programming 13 05-26-2011 08:44 AM
[SOLVED] Recursive delete specific files from sub-directories. guriinii Linux - Newbie 11 03-07-2011 10:41 AM
How to delete files tat contains a specific word?? linuq Linux - Newbie 4 06-30-2008 12:12 AM
ow do I delete all files which were created on a specific date marsguy Linux - General 9 08-21-2007 08:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:05 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