Solved

Check whether the directory is empty or not

Posted on 2011-02-25
7
763 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 25 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 19

Assisted Solution

by:simon3270
simon3270 earned 25 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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 19

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

717 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