bash + 複数の組み合わせで終わるファイルがあることを確認する [複製]

bash + 複数の組み合わせで終わるファイルがあることを確認する [複製]

/tmp/file.1私たちは、または/tmp/file.43.434背中/tmp/file-hegfegfを持つことができます。

それでは、bashで存在をどのように確認しますか/tmp/file*

私たちは次のように努力します

[[ -f "/tmp/file*" ]] && echo "file exists" 

しかし、上記の方法は機能しません。

どうすれば修正できますか?

ベストアンサー1

findこのケースを識別するためにorループを使用しますfor

例#1 find(GNU拡張を使用した検索スペースの制限):

# First try with no matching files
[ -n "$(find /tmp/file* -maxdepth 1 -type f -print -quit)" ] && echo yes || echo no    # "no"

# Create some matching files and try the same command once more
touch /tmp/file.1 /tmp/file.43.434 /tmp/file-hegfegf
[ -n "$(find /tmp/file* -maxdepth 1 -type f -print -quit)" ] && echo yes || echo no    # "yes"

forループ付きの例#2

found=
for file in /tmp/file*
do
    [ -f "$file" ] && found=yes && break
done
[ yes = "$found" ] && echo yes || echo no    # No files "no", otherwise "yes"

おすすめ記事