3

how to check identical files name as prefix present or not in shell script i.e. MY_REPORT_2018_04_23_01.txt, MY_REPORT_2018_04_23_02.txt, MY_REPORT_2018_04_13_03.txt etc.

when i am trying:

if [ ! -e MY_REPORT_*.txt ] ;
 then
        echo "files not present in current directory"
        exit 1
 fi

It's working but with a warning message:

[: too many arguments

codeforester
  • 39,467
  • 16
  • 112
  • 140

1 Answers1

2

Use an array:

#!/usr/bin/env bash
shopt -s nullglob               # make sure glob evaluates to nothing if there are no matches
files=(MY_REPORT_*.txt)         # create an array of matching files
if ((${#files[@]} == 0)); then  # check number of items in array
  echo "Files not present"
  exit 1
fi

Check this post to understand how [ ... ] works:

Another related post:

codeforester
  • 39,467
  • 16
  • 112
  • 140