Fedora Linux Support Community & Resources Center
  #1  
Old 9th January 2011, 01:22 PM
glennzo's Avatar
glennzo Offline
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Posts: 13,929
linuxchrome
Find empty folders

How would one go about finding empty folders in a specific path? In other words, I have a music folder, /mnt/sata500/music. In that folder path there are folders that contain albums and music files but there are also various empty folders. I would like to remove those folders but I need to find them first. I realize that I could use Nautilus and find/remove each one by hand but that would take considerable time. I'd like to find them using a command line utility and delete them in a similar fashion.

What I've been doing to find various music files is using find, for example
Code:
[glenn@phenom14 sata500>$ find /mnt/sata500/music/ -type f | grep mp3 | wc -l
2018
Can I use this same type of search to find folders that are empty? If so, how then would I remove them? Maybe pipe the find command to a text file and then use the text file as criteria for the rm command?
__________________
Glenn
The Bassinator © ®


Laptop: Toshiba Satellite / Intel Core 2 Duo 1.73 GHz / 2GB / 160GB / Intel Mobile 945GM/GMS/GME/943/940GML Integrated Graphics
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / 1TB SATA / 500GB SATA / EVGA GeForce 8400 GS 1GB
Reply With Quote
  #2  
Old 9th January 2011, 01:51 PM
Dangermouse's Avatar
Dangermouse Offline
Administrator
 
Join Date: Aug 2007
Location: London Postbox (the red one)
Age: 47
Posts: 3,847
linuxfedorafirefox
Re: Find empty folders

Hi Glenn

To find
Quote:
find -type d -empty
and to remove (AT YOUR OWN RISK)
Quote:
find -type d -empty -delete
ps do you like the new look? (fedoraplus)
Reply With Quote
  #3  
Old 9th January 2011, 01:53 PM
glennzo's Avatar
glennzo Offline
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Posts: 13,929
linuxchrome
Re: Find empty folders

You know what? MC seems the best way to go.

Carry on .....


Thanks Dangermouse.

Yes, the new look is a tremendous improvement.
__________________
Glenn
The Bassinator © ®


Laptop: Toshiba Satellite / Intel Core 2 Duo 1.73 GHz / 2GB / 160GB / Intel Mobile 945GM/GMS/GME/943/940GML Integrated Graphics
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / 1TB SATA / 500GB SATA / EVGA GeForce 8400 GS 1GB
Reply With Quote
  #4  
Old 9th January 2011, 02:05 PM
smr54 Online
Registered User
 
Join Date: Jan 2010
Posts: 4,976
linuxchrome
Re: Find empty folders

find whatever |xargs rm -rf

xargs is a nice handy tool--it takes the result of what you're doing and uses that as the argument to the next command.

yum search font |grep -i japanese |awk '{print $1}' |xargs yum -y install

(If you knew all that, sorry).

To find empty directories it's surprisingly straightforward

find /mnt/sata500/music/ -type d -empty


So

find /mnt/sata500/music -type d -empty |xargs rmdir

This is pretty safe, because if the directory wasn't really empty, it won't work with rmdir, you'd have to use rm -rf.

One thing I like to do though, to avoid surprises, is to use echo first.

find /mnt/sata500/music -type d -empty |xargs echo rmdir

Which shows you what it would do.
Reply With Quote
  #5  
Old 9th January 2011, 06:23 PM
kurtdriver Offline
Registered User
 
Join Date: Jul 2005
Location: Vancouver Canada
Posts: 1,285
linuxfedorafirefox
Re: Find empty folders

This just worked on a test directory on F13.
Code:
ls -R |rmdir test/*
Seems odd that rmdir doesn't have an option for recursive.
Reply With Quote
  #6  
Old 9th January 2011, 08:16 PM
forkbomb's Avatar
forkbomb Offline
Registered User
 
Join Date: May 2007
Location: U.S.
Posts: 4,852
linuxfirefox
Re: Find empty folders

Quote:
Originally Posted by kurtdriver View Post
This just worked on a test directory on F13.
Code:
ls -R |rmdir test/*
Seems odd that rmdir doesn't have an option for recursive.
That's what rm -rf is for.
__________________
- Tom
"What is freedom? To have the will to be responsible for one's self." - Stirner
Reply With Quote
  #7  
Old 10th January 2011, 12:59 AM
kurtdriver Offline
Registered User
 
Join Date: Jul 2005
Location: Vancouver Canada
Posts: 1,285
linuxfedorafirefox
Re: Find empty folders

Quote:
Originally Posted by forkbomb View Post
That's what rm -rf is for.
Now that's a head slapper.
Reply With Quote
  #8  
Old 14th January 2011, 07:48 PM
banji Offline
Registered User
 
Join Date: Feb 2007
Posts: 5
linuxfedorafirefox
Re: Find empty folders

Please make sure that you use xargs whenever you need to execute a command on find's results. Also make sure that you display the results before deleting.
Reply With Quote
Reply

Tags
folders

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Disappearing files (empty folders) gregfr Using Fedora 2 27th December 2010 02:47 PM
samba folders are empty in Nautilus pxp Using Fedora 1 23rd August 2008 10:12 AM
Unable to find folders and files for installation anxious Using Fedora 1 7th June 2006 07:17 PM


Current GMT-time: 15:35 (Monday, 20-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat