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
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).

| improve this answer | |
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
| improve this answer | |
  • Add -type f to not get positive results from directories etc. – Kusalananda Jan 17 '18 at 6:59
  • Yes, @Kusalananda, that is a good point & it is already mentioned in the answer. – John1024 Jan 17 '18 at 7:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.