![bash + 여러 조합으로 끝나는 파일이 있는지 확인 [복제]](https://linux55.com/image/206348/bash%20%2B%20%EC%97%AC%EB%9F%AC%20%EC%A1%B0%ED%95%A9%EC%9C%BC%EB%A1%9C%20%EB%81%9D%EB%82%98%EB%8A%94%20%ED%8C%8C%EC%9D%BC%EC%9D%B4%20%EC%9E%88%EB%8A%94%EC%A7%80%20%ED%99%95%EC%9D%B8%20%5B%EB%B3%B5%EC%A0%9C%5D.png)
/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"