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
  • 67,283
  • 35
  • 116
  • 255
yael
  • 13,106

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
  • 333,661
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
  • 74,655