shell script (bash) 에서 file 유무 체크하는 방법과 파일 존재 검사 옵션 (file exist check)
shell script (bash) 에서 file 유무 체크하는 방법과 파일 존재 검사 옵션 (file exist check)
ubuntu linux 에서 shell script 를 사용하다보면 파일의 유무를 확인해야 하는 경우가 자주 있습니다. 이럴 때 사용할 수 있는 방법은 조건문에서 특별한 옵션을 사용하는 것 입니다.
기본 문법은 아래와 같습니다.
if [ -f "$FILENAME" ] ; then
echo "file exist"
else
echo "file not exist"
fi
-b FILE
파일이 존재하고 특별한 파일인지 체크
FILE exists and is block special
-c FILE
파일이 존재하고 특수문자가 있는지 체크
FILE exists and is character special
-d FILE
파일이 존재하고 그 파일이 폴더인지 체크
FILE exists and is a directory
-e FILE
파일이 존재 하는지 체크
FILE exists
-f FILE
파일이 존재하고 보통 파일인지 체크
FILE exists and is a regular file
-g FILE
파일이 존재하고 group ID 로 설정 되었는지 체크
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-h FILE
파일이 존재하고 symbolic link 파일인지 확인
FILE exists and is a symbolic link (same as -L)
-k FILE
FILE exists and has its sticky bit set
-L FILE
파일이 존재하고 symbolic link 파일인지 확인
FILE exists and is a symbolic link (same as -h)
-O FILE
파일이 존재하고 owner 가 유효한지 확인
FILE exists and is owned by the effective user ID
-p FILE
파일이 존재하고 그 파일이 pipe 인지 확인
FILE exists and is a named pipe
-r FILE
파일이 존재하고 read 가능한 파일인지 확인
FILE exists and read permission is granted
-s FILE
파일이 존재하고 0 size 파일이 아닌지 체크
FILE exists and has a size greater than zero
-S FILE
파일이 존재하고 그 파일이 소켓인지 확인
FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
파일이 존재하고 쓰기 가능한 파일인지 확인
FILE exists and write permission is granted
-x FILE
파일이 존재하고 실행 가능한 파일인지 확인
FILE exists and execute (or search) permission is granted
이와 같은 다양한 옵션을 사용하여 해당 파일이 존재하고 사용자가 사용할 수 있는 파일인지 까지 체크할 수 있습니다.
파일의 유무 확인은 매우 자주 사용되는 것이니 기본적인 옵션은 외워두고 사용해도 좋을 것 같네요. 그래서 다시한번 적어봅니다.
-f : 파일 유무 및 보통파일 확인
-d : 디렉토리 유무 확인
-e : 파일과 폴더를 가리지 않고 존재 하는지 확인
이렇게 세가지는 매우 자주 사용하는 것이니 꼭 외워두어야 겠습니다.
이상 shell script (bash) 에서 file 유무 체크하는 방법과 옵션 (file exist check)에 대한 글 이였습니다.