?
Solved

Linux cleanup script

Posted on 2012-08-23
15
Medium Priority
?
499 Views
Last Modified: 2012-08-28
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?
0
Comment
Question by:xoxomos
  • 5
  • 4
  • 3
  • +1
13 Comments
 

Expert Comment

by:eXedon
ID: 38328292
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
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38328547
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
 
LVL 19

Expert Comment

by:jools
ID: 38329638
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 1000 total points
ID: 38331266
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
 

Author Comment

by:xoxomos
ID: 38331393
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
 

Author Comment

by:xoxomos
ID: 38331504
jools solution is what i was looking for:

 find -mtime +30 -exec rm {} \;
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38332348
>>  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
 
LVL 19

Expert Comment

by:jools
ID: 38332482
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
 

Author Comment

by:xoxomos
ID: 38333537
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
 

Author Comment

by:xoxomos
ID: 38334965
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
 
LVL 19

Expert Comment

by:jools
ID: 38338990
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
 

Author Comment

by:xoxomos
ID: 38339020
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
 
LVL 19

Expert Comment

by:jools
ID: 38339824
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question