-1

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
Shawn
  • 47,241
  • 3
  • 26
  • 60
rapturas
  • 9
  • 1
  • 3
  • Use `bash -x yourscript` to see a trace log of what it's actually doing. Compare the grep command inside that log to the one you know works. – Charles Duffy Oct 14 '22 at 00:01
  • that said, to find files with a given date range, you should be using `find`, not grepping output of `ls`. (In general, `ls` is only for interactive use; it shouldn't be used for scripts at all). – Charles Duffy Oct 14 '22 at 00:02
  • See https://mywiki.wooledge.org/UsingFind#Searching_based_on_times, and `man find`. – Charles Duffy Oct 14 '22 at 00:04
  • Also, [Using find to locate files modified yesterday](https://stackoverflow.com/questions/38380079/using-find-to-locate-files-modified-within-yesterday) – Charles Duffy Oct 14 '22 at 00:04
  • Maybe you forgot ```ago``` in ```DATE=$(date -d '1 day' +%y%m%d)```? As it returns tomorrows day not yesterdays. The correct one for yesterday would be ```DATE=$(date -d '1 day ago' +%y%m%d)``` – m19v Oct 14 '22 at 00:08
  • Thanks for the quick replies, I'll try using `find` tomorrow. @m19v I did try `DATE=$(date -d '1 day ago' +%y%m%d)` and it gives the same output as before :) Confirmed by doing `echo "$DATE"` – rapturas Oct 14 '22 at 00:10
  • Why are you using `ls` and `grep` for this? Just use `if [ -d "/path/to/folders/*$DATE*" ]` – Barmar Oct 14 '22 at 00:44
  • Please note: [Why *not* parse `ls`?](http://unix.stackexchange.com/questions/128985/why-not-parse-ls) – Cyrus Oct 14 '22 at 01:09

2 Answers2

0

You may simply use ls -d startsWith* and check if the output is empty as follows:

#!/bin/bash

dirsStartingWith="/path/to/dir/*$(date -d '1 day ago' +%y%m%d)*"

if [[ $(ls -d $dirsStartingWith 2>/dev/null) ]]; then
  echo "there are folders starting with $dirsStartingWith"
  #ls -d $dirsStartingWith    # to test output
else
    echo "no folders starting with $dirsStartingWith found"
fi

P.S. You may also use find, but I think ls should be sufficient as date is contained in the name of folders.

m19v
  • 1,800
  • 4
  • 11
  • 26
  • I got it working but as the date is not at the beginning nor end of the folder name, I had to include basic regex: `#!/usr/bin/env bash` `dirsIncluding="$(date -d '-1 day' +%Y%m%d)"` `dirs="/path/to/dir"` `regex="*"` `echo $dirs/$regex$dirsIncluding$regex` `if [[ $(ls -d $dirs/$regex$dirsIncluding$regex 2>/dev/null) ]]; then` `echo "Something found"` ` else` `echo "Nothing found."` `fi` Thank you!! – rapturas Oct 14 '22 at 11:46
  • You can directly add ```*``` to the beginning of expression to be assigned to dirsStartingWith variable as it is added after to be matched date in the middle of the filename. I have adjusted my answer. Please consider accepting and voting if the answer helped you. – m19v Oct 14 '22 at 13:04
0

I don't see a compelling reason to use grep. I would simply use an explicit loop:

directories_found=0
for entry in *$(date -d '1 day' +%y%m%d)*
do
  if [[ -d $entry ]]
  then
    ((directories_found++))
  fi
done
echo Number of matching directories: $directories_found 
user1934428
  • 19,864
  • 7
  • 42
  • 87