<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Quickly delete a directory containing many files</title>
	<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files/</link>
	<description>Comments on Ask MetaFilter post Quickly delete a directory containing many files</description>
	<pubDate>Mon, 16 Apr 2007 14:12:00 -0800</pubDate>
	<lastBuildDate>Mon, 16 Apr 2007 14:12:00 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Quickly delete a directory containing many files</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files</link>	
		<description>In a Unix-type system, is there a way to quickly remove a directory that contains lots and lots of files? &lt;br /&gt;&lt;br /&gt; So I ran a job overnight and was kind of careless when I programmed it.  When I went to the directory where I was writing my log files and typed &apos;ls&apos;, the system basically sat there.  Then I realized that I had written a separate log file for each trial, and I ran a lot of trials... I&apos;m not sure how many files are in that folder but it could easily be a million.  oops!&lt;br&gt;
&lt;br&gt;
It&apos;s been spinning for an hour so far.  It isn&apos;t accepting new connections - it stalls in the password check of ssh - so I can&apos;t kill the job.  Once it finally finishes &apos;ls&apos;ing, I want to remove that folder so I don&apos;t forget and &apos;ls&apos; it again.  If I &apos;rm -rf&apos; it I expect it will have to page through the files and that could take just as long.  What if I just change the name of the folder, will that be quick?  Any other options?</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2007:site.60733</guid>
		<pubDate>Mon, 16 Apr 2007 14:07:05 -0800</pubDate>
		<dc:creator>PercussivePaul</dc:creator>
		
			<category>linux</category>
		
			<category>unix</category>
		
			<category>files</category>
		
			<category>directory</category>
		
	</item> <item>
		<title>By: chrismear</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914532</link>	
		<description>Yes, changing the name of the folder should be quick. But as for deleting (as far as I know) there&apos;s no way around the fact that each individual file has to be unlinked.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914532</guid>
		<pubDate>Mon, 16 Apr 2007 14:12:00 -0800</pubDate>
		<dc:creator>chrismear</dc:creator>
	</item><item>
		<title>By: about_time</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914536</link>	
		<description>you should kill the ls job though. One quicker way to see the dirs contents is &quot;find . -ls&quot;.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914536</guid>
		<pubDate>Mon, 16 Apr 2007 14:14:42 -0800</pubDate>
		<dc:creator>about_time</dc:creator>
	</item><item>
		<title>By: chrismear</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914539</link>	
		<description>Actually, the -d option to rm will apparently unlink the directory regardless of whether it&apos;s empty or not. Then, in theory, an fsck would deal with all the unreferenced files that are left on the disk.&lt;br&gt;
&lt;br&gt;
This feels iffy to me, though, and I don&apos;t know enough detail to say whether this is a good idea or not. Just throwing it out there.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914539</guid>
		<pubDate>Mon, 16 Apr 2007 14:18:38 -0800</pubDate>
		<dc:creator>chrismear</dc:creator>
	</item><item>
		<title>By: MarcieAlana</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914541</link>	
		<description>mv yourdirectory somethingelse &amp;amp;&amp;amp; rm -rf somethingelse &amp;amp;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914541</guid>
		<pubDate>Mon, 16 Apr 2007 14:22:03 -0800</pubDate>
		<dc:creator>MarcieAlana</dc:creator>
	</item><item>
		<title>By: PercussivePaul</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914543</link>	
		<description>I would love to kill the ls job but I can&apos;t.  the system is non-responsive and is not accepting new connections.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914543</guid>
		<pubDate>Mon, 16 Apr 2007 14:23:26 -0800</pubDate>
		<dc:creator>PercussivePaul</dc:creator>
	</item><item>
		<title>By: aeighty</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914547</link>	
		<description>Thanks for the -d flag (didn&apos;t known about that). The problem remains though, next time your system requires an fsck you&apos;re gonna end up with lots of crap in lost+found which you&apos;ll have to remove anyway. I suggest you rm -rf the directory, background the process and renice it and just leave it running..</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914547</guid>
		<pubDate>Mon, 16 Apr 2007 14:24:15 -0800</pubDate>
		<dc:creator>aeighty</dc:creator>
	</item><item>
		<title>By: fvw</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914554</link>	
		<description>Don&apos;t think most modern filesystems still allow unlinking directories, and even if yours does I suspect the fsck will be just as slow. Just move it off and rm -r later. &lt;br&gt;
&lt;br&gt;
It shouldn&apos;t be blocking you from logging in though unless there system has per user process limits set up or something. It might be slow though, so try giving it some time.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914554</guid>
		<pubDate>Mon, 16 Apr 2007 14:29:40 -0800</pubDate>
		<dc:creator>fvw</dc:creator>
	</item><item>
		<title>By: grouse</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914570</link>	
		<description>It may not wake up. Now is the time to fess up to your system administrator.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914570</guid>
		<pubDate>Mon, 16 Apr 2007 14:37:58 -0800</pubDate>
		<dc:creator>grouse</dc:creator>
	</item><item>
		<title>By: mendel</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914582</link>	
		<description>Yeah, there aren&apos;t many reasons why ls&apos;ing a directory, even a huge one, should cause the system to outright &lt;i&gt;hang&lt;/i&gt; unless it&apos;s thrashing (low enough on memory that all of the CPU time is being used trying to rearrange processes to fit). Unless everything else is blocking on disk IO, I guess, but that&apos;d be an awfully slow disk.&lt;br&gt;
&lt;br&gt;
One way to get away with &quot;rm -rf&quot; once you&apos;re back in, without crippling the system, is to use &quot;nice&quot; to set the priority of the &quot;rm -rf&quot;:&lt;br&gt;
&lt;br&gt;
&lt;b&gt;nice -n 19 rm -rf directory&lt;/b&gt;&lt;br&gt;
&lt;br&gt;
will run &quot;rm -rf directory&quot; at the lowest possible priority.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914582</guid>
		<pubDate>Mon, 16 Apr 2007 14:51:17 -0800</pubDate>
		<dc:creator>mendel</dc:creator>
	</item><item>
		<title>By: PercussivePaul</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914600</link>	
		<description>Yeah, the admin sits beside me and he can&apos;t get into the system either.  It&apos;s not actually refusing connections - it just hangs after you type the password.  He thinks the disks are thrashing or something like that.  And we were going to hard reboot it but we just moved it to another server room and the guy with the key isn&apos;t here... fortunately the server is one used only by our research group so I didn&apos;t screw up anything important.  :)  Thanks guys.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914600</guid>
		<pubDate>Mon, 16 Apr 2007 15:02:02 -0800</pubDate>
		<dc:creator>PercussivePaul</dc:creator>
	</item><item>
		<title>By: melt away</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914608</link>	
		<description>Just FYI [and this may only apply to Linux systems], if the issue was too many files being present you would probably receive the &quot;too many files&quot; message itself when issuing a command on them. It could be the size of the files though.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914608</guid>
		<pubDate>Mon, 16 Apr 2007 15:09:01 -0800</pubDate>
		<dc:creator>melt away</dc:creator>
	</item><item>
		<title>By: flabdablet</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914641</link>	
		<description>Lots of core utilities can be stopped with ctrl-Z and then killed with kill %1, even if they&apos;re not responding to ctrl-C.  So you might want to fire a ctrl-Z down your existing connection and see if you can persuade ls to see reason (it&apos;s probably the sorting of the directory listing that&apos;s taking up all the CPU time and possibly causing thrashing).  If that works, then nice -n 19 rm -rf as mendel said.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914641</guid>
		<pubDate>Mon, 16 Apr 2007 15:39:04 -0800</pubDate>
		<dc:creator>flabdablet</dc:creator>
	</item><item>
		<title>By: PercussivePaul</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#914664</link>	
		<description>The guy with the key showed up.  We reset the server and tried to delete the offending directory, but there are some residual filesystem glitches.  It will be dealt with appropriately.  Thanks.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-914664</guid>
		<pubDate>Mon, 16 Apr 2007 16:01:13 -0800</pubDate>
		<dc:creator>PercussivePaul</dc:creator>
	</item><item>
		<title>By: Nelson</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#915037</link>	
		<description>Glad you found a fix. One last thing for the search engines.. Many Unix file systems are designed so that operations on files in a directory are O(n) where N is the number of files in that directory. That makes deleting a directory with 100,000 files a very unpleasant O(n^2) mess. And since most of the work is going on in the kernel, a badly designed Unix can get in a bad state simply by trying to remove a bunch of logfiles.&lt;br&gt;
&lt;br&gt;
File systems built since around 2000 tend to have fixed this problem. For instance, my memory is Linux ext3fs doesn&apos;t have the performance problem, whereas ext2fs did.&lt;br&gt;
&lt;br&gt;
On a crappy Unix, once you get a directory with too many files there aren&apos;t a lot of good solutions for cleaning it out. Booting single user and doing an rm -r is one way. Or try to remove a few files at a time and then sleep. One time I had this problem it was faster to copy all the &lt;i&gt;other&lt;/i&gt; files off the filesystem, then reformat.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-915037</guid>
		<pubDate>Tue, 17 Apr 2007 01:59:24 -0800</pubDate>
		<dc:creator>Nelson</dc:creator>
	</item><item>
		<title>By: cmiller</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#915087</link>	
		<description>Fwiw, &quot;rm&quot; isn&apos;t the lowest level you have access to.  If you need more, then you should write your own &quot;rm&quot;.  &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;i&gt;Open the directory,&lt;br&gt;
read an entry,&lt;br&gt;
loop while the entry is valid (not at the end of the list):&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;sleep for a few milliseconds,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if it&apos;s a directory, recurse back at the top &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;remove it,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;read an entry&lt;/i&gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
That will take a while, but won&apos;t thrash your machine.  Even better, add a &quot;--be-very-polite&quot; flag to GNU rm and submit a patch.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-915087</guid>
		<pubDate>Tue, 17 Apr 2007 05:43:05 -0800</pubDate>
		<dc:creator>cmiller</dc:creator>
	</item><item>
		<title>By: polyglot</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#915122</link>	
		<description>Glad it&apos;s fixed... but moving the directory won&apos;t halt the ls.  The ls has already opened the directory and is iterating through its entries; changing its name in the parent directory will do nothing.&lt;br&gt;
&lt;br&gt;
On any unix I&apos;ve come across, an open file handle (including a directory as the result of opendir(3) or whatever) counts as a reference to the file so you can delete the file and anything that&apos;s got it open (ls in your case) can still see it until they close their descriptor.&lt;br&gt;
&lt;br&gt;
Hanging after you type the password makes it sound like you&apos;ve run out of processes.  Why, I do not know.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-915122</guid>
		<pubDate>Tue, 17 Apr 2007 07:02:22 -0800</pubDate>
		<dc:creator>polyglot</dc:creator>
	</item><item>
		<title>By: imagesafari</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#915667</link>	
		<description>find /the/directory -exec rm -f {} \;&lt;br&gt;
&lt;br&gt;
rm -rf /the/directory</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-915667</guid>
		<pubDate>Tue, 17 Apr 2007 16:50:38 -0800</pubDate>
		<dc:creator>imagesafari</dc:creator>
	</item><item>
		<title>By: smackfu</title>
		<link>http://ask.metafilter.com/60733/Quickly-delete-a-directory-containing-many-files#1166335</link>	
		<description>I had to deal with this problem, and the guy from Dreamhost support suggested:&lt;br&gt;
&lt;br&gt;
find . -type f -exec rm -v {} \;&lt;br&gt;
&lt;br&gt;
...which is the same basic idea that others had, but also shows you the progress of the command.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.60733-1166335</guid>
		<pubDate>Thu, 13 Dec 2007 13:19:22 -0800</pubDate>
		<dc:creator>smackfu</dc:creator>
	</item>
	</channel>
</rss>
