LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 07-07-2012, 07:06 AM   #1
caiphn
LQ Newbie
 
Registered: May 2012
Posts: 15

Rep: Reputation: Disabled
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.
 
Old 07-07-2012, 07:09 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
According to the manual add -type f to the find options.

jlinkels
 
Old 07-07-2012, 07:15 AM   #3
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
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.
Old 07-07-2012, 07:16 AM   #4
caiphn
LQ Newbie
 
Registered: May 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-07-2012, 07:18 AM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Code:
find /opt/files/users/stopper/ -type f -mtime +90 -exec rm -rf {} \;
Add -type f for that you only want files.
 
Old 07-07-2012, 07:21 AM   #6
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by caiphn View Post
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.
Old 07-07-2012, 07:22 AM   #7
caiphn
LQ Newbie
 
Registered: May 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by 414N View Post
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'?
 
Old 07-07-2012, 07:24 AM   #8
caiphn
LQ Newbie
 
Registered: May 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by 414N View Post
rf are options to the "rm" command.
My bad, it's late. Thanks.
 
Old 07-07-2012, 07:30 AM   #9
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by caiphn View Post
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.
Old 07-07-2012, 07:35 AM   #10
caiphn
LQ Newbie
 
Registered: May 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by 414N View Post
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!
 
Old 07-07-2012, 07:37 AM   #11
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Please mark the thread "Solved" if the issue is nomore
 
1 members found this post helpful.
Old 07-07-2012, 12:27 PM   #12
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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.
Old 07-07-2012, 07:11 PM   #13
caiphn
LQ Newbie
 
Registered: May 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
delete files when it's older 30 days Madison00 Linux - Newbie 8 01-07-2011 02:07 AM
[SOLVED] Delete old files older than 7 days anon091 Linux - Newbie 3 09-18-2009 01:15 PM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
Delete files older then 30 days stefaandk *BSD 1 01-07-2008 08:31 PM
delete files older than 30 days using cronjob latheesan Linux - Newbie 5 06-14-2005 02:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:57 AM.

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