Removing empty folders using the "find" command | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Removing empty folders using the "find" command

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-25-2008
biot biot is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 31 March 2008, 8:30 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Removing empty folders using the "find" command

Hi
I'm trying to remove empty sub-folders from 1 main folder using the find method, but the "- empty" parameter isn't recognized by my Unix version.
Any idea how to implement such thing?
Thanks.
Sponsored Links
    #2  
Old 03-25-2008
krishmaths krishmaths is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 30 June 2014, 4:32 AM EDT
Location: Chennai, India
Posts: 443
Thanks: 22
Thanked 70 Times in 68 Posts
rmdir removes only empty dirs

You can use the below command


Code:
find ./ -type d -exec rmdir 2>/dev/null {} \;

The above will remove all empty directories from the current directory.
Sponsored Links
    #3  
Old 03-25-2008
ShawnMilo ShawnMilo is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 10 November 2009, 8:27 AM EST
Posts: 252
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
Originally Posted by krishmaths View Post
You can use the below command


Code:
find ./ -type d -exec rmdir 2>/dev/null {} \;

The above will remove all empty directories from the current directory.
To clarify this, the rmdir command should fail if the directory isn't empty. So by ignoring error messages (redirecting them to a black hole, /dev/null), you will successfully delete the empty ones and the non-empty ones shouldn't be in any danger.

It would probably be a good idea to put dummy data into a directory and try to use rmdir on it, just to make sure your environment behaves this way. But it's the default, so it should be fine.

ShawnMilo
    #4  
Old 03-25-2008
biot biot is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 31 March 2008, 8:30 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you both for the solution & clarification.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block lifechamp UNIX for Dummies Questions & Answers 0 03-12-2012 11:01 AM
"find command" to find the files in the current directories but not in the "subdir" swamymns Shell Programming and Scripting 9 07-22-2008 12:23 PM
Removing empty folders using 'find' deTTo UNIX for Dummies Questions & Answers 5 04-21-2008 10:24 PM



All times are GMT -4. The time now is 07:01 AM.