리눅스 디렉터리 관리 기본 명령어 (pwd, cd, mkdir, rmdir)

주영·2023년 9월 13일
0

Linux Master

목록 보기
9/11
post-thumbnail

디렉터리 관리 명령어 (pwd, cd, mkdir, rmdir)

1. pwd

  • print working directory
  • 현재 작업 디렉터리의 이름 출력
  • 옵션
    • -P: 심볼릭 링크 없이 실제 디렉터리를 출력 (Physical)
# pwd --help
pwd: pwd [-LP]
    Print the name of the current working directory.

    Options:
      -L        print the value of $PWD if it names the current working
                directory
      -P        print the physical directory, without any symbolic links
  • 예시
    [root@master1 ~]# pwd
    /root

2. cd

  • change direcory
  • 쉘 작업 디렉터리 변경
cd [상대경로|절대경로][설정경로]
  • 절대 경로: 시작 위치와 상관 없이 경로에 모든 디렉터리를 표시, /(루트)에서부터 시작
  • 상대 경로: 현재 작업 중인 디렉터리를 기준으로 표시하는 경로
  • 설정 경로
    • ~ (틸다): 현재 사용자의 홈 디렉터리
    • . (점 한개): 현재 디렉터리
    • .. (점 두개): 현재에서 한 단계 상위 디렉터리
    • / : 루트 디렉터리
  • 옵션
    • -P: 심볼릭 링크를 따르지 않고 물리적 디렉토리 구조 사용 (Physical)
# cd --help
cd: cd [-L|[-P [-e]] [-@]] [dir]
    Change the shell working directory.
    ...
    Options:
      -P        use the physical directory structure without following
                symbolic links: resolve symbolic links in DIR before
                processing instances of `..'
      ...
  • 예시

    // 현재 사용자의 홈 디렉터리로 이동
    [root@master1 ~]# cd ~
    [root@master1 ~]# pwd
    /root
    
    // 현재 디렉터리로 이동
    [root@master1 ~]# cd .
    [root@master1 ~]# pwd
    /root
    
    // 상위 디렉터리로 이동
    [root@master1 ~]# cd ..
    [root@master1 /]# pwd
    /
    
    // 루트 디렉터리로 이동
    [root@master1 /]# cd /
    [root@master1 /]# pwd
    /

3. mkdir

  • make directory
  • 디렉터리 생성
  • 옵션
    • -m: 디렉터리 생성 시 권한 설정(defualt: 755) (mode)
    • -p: 상위 디렉터리 생성. 존재하는 경우 오류가 없음(parents)
    • -v: 생성된 각 디렉토리에 대한 메시지를 출력(verbose)
# mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

  -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
  • 예시

    // -m 옵션
    [root@localhost juyoung]# mkdir test1
    [root@localhost juyoung]# ll
    total 4
    drwxr-xr-x. 2 root root 4096 Sep 13 21:39 test1   // default 권한인 755로 생성된 디렉터리
    
    [root@localhost juyoung]# mkdir -m 700 test2
    [root@localhost juyoung]# ll
    total 8
    drwxr-xr-x. 2 root root 4096 Sep 13 21:39 test1
    drwx------. 2 root root 4096 Sep 13 21:39 test2   // -m 옵션을 통해 권한이 700으로 생성된 디렉터리
    
    // -p 옵션
    [root@localhost ~]# ls   // 디렉터리가 없는 상태
    [root@localhost ~]# mkdir -p juyoung/test   // test의 상위 디렉터리인 juyoung이라는 디렉터리가 없을 경우 함께 생성
    [root@localhost ~]# ls
    juyoung
    [root@localhost ~]# cd juyoung/
    [root@localhost juyoung]# ls
    test
    
    // -v 옵션
    [root@localhost juyoung]# mkdir -v test2
    mkdir: created directory 'test2'
    [root@localhost juyoung]# ls
    test2

4. rmdir

  • remove directory
  • 비어 있는 경우 디렉터리를 제거
  • 디렉터리 안에 파일이 존재하는 경우 삭제되지 않는다.
  • 옵션
    • -P: 디렉터리와 상위 디렉터리 제거(parents)
    • -v: 처리된 모든 디렉터리에 대한 메시지 출력(verbose)
# rmdir --help
Usage: rmdir [OPTION]... DIRECTORY...
Remove the DIRECTORY(ies), if they are empty.

  -p, --parents   remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is
                    similar to 'rmdir a/b/c a/b a'
  -v, --verbose   output a diagnostic for every directory processed
  • 예시
// 비어있는 디렉터리 제거
[root@localhost juyoung]# ls
test  test2
[root@localhost juyoung]# rmdir test2

// 파일이 존재하는 디렉터리 제거 시 실패
[root@localhost juyoung]# rmdir test3
rmdir: failed to remove 'test3': Directory not empty

// -v 옵션
[root@localhost juyoung]# rmdir -v test3
rmdir: removing directory, 'test3'

참고: 2021 이기적 리눅스마스터 2급

0개의 댓글

관련 채용 정보