code

존재하지 않는 경우에만 mkdir

starcafe 2023. 5. 23. 22:04
반응형

존재하지 않는 경우에만 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

-p 경우 생성을(를) 사용할 수 있습니다.directory하지 않습니다.

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, --부모 오류 없음, 필요에 따라 부모 디렉토리 작성

사용해 보십시오.

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

반응형