?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print


http://foris.tistory.com/256



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 : 파일과 폴더를 가리지 않고 존재 하는지 확인


이렇게 세가지는 매우 자주 사용.


Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
476 일반 PROMPT Example secret 2023.05.22 0 0
475 Android Flutter 문법 정리 secret 2023.09.24 0 0
474 Android 원격 linux 서버에서 local device로 adb 접속하기 secret 2015.11.05 1 0
473 Pi 회로부품 메모 1 secret 2019.08.09 1 0
472 Android [GIT 사용법] Dreamy의 요약 2 secret 2012.04.20 158 0
471 Android 대화상자 만들기 메모 2023.11.01 1079 0
470 PHP [MySQL] 뷰 생성하기(VIEW 생성하기) 2022.10.12 1415 0
469 PHP sQL:: JOIN, UNION 사용법 2022.09.05 1528 0
468 HTML5 TypeScript 문법 정리 2023.01.13 1546 0
467 JAVA [JAVA] 람다식 기본 예제 1 (map, filter, reduce, collect) 2022.11.04 1700 0
466 HTML5 [TypeORM] TypeORM CreateQueryBuilder 2023.01.13 1729 0
465 HTML5 typescript 기본문법 정리 2023.01.13 1735 0
464 PHP SQL :: EXIST , NOT EXIST 2022.09.05 1863 0
463 업무 Back-End 관련 정보 2022.05.17 2049 0
462 일반 3D프린트 가이드 모음 2022.04.02 2740 0
목록
Board Pagination ‹ Prev 1 2 3 4 5 6 7 8 9 10 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5