<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Bash Shell Check Whether a Directory is Empty or Not</title> <atom:link href="http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/feed/" rel="self" type="application/rss+xml" /><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/</link> <description>Every answer asks a more beautiful question.</description> <lastBuildDate>Fri, 10 Feb 2012 19:55:56 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: stainless</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-66413</link> <dc:creator>stainless</dc:creator> <pubDate>Thu, 05 Jan 2012 15:09:12 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-66413</guid> <description>Ok, this works, after hours of coding a simple solution was found to be best  loll.What kept giving me trouble was the *, also I had found a solution a long time ago, but it didnt work if you had empty files in the directory.I tried all the IF statement solutions and it wont work in all situations.So best method is below:
#!/bin/bash
DIR=/var/www/tee
for i in $DIR/*
do
if [ -e $i ] &amp;&amp; [ $(echo $?) -eq &quot;0 &quot; ]
then
echo The Directory is NOT EMPTY
else
echo The directory is EMPTY
fi
done</description> <content:encoded><![CDATA[<p>Ok, this works, after hours of coding a simple solution was found to be best  loll.What kept giving me trouble was the *, also I had found a solution a long time ago, but it didnt work if you had empty files in the directory.I tried all the IF statement solutions and it wont work in all situations.So best method is below:</p><p>#!/bin/bash</p><p>DIR=/var/www/tee</p><p>for i in $DIR/*<br
/> do<br
/> if [ -e $i ] &amp;&amp; [ $(echo $?) -eq "0 " ]</p><p> then<br
/> echo The Directory is NOT EMPTY</p><p> else</p><p> echo The directory is EMPTY</p><p> fi<br
/> done</p> ]]></content:encoded> </item> <item><title>By: ReddyRace</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-65390</link> <dc:creator>ReddyRace</dc:creator> <pubDate>Fri, 09 Dec 2011 21:58:55 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-65390</guid> <description>Jairo :
Declare the variable ,before you use it.</description> <content:encoded><![CDATA[<p>Jairo :</p><p>Declare the variable ,before you use it.</p> ]]></content:encoded> </item> <item><title>By: Jairo</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-64245</link> <dc:creator>Jairo</dc:creator> <pubDate>Wed, 09 Nov 2011 04:29:56 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-64245</guid> <description>Hi all,
someone please say me why this dont work.
src=&quot;/Users/xxxx/scripts/dirtest&quot;
for dir in `ls -R &quot;$src&quot;`
do
if [ -d &quot;$dir&quot; ]; then
echo this is a dir: $dir
fi
done
thanks</description> <content:encoded><![CDATA[<p>Hi all,<br
/> someone please say me why this dont work.</p><p>src=&#8221;/Users/xxxx/scripts/dirtest&#8221;<br
/> for dir in `ls -R &#8220;$src&#8221;`<br
/> do<br
/> if [ -d "$dir" ]; then<br
/> echo this is a dir: $dir<br
/> fi<br
/> done</p><p>thanks</p> ]]></content:encoded> </item> <item><title>By: samoak</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-59174</link> <dc:creator>samoak</dc:creator> <pubDate>Wed, 04 May 2011 07:40:18 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-59174</guid> <description>what if the directory has files but each file is of 0 size?
best way is to do as below:
dir_full_path=&quot;/tmp/foo&quot;
if [ -d &quot;$dir_full_path&quot; ] ; then
if [ 4 -ge $(du -s &quot;$dir_full_path&quot; ] ; then
echo &quot;empty directory $dir_full_path&quot;
else
echo &quot;non-empty directory $dir_full_path&quot;
fi
fi
This snippet assumes that a directory size, by its own name, is always 4096 bytes, approximated to 4.0K evaluated to int as 4</description> <content:encoded><![CDATA[<p>what if the directory has files but each file is of 0 size?</p><p>best way is to do as below:<br
/> dir_full_path=&#8221;/tmp/foo&#8221;<br
/> if [ -d "$dir_full_path" ] ; then<br
/> if [ 4 -ge $(du -s "$dir_full_path" ] ; then<br
/> echo &#8220;empty directory $dir_full_path&#8221;<br
/> else<br
/> echo &#8220;non-empty directory $dir_full_path&#8221;<br
/> fi<br
/> fi</p><p>This snippet assumes that a directory size, by its own name, is always 4096 bytes, approximated to 4.0K evaluated to int as 4</p> ]]></content:encoded> </item> <item><title>By: kevin</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-54756</link> <dc:creator>kevin</dc:creator> <pubDate>Sat, 08 Jan 2011 01:31:47 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-54756</guid> <description>if [ &quot;$(ls -A $DIR)&quot; ]; then
How can I use this expression exclude one directory?</description> <content:encoded><![CDATA[<p>if [ "$(ls -A $DIR)" ]; then<br
/> How can I use this expression exclude one directory?</p> ]]></content:encoded> </item> <item><title>By: anonymous</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-51728</link> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 09 Dec 2010 21:25:06 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-51728</guid> <description>find &quot;/dir2&quot; -type d -empty -exec command1 -arg1 {} \;</description> <content:encoded><![CDATA[<p>find &#8220;/dir2&#8243; -type d -empty -exec command1 -arg1 {} \;</p> ]]></content:encoded> </item> <item><title>By: SnakE</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-50721</link> <dc:creator>SnakE</dc:creator> <pubDate>Sat, 13 Nov 2010 05:20:19 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-50721</guid> <description>Replace
if [ &quot;$(ls -A $DIR)&quot; ]; then
with
if [ &quot;$(ls -A &quot;$DIR&quot;)&quot; ]; then
That&#039;s just another pair of quotes, no escapes.  Should work.</description> <content:encoded><![CDATA[<p>Replace</p><p>if [ "$(ls -A $DIR)" ]; then</p><p>with</p><p>if [ "$(ls -A "$DIR")" ]; then</p><p>That&#8217;s just another pair of quotes, no escapes.  Should work.</p> ]]></content:encoded> </item> <item><title>By: pmcneil</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-50331</link> <dc:creator>pmcneil</dc:creator> <pubDate>Mon, 25 Oct 2010 06:14:45 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-50331</guid> <description>dude. give find some love ;-)
find . -type d -empty -exec touch {}/.empty \;</description> <content:encoded><![CDATA[<p>dude. give find some love ;-)</p><p>find . -type d -empty -exec touch {}/.empty \;</p> ]]></content:encoded> </item> <item><title>By: daniel</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-48709</link> <dc:creator>daniel</dc:creator> <pubDate>Tue, 10 Aug 2010 08:49:33 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-48709</guid> <description>The following code does not correctly identify empty directories. Using DIR=$1 then passing &quot;home/dan/not empty&quot; does not help, neither does using &quot;home/dan/not\ empty&quot;  or home/dan/not\ empty.
#!/bin/bash
DIR=&quot;home/dan/not empty&quot;
if [ &quot;$(ls -A $DIR)&quot; ]; then
echo &quot;$DIR is not empty&quot;
else
echo &quot;$DIR is empty&quot;
fi</description> <content:encoded><![CDATA[<p>The following code does not correctly identify empty directories. Using DIR=$1 then passing &#8220;home/dan/not empty&#8221; does not help, neither does using &#8220;home/dan/not\ empty&#8221;  or home/dan/not\ empty.</p><p>#!/bin/bash</p><p>DIR=&#8221;home/dan/not empty&#8221;<br
/> if [ "$(ls -A $DIR)" ]; then<br
/> echo &#8220;$DIR is not empty&#8221;<br
/> else<br
/> echo &#8220;$DIR is empty&#8221;<br
/> fi</p> ]]></content:encoded> </item> <item><title>By: Crístian Viana</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-48702</link> <dc:creator>Crístian Viana</dc:creator> <pubDate>Mon, 09 Aug 2010 15:51:48 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-48702</guid> <description>enclose the directory name parameter in double quotes.</description> <content:encoded><![CDATA[<p>enclose the directory name parameter in double quotes.</p> ]]></content:encoded> </item> <item><title>By: daniel</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-48699</link> <dc:creator>daniel</dc:creator> <pubDate>Mon, 09 Aug 2010 14:50:55 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-48699</guid> <description>I am trying to pass a directory path as a parameter to my bash script (which tests whether its empty), but the directory name contains spaces. Any suggestions?</description> <content:encoded><![CDATA[<p>I am trying to pass a directory path as a parameter to my bash script (which tests whether its empty), but the directory name contains spaces. Any suggestions?</p> ]]></content:encoded> </item> <item><title>By: nova</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-47210</link> <dc:creator>nova</dc:creator> <pubDate>Thu, 06 May 2010 05:16:51 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-47210</guid> <description>Works with both BSD and GNU Linux find:
&lt;code&gt;find &quot;/nas/data&quot; -maxdepth 0 -empty -exec echo {} is empty. \;  &lt;/code&gt;
OR
&lt;code&gt;find &quot;/dir2&quot; type d -empty -exec command1 -arg1 {} \;  &lt;/code&gt;</description> <content:encoded><![CDATA[<p>Works with both BSD and GNU Linux find:<br
/> <code>find "/nas/data" -maxdepth 0 -empty -exec echo {} is empty. \; </code><br
/> OR<br
/> <code>find "/dir2" type d -empty -exec command1 -arg1 {} \; </code></p> ]]></content:encoded> </item> <item><title>By: Indie</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-45801</link> <dc:creator>Indie</dc:creator> <pubDate>Tue, 02 Feb 2010 17:49:44 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-45801</guid> <description>A slight variation on the original post which does it numerically and includes dot files.
&lt;pre&gt;DIR=/home/user
[ $(( $(ls -a1 $DIR &#124; wc -l) &gt;= 3 )) = 0 ] &amp;&amp; echo Empty &#124;&#124; echo Nope&lt;/pre&gt;
That&#039;s a one and not an &#039;l&#039; to the ls command. An empty directory only has the two entries - . &amp; ..</description> <content:encoded><![CDATA[<p>A slight variation on the original post which does it numerically and includes dot files.</p><pre>DIR=/home/user
[ $(( $(ls -a1 $DIR | wc -l) &gt;= 3 )) = 0 ] &amp;&amp; echo Empty || echo Nope</pre><p>That&#8217;s a one and not an &#8216;l&#8217; to the ls command. An empty directory only has the two entries &#8211; . &amp; ..</p> ]]></content:encoded> </item> <item><title>By: Kyle</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-44108</link> <dc:creator>Kyle</dc:creator> <pubDate>Thu, 15 Oct 2009 18:48:55 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-44108</guid> <description>Scot&#039;s suggestion works absolutely perfectly.</description> <content:encoded><![CDATA[<p>Scot&#8217;s suggestion works absolutely perfectly.</p> ]]></content:encoded> </item> <item><title>By: Tomas M</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-41720</link> <dc:creator>Tomas M</dc:creator> <pubDate>Thu, 21 May 2009 12:48:40 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-41720</guid> <description>Thanks Scot!</description> <content:encoded><![CDATA[<p>Thanks Scot!</p> ]]></content:encoded> </item> <item><title>By: Scot</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-39720</link> <dc:creator>Scot</dc:creator> <pubDate>Tue, 06 Jan 2009 01:06:42 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-39720</guid> <description>&lt;code&gt;find -type d -empty&lt;/code&gt;</description> <content:encoded><![CDATA[<p><code>find -type d -empty</code></p> ]]></content:encoded> </item> <item><title>By: David</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-37075</link> <dc:creator>David</dc:creator> <pubDate>Fri, 23 Nov 2007 21:29:13 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-37075</guid> <description>Yes.  I&#039;d forgotten: when no matches are found (&quot;*&quot;) then the resulting text is the character unchanged.  This should work better:
FILES=&quot;`echo .* *`&quot;
if [ $FILES = &#039;. .. *&#039; ] ; then
: empty dir
else
: not empty
fi</description> <content:encoded><![CDATA[<p>Yes.  I&#8217;d forgotten: when no matches are found (&#8220;*&#8221;) then the resulting text is the character unchanged.  This should work better:</p><p>FILES=&#8221;`echo .* *`&#8221;<br
/> if [ $FILES = '. .. *' ] ; then<br
/> : empty dir<br
/> else<br
/> : not empty<br
/> fi</p> ]]></content:encoded> </item> <item><title>By: Raju</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-37074</link> <dc:creator>Raju</dc:creator> <pubDate>Fri, 23 Nov 2007 21:06:43 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-37074</guid> <description>David,
It is not working for me. I&#039;m using Debian + Bash 3. It returns 3 when directory is empty, it should be 2 as empty directory has only . and ..
Any idea?</description> <content:encoded><![CDATA[<p>David,</p><p>It is not working for me. I&#8217;m using Debian + Bash 3. It returns 3 when directory is empty, it should be 2 as empty directory has only . and ..</p><p>Any idea?</p> ]]></content:encoded> </item> <item><title>By: David</title><link>http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-37073</link> <dc:creator>David</dc:creator> <pubDate>Fri, 23 Nov 2007 19:36:11 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/#comment-37073</guid> <description>Seems to me that using ls is not required - in fact, it is doable within the shell alone:
&lt;code&gt;set - `echo .* *`
if [ $# = &quot;2&quot; ] ; then
: empty directory
else
: not empty ...
fi&lt;/code&gt;
David
&lt;a href=&quot;http://administratosphere.wordpress.com&quot; title=&quot;UNIX Administratosphere&quot; rel=&quot;nofollow&quot;&gt;UNIX Administratosphere&lt;/a&gt;</description> <content:encoded><![CDATA[<p>Seems to me that using ls is not required &#8211; in fact, it is doable within the shell alone:</p><p><code>set - `echo .* *`<br
/> if [ $# = "2" ] ; then<br
/> : empty directory<br
/> else<br
/> : not empty ...<br
/> fi</code></p><p>David<br
/> <a
href="http://administratosphere.wordpress.com" title="UNIX Administratosphere" rel="nofollow">UNIX Administratosphere</a></p> ]]></content:encoded> </item> </channel> </rss>
