found_nonempty=''
for file in $WORKING_DIR* ; do
if [[ -s "$file" ]] ; then
found_nonempty=1
fi
done
if [[ "$found_nonempty" ]] ; then
echo found one
else
echo found none
fi
but this was only to check if file exist in current directory.
Please advise,
Thanks,
---------- Post updated at 10:27 AM ---------- Previous update was at 08:35 AM ----------
doesn't say anything about file sizes (which is all that the code snippet you have shown us tests).
If a file found in $DIR2 and is not found in $WORKING_DIR , what do you want to happen?
If a file is present in $WORKING_DIR that is not found in $DIR2 , is anything supposed to happen? If so, what?
Does the size of the file make any difference in what is supposed to happen if a file is found in one of these two directories and not in the other? If so, what?
WORKING_DIR=/home/working_dir
DIR2=/home/target_dir
#check if there's a zip file in the WORKING_DIR
if [ "`ls -A $WORKING_DIR/*.ZIP 2>/dev/null`" ]
then
echo "contains files (or is a file)"
cd $WORKING_DIR
for i in *.ZIP
do
a=$(find $DIR2 -iname "$i" -print | wc -l) -- i used find command to check if there's a file in $DIR2 equals to $i and count
if [[ $a > 0 ]]; then
echo "File Exist in $DIR2"
else
echo "File Not Exist in $DIR2"
echo "Copying $i to $DIR2"
cp $i $DIR2
fi
done
else
echo "No such file in the Directory"
fi
Assuming that filenames are identical if they exist in both directories (i.e., no case differences), you might find the following to be more concise and more efficient in terms of CPU and disk usage:
#!/bin/ksh
WORKING_DIR=/home/working_dir
DIR2=/home/target_dir
cd $WORKING_DIR
for i in *.[Zz][Ii][Pp]
do if [ "$i" = "*.[Zz][Ii][Pp]" ]
then echo "No such file in the Directory"
exit
fi
if [ -f "$DIR2/$i" ]
then echo "File $i Exist in $DIR2"
else echo "File $i Not Exist in $DIR2"
echo "Copying $i to $DIR2"
cp "$i" "$DIR2"
fi
done
I used the Korn shell for testing this (you didn't say what shell you're using), but this should work with any shell that follows basic Bourne shell syntax.