Linux의 Bash 스크립트에서 어떤 파일이 존재하는지 확인하는 방법을 소개합니다.
1. 파일이 존재하는지 확인
디렉토리가 아닌, 일반적인 파일이 존재하는지 확인할 때 -f로 확인할 수 있습니다. File 변수의 파일 주소가 디렉토리라면 False를 리턴합니다.
#!/bin/bash
File=file.txt
if [ -f "$File" ]; then  
  echo "$File exist "  
fiOutput:
$ bash example.sh
file.txt exist1.1 방법 (2)
위의 코드는 아래와 같은 코드와 동일하게 동작합니다. 선호하는 방법을 선택하시면 됩니다.
#!/bin/bash
File=file.txt  
if [[ -f "$File" ]]; then  
  echo "$File exist "  
fi  1.2 방법 (3)
위의 코드는 아래와 같은 코드와 동일하게 동작합니다. 선호하는 방법을 선택하시면 됩니다.
#!/bin/bash
File=file.txt
if test -f "$File"; then  
  echo "$File exist "
fi2. 디렉토리가 존재하는지 확인
일반적인 파일이 아닌 디렉토리가 존재하는지 확인할 때는 -d를 사용합니다. 만약 이 연산자로 일반적인 파일을 체크한다면 False를 리턴합니다.
#!/bin/bash
Dir=mydir
if [ -d "$Dir" ]; then  
  echo "$Dir exist "  
fiOutput:
$ bash example.sh
mydir exist3. 어떤 파일이 존재하는지 확인
디렉토리 또는 파일과 무관하게, 어떤 파일이 존재하는지 확인할 때는 -e를 사용합니다. 이 연산자는 존재하는 디렉토리와 파일에 대해서 모두 True를 리턴합니다.
#!/bin/bash
File=mydir
if [ -e "$File" ]; then  
  echo "$File exist "  
fiOutput:
$ bash example.sh
mydir exist파일 테스트 연산자
-f, -d 외에도, 다양한 파일 테스트 연산자가 있습니다. 다음을 참고하셔서 필요한 상황에 사용하시면 됩니다.
| File Operator | Description | 
|---|---|
| -b File Returns | 'True' if the FILE exists as a block special file. | 
| -c File Returns | 'True' if the FILE exists as a special character file. | 
| -d File Returns | 'True' if the FILE exists as a directory. | 
| -e File Returns | 'True' if the FILE exists as a file, regardless of type (node, directory, socket, etc.). | 
| -f File Returns | 'True' if the FILE exists as a regular file (not a directory or device). | 
| -G File Returns | 'True' if the FILE exists and contains the same group as the user is running the command. | 
| -h File Returns | 'True' if the FILE exists as a symbolic link. | 
| -g File Returns | 'True' if the FILE exists and contains set-group-id (sgid) flag set. | 
| -k File Returns | 'True' if the FILE exists and contains a sticky bit flag set. | 
| -L File Returns | 'True' if the FILE exists as a symbolic link. | 
| -O File Returns | 'True' if the FILE exists and is owned by the user who is running the command. | 
| -p File Returns | 'True' if the FILE exists as a pipe. | 
| -r File Returns | 'True' if the FILE exists as a readable file. | 
| -S File Returns | 'True' if the FILE exists as a socket. | 
| -s File Returns | 'True' if the FILE exists and has nonzero size. | 
| -u File Returns | 'True' if the FILE exists, and set-user-id (suid) flag is set. | 
| -w File Returns | 'True' if the FILE exists as a writable file. | 
| -x File Returns | 'True' if the FILE exists as an executable file. | 
References
Loading script...
Related Posts
- Bash Shell - 문자열에서 첫번째 문자 N개 제거
 - Bash Shell - 문자열에서 마지막 문자 N개 제거
 - Bash Shell - 실행 시간 측정 방법
 - Bash Shell - 나눗셈에서 버림, 올림, 반올림 처리
 - Bash Shell - 숫자가 양수인지 음수인지 확인하는 방법
 - Bash Shell - 자신의 스크립트 파일 이름 가져오기
 - Bash Shell - 문자열 출력 방법(echo, printf)
 - Bash Shell - seq 명령어로 반복문 구현 및 예제
 - Bash Shell - File 테스트 연산자
 - Bash Shell - 무한 루프 (Infinite loop)
 - Bash Shell - 숫자 비교 연산자
 - Bash Shell - 문자열 비교 연산자
 - Bash Shell - 대문자, 소문자로 변환 (Uppercase, Lowercase)
 - Bash Shell - 스트립트를 root로 실행했는지 확인
 - Bash Shell - 명령어 실행 결과를 변수에 저장
 - Bash Shell - select로 선택 메뉴 구현
 - Bash Shell - 변수가 정의되었는지 확인하는 방법
 - Bash Shell - sleep 함수, 특정 시간 멈추기
 - Bash Shell - 파일이 존재하는지 확인
 - Bash Shell - 문자열 자르기 (substring, split)
 - Bash Shell - 날짜, 시간 가져오기
 - Bash Shell - Case 조건문
 - Bash Shell - 파일 읽는 방법
 - Bash Shell - 사용자로부터 입력 받기
 - Bash Shell - 문자열을 연결하는 방법
 - Bash Shell - 파일 생성 및 문자열 추가
 - Bash Shell - 숫자 변수 증가/감소 시키기
 - Bash Shell - 배열 할당, 길이, 객체 포함 여부 확인
 - Bash Shell - 문자열 비교, 문자열 포함 여부 확인
 - Bash Shell - 변수 선언, 할당
 - Bash Shell - 조건문(if-else)
 - Bash Shell - 숫자 연산 (더하기, 빼기, 곱하기, 나누기)
 - Bash Shell - 반복문(for, while, until loop)
 - Bash Shell - Command Line으로 전달된 인자 받기
 - Bash Shell - 함수(Function)