On my Mac I am trying to figure out a way to check a mounted volume to a server to see if the directory receives a log file through a shell script that will be used in launchd
set to a time interval.
From my searches and historically I've used:
$DIR="/path/to/file"
THEFILES=(`find ./ -maxdepth 1 -name "*.log"`)
if [ ${#THEFILES[@]} -gt 0 ]; then
echo "exists"
else
echo "nothing"
fi
If the shell script is placed inside that particular directory and the files are present. However, when I move the script outside of that directory and try:
THEFILES=(`find ./ -maxdepth 1 -name "*.log"`)
cd $DIR
if [ ${#THEFILES[@]} -gt 0 ]; then
echo "exists"
else
echo "nothing"
fi
I get a constant return of nothing
. I thought it might be in regards to the depth so I changed -maxdepth 1
to -maxdepth 0
but I still get nothing
. Through searching I ran across "Check whether a certain file type/extension exists in directory" and tried:
THEFILES=$(ls "$DIR/.log" 2> /dev/null | wc -l)
echo $THEFILES
but I'm returned a constant 0
. When I searched further I ran across "Checking from shell script if a directory contains files" and tried a variation using find
with:
THEFILES=$(find "$DIR" -type f -regex '*.log')
cd $DIR
if [ ${#THEFILES[@]} -gt 0 ]; then
echo "exists"
else
echo "nothing"
fi
with a blank return. When I try:
if [ -n "$(ls -A $DIR)" ]; then
echo "exists"
else
echo "nothing"
fi
I get a blank terminal returned. On this answer I dont have prune
or shopt
on my Mac. So how I can I check a mounted server's directory to see if a particular file with a specific extension exists that will not give a false return from hidden files?
EDIT:
Per comment I tried removing the depth with:
THEFILES=$(find ./ -name "*.log")
but I get a blank return but if I drop a .log
file in there it runs but I don't understand why else isn't returning nothing
unless it's considering the hidden files. Thanks to l'L'l I learned -prune
was in find
's utility but when I try:
if [ -n "$(find $DIR -prune -empty -type d)" ]; then
I get a constant return of nothing
when there is a LOG file present.