<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "[Linux]找出某個目錄底下的空目錄並刪除之"]]></title>
		<link>https://forum.andowson.com/posts/list/16.page</link>
		<description><![CDATA[Latest messages posted in the topic "[Linux]找出某個目錄底下的空目錄並刪除之"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>[Linux]找出某個目錄底下的空目錄並刪除之</title>
				<description><![CDATA[ 最近發現網站的upload目錄底下出現一些空的目錄，原本檔案上傳的程式的邏輯是在檔案上傳時同時依年、月、日建立三層目錄（例如upload/2010/10/24)，將檔案放在日期那一層。如果事後將該檔案刪除，就會留下一個空的日期目錄。 
<br>
<br>
要將這些空目錄一個一個去找出來顯然不太聰明而且相當費時，我們可以簡單的利用 find 指令來找，有兩個參數可用，分別是 -depth 與 -empty。另外可以加上 -type d 讓它只找目錄，不加的話，它會會連空檔案都找出來。 
<br>
[code=bash]find ~/www/upload -depth -empty -type d[/code] 
<br>
確認沒問題後，配合 -exec 就可以將這些目錄全部刪掉了： 
<br>
[code=bash]find ~/www/upload -depth -empty -type d -exec rmdir -v {} \;[/code] 
<br>
最後，要正本清源還是得修改程式，將刪除檔案的功能補上檢查上層目錄是否為空並刪除該上層目錄的功能，如果不是每個月都有檔案上傳的話，可以考慮刪到月那一層的目錄。 
<br>
<br>
參考資料： 
<br>
http://people.debian.org.tw/~chihchun/2010/04/15/find-the-empty-dir/]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/448/915.page</guid>
				<link>https://forum.andowson.com/posts/preList/448/915.page</link>
				<pubDate><![CDATA[Sun, 24 Oct 2010 21:48:25]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>