본문 바로가기
Script/Linux Bash

[Bash] 디렉토리/파일 없을 경우 생성하기

by 게킴의 블로그 2022. 12. 15.
 
1. 디렉토리가 없을 경우 생성 (-d 옵션)
#!/bin/bash

# Test 디랙토리 변수 지정
Testdir="/etc/Testdir"

# TestDir 디렉토리가 경로에 없을 경우 mkdir 명령어로 생성
if [ ! -d $TestDir ]; then
	mkdir -p $TestDir
else
	echo "Directory exists"
fi​

 

2. 파일이 없을 경우 생성 (-e 옵션)

#!/bin/bash

# Test 디랙토리 변수 지정
Testfile="/etc/Testfile"

# TestDir 디렉토리가 경로에 없을 경우 touch 명령어로 생성
if [ ! -e $Testfile ]; then
	touch $Testfile
else
	echo "File exists"
fi​

최근댓글

최근글

skin by © 2024 ttuttak