Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-07-2012, 07:06 AM
|
#1
|
LQ Newbie
Registered: May 2012
Posts: 15
Rep: 
|
Attempting to delete files that are older than 90 days, but not directories
Hi everyone;
Sorry if this is another eye roller, I tried going through the manual and wasn't sure, I tried searching around and couldn't find a specific answer to my question, which is THIS:
I want all files that are older than 90 days in this one particular directory and all sub directories to be removed. However, when I ran this command, it also ended up deleting a bunch of sub directories. (I'm not sure why, as no files had gone through them for the past 90 days?)
I don't want directories to be deleted, only files, this is the command I used after doing a bit of searching, I decided it was the best:
find /opt/files/users/stopper/ -mtime +90 -exec rm -rf {} \;
the 'stopper' directory has a ton of other sub directories (well, not anymore!) and as I said, I'd like for it to only delete files and not directories, any assistance would be appreciated.
|
|
|
07-07-2012, 07:09 AM
|
#2
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
According to the manual add -type f to the find options.
jlinkels
|
|
|
07-07-2012, 07:15 AM
|
#3
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 651
Rep: 
|
I can spot 2 errors in your "find" command: - you didn't set "find" to search only for regular files (option -type f);
- you ran a rm -rf on everything "find" found, so all subdirectories older than 90 days got wiped. Still puzzles me why almost everyone issues an "exec rm -rf {}" command while "find" comes itself with a -delete option...
I suggest you try the following:
Code:
find /opt/files/users/stopper/ -type f -mtime +90 -delete
You should remove the delete command first to see which files will be deleted and then, when happy with the result, add it again.
Last edited by 414N; 07-07-2012 at 07:16 AM.
|
|
2 members found this post helpful.
|
07-07-2012, 07:16 AM
|
#4
|
LQ Newbie
Registered: May 2012
Posts: 15
Original Poster
Rep: 
|
Right. so thus:
find /opt/files/users/stopper/ -type f -mtime +90 -exec rm -rf {} \;
-type f is regular file, so it should only find regular files at that point, and not directories, but it'll still search all the directories? I don't have a manual for -rf so I'm not actually sure what they're doing, I just get 'no manual entry for rf'
Thanks for the quick response.
|
|
|
07-07-2012, 07:18 AM
|
#5
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594
Rep: 
|
Code:
find /opt/files/users/stopper/ -type f -mtime +90 -exec rm -rf {} \;
Add -type f for that you only want files.
|
|
|
07-07-2012, 07:21 AM
|
#6
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 651
Rep: 
|
Quote:
Originally Posted by caiphn
I don't have a manual for -rf so I'm not actually sure what they're doing, I just get 'no manual entry for rf'
|
rf are options to the "rm" command.
|
|
1 members found this post helpful.
|
07-07-2012, 07:22 AM
|
#7
|
LQ Newbie
Registered: May 2012
Posts: 15
Original Poster
Rep: 
|
Quote:
Originally Posted by 414N
I can spot 2 errors in your "find" command: - you didn't set "find" to search only for regular files (option -type f);
- you ran a rm -rf on everything "find" found, so all subdirectories older than 90 days got wiped. Still puzzles me why almost everyone issues an "exec rm -rf {}" command while "find" comes itself with a -delete option...
I suggest you try the following:
Code:
find /opt/files/users/stopper/ -type f -mtime +90 -delete
You should remove the delete command first to see which files will be deleted and then, when happy with the result, add it again.
|
Thank-you for the fast response. The reason I used 'exec rm {}' is that several 'how to' websites reference to it. I am a huge newbie, I see the massive list of files, what do I need to add to that so I see a time stamp of the files, similar to a 'ls -ltr'?
|
|
|
07-07-2012, 07:24 AM
|
#8
|
LQ Newbie
Registered: May 2012
Posts: 15
Original Poster
Rep: 
|
Quote:
Originally Posted by 414N
rf are options to the "rm" command.
|
My bad, it's late. Thanks.
|
|
|
07-07-2012, 07:30 AM
|
#9
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 651
Rep: 
|
Quote:
Originally Posted by caiphn
what do I need to add to that so I see a time stamp of the files, similar to a 'ls -ltr'?
|
Well, you could exec "ls -ltr" on every file found instead of "rm -rf"
Code:
find /opt/files/users/stopper/ -type f -mtime +90 -exec ls -ltr {} \;
You may want so save the output to a file using redirection:
Code:
find /opt/files/users/stopper/ -type f -mtime +90 -exec ls -ltr {} \; > filelist.txt
|
|
1 members found this post helpful.
|
07-07-2012, 07:35 AM
|
#10
|
LQ Newbie
Registered: May 2012
Posts: 15
Original Poster
Rep: 
|
Quote:
Originally Posted by 414N
Well, you could exec "ls -ltr" on every file found instead of "rm -rf"
Code:
find /opt/files/users/stopper/ -type f -mtime +90 -exec ls -ltr {} \;
You may want so save the output to a file using redirection:
Code:
find /opt/files/users/stopper/ -type f -mtime +90 -exec ls -ltr {} \; > filelist.txt
|
Thanks a bunch!
|
|
|
07-07-2012, 07:37 AM
|
#11
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 651
Rep: 
|
Please mark the thread "Solved" if the issue is nomore 
|
|
1 members found this post helpful.
|
07-07-2012, 12:27 PM
|
#12
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
About not having the manual for find, do man find. You'll be surprised  . The man command works for virtually all commands.
jlinkels
|
|
1 members found this post helpful.
|
07-07-2012, 07:11 PM
|
#13
|
LQ Newbie
Registered: May 2012
Posts: 15
Original Poster
Rep: 
|
jlinkels: Sorry, I meant I was attempting to manual for 'rm', but I was typing 'rf' instead as it was 5:30AM and my brain was a little fried. Thanks for everyones help.
|
|
|
All times are GMT -5. The time now is 09:33 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|