Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the best way to check if a directory is empty

Status
Not open for further replies.

HHG

Technical User
Nov 8, 2003
68
GB
Hi, I would very much appreciate your help in this. I need to write a condition around the whole of my scripts. In that I need to check if a directory is empty before the script kicks off and does it business. The script is run in cron.

e.g. if the directory a is empty I want the rest of the script gets excuted. Otherwise an email to be sent to the user.

if directory empty
then
process the whole script
else
send an email.

Hope you can help. Many thanks in advance
 
Code:
if [ $(ls | wc -l) -eq 0 ]
then
  process_script
else
  echo its empty | mail me@myserver
fi

Ceci n'est pas une signature
Columb Healy
 
Maby this way?
Code:
geir@festy-fawn:~/testdir$ ls -l
totalt 0
geir@festy-fawn:~/testdir$ touch testfile.txt
geir@festy-fawn:~/testdir$ if [ -e ./* ]; then echo "It's not empty."; else echo "It's empty."; fi
It's not empty.
geir@festy-fawn:~/testdir$ rm testfile.txt
geir@festy-fawn:~/testdir$ if [ -e ./* ]; then echo "It's not empty."; else echo "It's empty."; fi
It's empty.
geir@festy-fawn:~/testdir$

HTH :)
 

Thanks all for this I used columb syntax and it works fine.

Thanks for your help much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top