Linux cleanup script

I run ls -ltr and get  a listing of files in the directory.   What command(s) could I enter to  run the ls -ltr and rm everything with a date in the month of JUL?
xoxomosAsked:
Who is Participating?
 
Gerwin Jansen, EE MVETopic Advisor Commented:
How about this:
touch -d "2012-07-01" /var/tmp/startd
touch -d "2012-08-01" /var/tmp/endd
find . -maxdepth 1 -type f -cnewer /var/tmp/startd ! -cnewer /var/tmp/endd -exec ls -l {} \;

Open in new window

This will show you a list of files created between the 2 dates (startd and endd) in the current directory. If you get the correct list, change "ls -l" to "rm" or "rm -f" if you like.
0
 
eXedonCommented:
Hi xoxomos,

first, you have to focus on files query.
here are the command for getting files with a date in the month of JUL only.
ls -l | grep "Jul"

Open in new window

and verify if the result is correct.

If the result was correct, put the list to rm command.
rm -f $(ls -l | grep "Jul")

Open in new window


But please note, before you execute command, you have to change to source directory first.
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Hi, I don't believe the method above will work because the output of ls -ltr will look like this:

-rwxr--r--  5  user  group 1234 Jul 29 2011 file.txt

Trying to remove the above using rm will fail. Besides the fact that you will also delete files from 2011 if the rm command would work.

Do you just want to remove files from July in this year? And what about directories?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
joolsCommented:
do a man on find and look at the -exec rm {} \; option with one of the -- time options (cant remember which one off the top of my head)
0
 
xoxomosAuthor Commented:
jools is on the right track.  It's some combination of mtime and exec with some of those squiggly characters sort of like find . -mtime +15 -exec rm {}\;
0
 
xoxomosAuthor Commented:
jools solution is what i was looking for:

 find -mtime +30 -exec rm {} \;
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
>>  find -mtime +30 -exec rm {} \;

This command is deleting files in July but it does not delete "everything  with a date in the month of JUL?" It will not delete files from July 1st to July 25th. So I'm afraid you have chosen the incorrect answer for your question. My suggestion is creating 2 'timestamp' files that are used to select files in July, did you try my suggestion at all?
0
 
joolsCommented:
You're welcome to raise this with the moderators if you think its not fair but the asked did say my response was what they were after.
0
 
xoxomosAuthor Commented:
I guess I'm one of those who has to see what he gets before he knows what he wants.
I'll see if the moderator can somehow let me raise the points to 500 and split them.
0
 
xoxomosAuthor Commented:
Guess they will work on this after the weekend

"Experts Exchange  |  AUG 26, 2012  |  04:25AM UTC
Thank you for your request; we will respond as soon as possible. Please reply to this email if you have additional information for this case. Please Note: We are currently experiencing high email volume due to an email bug we encountered during the weekend. If you were affected by this email bug please go here for more information.

This message was sent to xoxomos@gmail.com in reference to Case # #22577."
0
 
joolsCommented:
Oh well, easy come, easy go.

I would have liked to have thought I'd contributed a little to the question but I guess in this case it wasn't to be.
0
 
xoxomosAuthor Commented:
I agree, but sorry it's my bad.  I should have.  Like I said, it's another case of a user having to see what he gets before he can say what he wants:-(
0
 
joolsCommented:
No worries, no one died and it is after all just a question. I think the moderators were fair and I did suggest raising it if anyone wasn't happy.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.