?

단축키

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
번호 분류 제목 날짜 조회 수 추천 수
341 일반 네트워크 특정 IP 작업그룹/컴퓨터명 정보 조회 1 2015.02.10 10194 0
340 PHP PHP GD 설명 2014.07.09 10275 0
339 C# Application 클래스 2013.09.25 10281 0
338 LINUX 리눅스 시스템 시작시에 자동 실행할 파일 등록하기 2015.09.30 10292 0
337 LINUX shutdown 명령어 2013.07.26 10330 0
336 Android adb로 display 해상도 조정 2015.04.22 10423 0
335 Android logcat, main+kernel로그 합치는 로그 2014.10.07 10434 0
334 LINUX 리눅스에서 특정작업 or 명령어 반복하기 Crontab 2014.11.10 10448 0
333 Pi 아두이노 Arduino String Class 2017.07.16 10492 0
332 LINUX 리눅스 부팅시 명령스크립트 실행하기 2015.09.30 10554 0
331 PHP move_uploaded_file() 2014.04.29 10562 0
330 Pi 파이썬 코드를 이용한 파이카메라 제어 2018.04.24 10676 0
329 Pi 키르히호프의 법칙 2016.12.05 10726 0
328 LINUX OpenGrok 설치 빨리하기 2016.04.27 10734 0
327 Python wxPython Tutorial 링크 2014.04.24 10745 0
목록
Board Pagination ‹ Prev 1 ... 7 8 9 10 11 12 13 14 15 16 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5