I've searched high and low for a working answer but here I am, still stuck. I'm new to bash scripting and have spent the past few days trying to achieve my goal, but I'm losing my mind instead.
GOAL: I want to run a script that checks for directories that contains yesterday's date (date appears in between other text in the directory name). Sounds simple!
What I have so far:
DATE=$(date -d '1 day' +%y%m%d)
ls /path/to/folders > ~/listofdirs.txt
GREPDIR=$(grep $DATE ~/listofdirs.txt)
if [ -d /path/to/folders/$GREPDIR ]; then
echo "Dir exists!"
echo "(cat $GREPDIR)"
exit 1
else
echo "Nothing found."
fi
Grep isn't finding any results as I am sure the $DATE isn't working as I expect. If I substitute $DATE with eg: 2022, I get a result. Thanks for any help, direction, advice.
EDIT: The following works :D
#!/usr/bin/env bash
#
dirsIncluding="$(date -d '-1 day' +%Y%m%d)"
dirs="/path/to/dir"
regex="*"
if [[ $(ls -d $dirs/$regex$dirsIncluding$regex 2>/dev/null) ]]; then
echo "Something found."
else
echo "Nothing found."
fi