Solved

Check whether the directory is empty or not

Posted on 2011-02-25
7
726 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using YubiKey with REST API application 2 108
endX challenge 2 63
allswap challenge 6 80
Regular Expression Calculator Tester 2 59
This is an explanation of a simple data model to help parse a JSON feed
This is about my first experience with programming Arduino.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now