LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-27-2004, 01:34 PM   #1
Aziz
LQ Newbie
 
Registered: Sep 2004
Location: USA
Distribution: Fedora Core 3
Posts: 29

Rep: Reputation: 15
How to check if a directory is empty?


Hi guys I just went throught this site by luck.. seems very nice and hope I will learn alot from it..

I am trying to write a script for my first class, part of it to check is if a directory is empty or not.. I am doing it in bash/korn

I tried many things like:

#$d is the directory name

if [[ ls $d ]] > /dev/null
then
echo "not empty"
else
echo "empty"
fi

I also tried if [[ -f $d/*.* ]]

which also didnt work

I would appreciate any help

peace,
 
Old 09-27-2004, 03:01 PM   #2
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
if [ -z `ls /some/dir` ]
 
Old 09-27-2004, 04:46 PM   #3
wipe
Member
 
Registered: Jun 2004
Location: High Green
Distribution: Fedora Core 4
Posts: 180

Rep: Reputation: 30
That won't work properly, it produces an error if there is more than one file in the directory. Also, hidden files are missed. A suggestion:
Code:
if [ -z "`ls -A $d`" ]; then
    echo empty
else
    echo not empty
fi
The [[ ]] command seems to be rarely used (at least in Bash). It can do string matching with the normal pathname expansion patterns that resemble primitive regular expressions. The [ ] command is more useful. Proper regular expressions can be handled with grep and sed, for example.

Here's a good Bash guide:
http://www.tldp.org/LDP/abs/html/index.html

Regards
Simon

Last edited by wipe; 09-27-2004 at 05:03 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rm: cannot remove directory `ff': Directory not empty blancs Linux - General 17 08-29-2011 01:47 PM
Empty directory contents stefaandk Linux - General 1 08-22-2005 07:03 AM
my /boot directory is empty nvemuri Linux - Newbie 3 03-17-2005 10:00 PM
How can I check wether a file is empty in Linux by using shell script programming ??? Bassam Linux - General 2 02-26-2004 01:01 AM
deleting non-empty directory sadiboyz Linux - Newbie 2 04-21-2003 09:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration