Solved

Check whether the directory is empty or not

Posted on 2011-02-25
7
713 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Batch file output 20 78
countAbc challenge 9 50
mapShare challenge 13 69
VB6 Compile Compatibility Issue 4 40
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now