programing tip

존재하지 않는 경우에만 mkdir

itbloger 2020. 9. 13. 10:13
반응형

존재하지 않는 경우에만 mkdir [중복]


이 질문에 이미 답변이 있습니다.

내 bash 스크립트에서 다음을 수행합니다.

mkdir product;

스크립트를 두 번 이상 실행하면 다음과 같은 결과가 나타납니다.

mkdir: product: File exists

콘솔에서.

그래서 dir이 존재하지 않는 경우에만 mkdir을 실행하려고합니다. 이게 가능해?


테스트하기

[[ -d dir ]] || mkdir dir

또는 -p 옵션을 사용하십시오.

mkdir -p dir

if [ ! -d directory ]; then
  mkdir directory
fi

또는

mkdir -p directory

-pdirectory존재하지 않는 경우 생성 보장


mkdir의 -p옵션을 사용 하지만 다른 효과도 있습니다.

 -p      Create intermediate directories as required.  If this option is not specified, the full path prefix of each oper-
         and must already exist.  On the other hand, with this option specified, no error will be reported if a directory
         given as an operand already exists.  Intermediate directories are created with permission bits of rwxrwxrwx
         (0777) as modified by the current umask, plus write and search permission for the owner.

mkdir -p

-p, --parents 존재하는 경우 오류 없음, 필요에 따라 상위 디렉토리 만들기


이것을 사용해보십시오 :-

mkdir -p dir;

참고 :- 존재하지 않는 중간 디렉터리도 생성됩니다. 예를 들어

mkdir -p 확인

또는 이것을 시도하십시오 :-

if [[ ! -e $dir ]]; then
    mkdir $dir
elif [[ ! -d $dir ]]; then
    echo "$Message" 1>&2
fi

참고 URL : https://stackoverflow.com/questions/18622907/only-mkdir-if-it-does-not-exist

반응형