Solved

Check whether the directory is empty or not

Posted on 2011-02-25
7
744 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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…

856 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