I am trying to write a script which will check subdirectories for the existence of a compressed file, and if file does not exist, compress necessary file.
I have tried two versions of my script:
VERSION 1
for directory in /pool/folder/ID*/; do
echo "Starting for loop"
if [ -e /pool/folder/ID*/*.txt.gz ]
then
echo "file already exists"
else
echo "File does not exist"
echo "file is being compressed"
gzip *.txt
fi
done
This didn't work, and I assumed it had something to do with my -e flag: http://www.ducea.com/2009/03/05/bash-tips-if-e-wildcard-file-check-too-many-arguments/
Once my "if" statement starts, the terminal output looks like it's trying to check every subdirectory on my computer:
Display all 4854 possibilities? (y or n)
And then it proceeds to list every directory. So I tried removing my -e flag and using ls:
VERSION 2
for directory in /pool/folder/ID*/; do
echo "Starting for loop"
files=$(ls /pool/folder/ID*/*.txt.gz 2> /dev/null | wc -l)
if [ **"$files" != "0"** ]
then
echo "zipped file already exists"
else
echo "Zipped file does not exist"
fi
done
But the same thing happened (listing out all the directories).