Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check whether the directory is empty or not

Posted on 2011-02-25
7
Medium Priority
?
768 Views
Last Modified: 2012-05-11
I am writing korn shell script in linux for some task.
As a part of it I want to check whether the directory is empty or not. If the directory is empty I just want to come out of the loop. Otherwise continue processing.

Is it the best way of testing by using  find command with emtpy option. What find will return in this case.

while(find /home/user/directory1 -empty -print)
do
//statement

done

Please suggest me a best solution for this

Thanks,
Radha.
0
Comment
Question by:Kris2011
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 12

Accepted Solution

by:
upanwar earned 100 total points
ID: 34985900
the simplest and most effective way is to use ls command with -A option:

$ [ "$(ls -A /path/to/directory)" ] && echo "Not Empty" || echo "Empty"

or
$ [ "$(ls -A /tmp)" ] && echo "Not Empty" || echo "Empty"

or

Use if..else.fi in a shell script:

#!/bin/bash
FILE=""
DIR="/tmp"
# init
# look for empty dir
if [ "$(ls -A $DIR)" ]; then
     echo "Take action $DIR is not Empty"
else
    echo "$DIR is Empty"
fi
# rest of the logic
0
 
LVL 20

Assisted Solution

by:simon3270
simon3270 earned 100 total points
ID: 34986355
or

if [ "$(ls -l /path/to/dir | head -1)" = "total 0" ]
then
  dir is empty
else
  is not empty
fi
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 35321643
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Kris2011
ID: 35372498
I am sorry for no reply for these many days.

What is the problem if I use find command with -empty option? Just to know the difference.
0
 
LVL 20

Expert Comment

by:simon3270
ID: 35372650
find dir1 -empty -print will display the names of all empty directories under the dir1 directory, but also any zero-length files found in a those directories.  If there are no subdirectories, and dir1 is empty, it will indeed return just dir1.  If dir1 has no subdirectories but is not empty and has no zero-length files, the find will return no data.  The "-print" is usually omitted (it is assumed for most common "find" statements).

If you know that there are no subdirectories in dir1, and that none of the files are zero-length, you could do something like:
if [ "$(find dir1 -empty)" = "" ]
then
    # the directory is not empty
    : do work for non-empty directory
done

Open in new window

But I think it is safer to use the solutions we provided!
 
0
 

Author Comment

by:Kris2011
ID: 35397404
Thanks for your clear explanation
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question