본문 바로가기

리눅스

mkdir 명령어

728x90

mkdir 명령어

mkdir 명령어는 Linux 및 UNIX 기반 시스템에서 디렉토리(Directory)를 생성하는 데 사용되는 명령어입니다. mkdir은 Make Directory의 줄임말입니다. 디렉토리는 파일을 그룹화하고 구조화하는 데 사용되며, 파일 시스템 내에서 계층적인 구조를 형성합니다.

 

mkdir 명령어의 기본적인 구문은 다음과 같습니다.

mkdir [옵션] 디렉토리명

일반적으로 사용되는 주요 옵션은 다음과 같습니다.

 

  • -p: 지정된 경로에 디렉토리가 없는 경우, 중간 경로에 존재하지 않는 디렉토리도 함께 생성합니다. 이 옵션을 사용하여 중첩된 디렉토리를 한 번에 생성할 수 있습니다.

예를 들어, docs라는 디렉토리를 생성하려면 다음과 같이 사용합니다.

mkdir docs

docs 디렉토리가 현재 위치에 생성됩니다.

 

또는 -p 옵션을 사용하여 data 디렉토리 안에 2023과 07과 sales라는 중첩된 디렉토리를 한 번에 생성하려면 다음과 같이 사용합니다.

mkdir -p data/2023/07/sales

위 명령을 실행하면 data 디렉토리 안에 2023 디렉토리가 생성되고, 2023 디렉토리 안에 07 디렉토리가 생성되며, 07 디렉토리 안에 sales 디렉토리가 생성됩니다.

 

디렉토리 생성이 완료되면 해당 디렉토리 안에 파일을 저장하거나 서브 디렉토리를 생성하는 등의 작업을 할 수 있습니다.

728x90

 

디렉터리 생성

mkdir tmp-directory
  • 여러 단계의 디렉토리 생성
    • -p (--parents) 옵션을 사용하여 여러 단계의 디렉터리를 생성할 수 있습니다.
$ mkdir tmp/directory1/directory2/directory3
mkdir: `tmp/directory1/directory2/directory3' 디렉토리를 만들 수 없습니다: 그런 파일이나 디렉터리가 없습니다
mkdir -p tmp/directory1/directory2/directory3
  • 권한 설정
    • -m (--mode=mode) 옵션을 사용하여 생성된 디렉토리에 대한 파일 모드(권한 등)를 설정할 수 있습니다.
mkdir -m 777 -p tmp/directory1/directory2/directory3
$ ls -l tmp/directory1/directory2
합계 0
drwxrwxr-x 2 root root 6  1월 11 17:04 directory3

mkdir 옵션

$ mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
  -v, --verbose     print a message for each created directory
  -Z                   set SELinux security context of each created directory
                         to the default type
      --context[=CTX]  like -Z, or if CTX is specified then set the SELinux
                         or SMACK security context to CTX
      --help     이 도움말을 표시하고 끝냅니다
      --version  버전 정보를 출력하고 끝냅니다

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report mkdir translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'mkdir invocation'

 

728x90