IT 정보/Linux
[리눅스/쉘스크립트] 파일 또는 디렉터리 존재 유무 확인하기
린아저씨
2020. 1. 30. 14:13
1. 파일의 존재 여부 확인하기
test 명령어 -e 옵션 사용 /// test 명령어를 대괄호[] 로 대체 가능
확인할 파일 : /tmp/my_test_file.txt
#!/bin/bash
file_path='my_test_file.txt'
#파일이 있을 경우 메시지 출력
if test -e $file_path; then
echo "$file_path : Found the file."
fi
-------------------------------
#파일이 없을 경우 메시지 출력
if ! test -e $file_path; then
echo "$file_path : Not found the file."
fi
-------------------------------
#대괄호[] 사용
if [ -e $file_path ]; then
echo "$file_path : Found the file."
fi
2) 디렉터리 존재 여부 확인하기
test 명령어 -d 옵션 사용 /// test 명령어를 대괄호[] 로 대체 가능
확인할 디렉터리 : /tmp/my_test_dir
#!/bin/bash
dir_path='my_test_dir'
#파일이 있을 경우 메시지 출력
if test -d $dir_path; then
echo "$dir_path : Found the directory
fi
-------------------------------
#파일이 없을 경우 메시지 출력
if ! test -d $dir_path; then
echo "$dir_path : Not found the directory."
fi
-------------------------------
#대괄호[] 사용
if [ -d $dir_path ]; then
echo "$dir_path : Found the directory."
fi