Match filename pattern with -f

Hello All,
I have two issues.
1).I want to check if directory exists and inside that if file exists with today's date minus one. I can check directory exists but how can i check only a pattern of filename in that directory.Name of file is files-20170105-09.gz.
2).Also i want to exit immediately if directory is not present. Is exit 0 correct. Please help.

dir=`date "+%Y%m%d"`
filename="files-`date -d '-1 day' '+%Y%m%d'`"
echo "$filename"

[[ -d "$dir" ]] && echo "Dir exists $dir" || echo "dir $dir doesnot exists" ||  exit 0

[[ -f '$dir/$filename*' ]] && echo "Filename $filename is correct" || echo "Filename is not correct"

For your second request, try a "group command":

[[ -d "$dir" ]] && echo "Dir exists $dir" || { echo "dir $dir doesnot exists"; exit 0; }

For your first question, what do you mean by "check only a pattern of filename"? A substring?

I usually prefer if-then-else-fi

if [[ -d "$dir" ]]; then echo "Dir exists $dir"; else echo "dir $dir doesnot exists"; exit 0; fi

and think that multi-line is better readable and maintainable.
Problem #2 "test for matching files" can be solved with a file_exists function.

file_exists(){
  for _i do
    [ -f "$_i" ] && return
  done
  return 1
}
if [[ -d "$dir" ]]
then
  echo "Dir exists $dir"
  if file_exists "$dir/$filename"*
  then
    echo "Filename $filename is correct"
  else
    echo "Filename is not correct"
  fi
else
  echo "dir $dir doesnot exists"
  exit 0
fi

In such a structure I can often omit an exit or return .

1 Like

Hello RudiC,
In the directory I will get filename say files-20170105-09.gz. I have to check if file exists also if exists then it should have today's date minus one. So I have to search this part 20170105 . Rest of the part of file , I will not be knowing. That is the reason to check it I was using wildcard *

---------- Post updated at 07:36 AM ---------- Previous update was at 07:34 AM ----------

Hello MIG , let me try it.

Still not clear what you want.

Just guessing: [[ -f '$dir/$filename*' ]] does not supply your filename.
man bash :

You could try to use [ ... ] . But, some caveats are to be considered:
Within single quotes, no variable expansion will be done - use double quotes instead. And, within quotes, the * loses its wildcard meaning - put it outside the quotes. And, make very sure that the expansion yields one single filename only to avoid a bash error.

Does it fly?

1 Like

Thanks MIG, I have applied your function and it is working fine. Could you please explain it. Also what if i simply use below code

if ls 20170108/file* ; then
echo "file is present"
else 
"file is not present"
fi

You can do it with ls -d , but the external program means some overhead, and you must discard stdout and stderr, and there are some border cases if special files would match.
--
The function lets the shell expand the arguments; the for loop cycles through them, tests them, and returns immediately with status 0 ("true") if one is present.