Computer >> 컴퓨터 >  >> 프로그래밍 >> Bash 프로그래밍

리눅스 cd 명령어 완벽 가이드: 셸에서 디렉토리 변경하는 방법

이 튜토리얼에서는 리눅스 셸에서 현재 작업 중인 디렉토리를 이동할 때 사용하는 cd(change directory) 명령어의 사용법을 자세히 알아봅니다.

앞서 ls 명령어로 디렉토리 내용을 살펴보는 방법을 다뤘다면, 이제는 실제로 디렉토리 사이를 자유롭게 이동하는 방법을 익힐 차례입니다. cd 명령어를 사용하면 아주 간단하게 디렉토리를 탐색할 수 있습니다.

cd 명령어의 도움말 확인하기

cd처럼 기본으로 내장된 유틸리티에 대해 더 알아보려면 어떻게 해야 할까요? 보통 매뉴얼 페이지를 볼 때 다음과 같이 입력해 봤을 겁니다.

man cd

하지만 대부분 "No manual entry for cd"라는 메시지가 출력됩니다. 그 이유는 cd가 외부 프로그램이 아니라 셸(bash)에 내장된 명령어이기 때문입니다. 내장 명령어의 도움말을 보려면 help를 사용해야 합니다.

help cd

cd 명령어 문법(Syntax)

help 유틸리티를 실행하면 다음과 같은 출력 결과를 확인할 수 있습니다.

cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.

Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.

The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.

If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.

Options:
-L force symbolic links to be followed: resolve symbolic
links in DIR after processing instances of `..'
-P use the physical directory structure without following
symbolic links: resolve symbolic links in DIR before
processing instances of `..'
-e if the -P option is supplied, and the current working
directory cannot be determined successfully, exit with
a non-zero status
-@ on systems that support it, present a file with extended
attributes as a directory containing the file attributes

The default is to follow symbolic links, as if `-L' were specified.
`..' is processed by removing the immediately previous pathname component
back to a slash or the beginning of DIR.

Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.

핵심만 요약하면 다음과 같습니다.

  • -L: 심볼릭 링크를 따라갑니다(기본값). `..' 처리 후 심볼릭 링크를 해석합니다.
  • -P: 심볼릭 링크를 따라가지 않고 물리적 디렉토리 구조를 사용합니다.
  • -e: -P 옵션과 함께 사용되며, 현재 작업 디렉토리를 확인하지 못하면 0이 아닌 상태 코드로 종료합니다.
  • 디렉토리 변경에 성공하면 종료 코드 0을 반환하고, 실패하면 0이 아닌 값을 반환합니다.

솔직히 말하면, cd 도움말 전체를 끝까지 읽어본 것은 이번이 처음일 정도로 대부분의 사용자에게는 기본적인 사용법만 알아도 충분합니다. 지금부터 가장 많이 쓰이는 핵심 사용법들을 살펴보겠습니다.

cd ~ : 홈 디렉토리로 이동

물결표(~)는 현재 사용자의 홈 디렉토리를 가리키는 특수 문자입니다. cd ~를 입력하면 어디에 있든 바로 홈 디렉토리로 이동합니다. 홈 디렉토리 경로가 궁금하다면 다음 명령어로 확인할 수 있습니다.

echo $HOME

이 명령은 홈 디렉토리의 절대 경로를 화면에 출력해 줍니다. 참고로 cd만 인자 없이 입력해도 홈 디렉토리로 이동합니다.

cd .. : 상위 디렉토리로 이동

..(점 두 개) 역시 특수한 참조값으로, 현재 작업 디렉토리의 부모(상위) 디렉토리를 의미합니다. cd ..를 입력하면 한 단계 위 디렉토리로 이동합니다.

두 단계 이상 올라가고 싶다면 슬래시로 연결해서 입력하면 됩니다.

cd ../..

위 명령은 상위 디렉토리로 두 번 연속 이동합니다. 필요에 따라 cd ../../..처럼 계층 수를 늘릴 수도 있습니다.

cd / : 루트 디렉토리로 이동

슬래시(/)는 리눅스 파일 시스템의 최상위인 루트(root) 디렉토리를 나타냅니다. cd /를 입력하면 시스템 전체 트리의 시작점으로 이동합니다. 단, 해당 위치에 접근할 수 있는 권한이 있어야 합니다.

마무리

지금까지 리눅스 셸에서 디렉토리를 이동하는 cd 명령어의 핵심 사용법을 알아봤습니다. ~(홈 디렉토리), ..(상위 디렉토리), /(루트 디렉토리) 세 가지만 기억해도 터미널에서 원활하게 탐색할 수 있습니다. 간단하지만 매일 쓰게 되는 필수 팁이니 꼭 숙지해 두세요!