LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-09-2010, 01:27 PM   #1
rbalaa
Member
 
Registered: Mar 2010
Posts: 61

Rep: Reputation: 0
How to check for number of folders, then delete IF ...


Hello,

I have a daily backup script executing on my server. I would like to be able to delete the "last" backup. for ex the folder that's 30 days old.

Can someone help me find a command that will check for the oldest folder ? Then do an action with that folder. In this case, delete.

Thanks in advance for any help.
 
Old 04-09-2010, 01:53 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
Code:
find ~ -type d -mtime +30 -exec rm -r {};
Find and delete every directory in your home directory that have not been modified in the last 30 days.
Code:
find ~ -type d -mtime +30 -print;
to print such directories

Last edited by troop; 04-09-2010 at 01:54 PM.
 
Old 04-09-2010, 03:09 PM   #3
indiajoe
Member
 
Registered: Jan 2009
Location: India
Distribution: Porteus atma
Posts: 84

Rep: Reputation: 21
Hi
In my bash terminal, when I tried troop's command,
The last semicolon in "rm -r {} ;" command has to be escaped with \ (ie replace with "rm -r {} \;" )
-Cheers
indiajoe

Last edited by indiajoe; 04-09-2010 at 03:11 PM.
 
Old 04-09-2010, 09:19 PM   #4
rbalaa
Member
 
Registered: Mar 2010
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by troop View Post
Code:
find ~ -type d -mtime +30 -exec rm -r {};
Find and delete every directory in your home directory that have not been modified in the last 30 days.
Code:
find ~ -type d -mtime +30 -print;
to print such directories
Thanks for the replies. How can I just -print the directories and not what's inside them. The above command was looking for directories 30 days old inside my parent directories. I know this might sound confusing.

ex:
/MainDir
Dir1
Dir2
Dir3

If im in "MainDir", I only want to print "Dir1", not the directories that are inside it.

Thanks.
 
Old 04-10-2010, 09:17 AM   #5
rbalaa
Member
 
Registered: Mar 2010
Posts: 61

Original Poster
Rep: Reputation: 0
Solved with -maxdepth 1 option.

Thanks.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 04:35 PM
Maximum number of folders in slackware cereal83 Slackware 3 11-23-2005 08:24 PM
Can't delete Folders. duffmckagan SUSE / openSUSE 1 09-19-2005 11:09 AM
generate a big number of folders mannoulla Linux - Software 1 06-14-2004 04:27 PM
Number of Sub Folders jmeneau Red Hat 3 05-19-2004 04:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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