0

the way to check file as all know is like this

[[ -f  /var/scripts_home/orig_create_DB_files_1.46.38 ]] && echo file exist

but how to check if file exist in case file contain name as - "create_DB_files"

I try this ( but not works )

[[ -f  /var/scripts_home/*create_DB_files* ]] && echo file exist

or

   partial_file_name=create_DB_files

   [[ -f  /var/scripts_home/*$partial_file_name* ]] && echo file exist
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
yael
  • 12,598
  • 51
  • 169
  • 303

2 Answers2

2
for name in *create_DB_files*; do
    if [ -f "$name" ]; then
        printf 'at least one file exists (%s)\n' "$name"
        break
    fi
done

That is, match the relevant names and check if any of them is a regular file (the loop will exit as soon as one is found).

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
2

This will test whether a file exists based on a partial name with all the flexibility for finding files that find allows:

find . -name '*create_DB_files*' -printf 1 -quit | grep -q 1

One might want to consider adding -type f to restrict matches to regular files or -mtime if one wants to match on file date, or -maxdepth 1 to restrict the search to the current directory, etc.

The above can be incorporated in an if command as follows:

if find . -name '*create_DB_files*' -printf 1 -quit | grep -q 1
then
    echo found
else
    echo Not found
fi
John1024
  • 73,527
  • 11
  • 167
  • 163