CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File & Directory Management / cd

cd

Change the current working directory

Beginneressentialnavigationshell builtin

cd moves your shell into another directory, so relative paths and every command you start from then on work from there. It is a shell builtin, not a program, and it has a few shortcuts (~, -, ..) you will use hundreds of times a day.

Syntaxcd [-L | -P] [DIRECTORY] cd - cd

Watch it run

You are checking logs and configs on a server and need to hop between two directories quickly.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 6
pwd

You start in your home directory after login. pwd shows the full path; the prompt shows it shortened to ~.

Anatomy of a command

$
builtincd

A shell builtin: the shell itself calls chdir(). A separate program could never change your shell's directory.

How it works

Every process has a current working directory, held by the kernel as a reference to a directory inode. Relative paths (logs/app.log, ../etc) are resolved from it. cd must be built into the shell because it changes the shell's own process: it calls the chdir() system call, and a child program could only change its own directory, not its parent's. The kernel walks the path one component at a time, checking that each exists, is a directory, and that you have execute (search) permission on it. Read permission is not needed to enter, only to list. On success bash sets OLDPWD to the old value and PWD to the new one. By default bash keeps the logical path, so cd into a symlink shows the link name and cd .. goes back up the link; -P resolves to the physical directory. Children started afterwards inherit the directory (visible in /proc/PID/cwd).

Options you'll actually use

cdWith no argument: go to your home directory ($HOME)
cd -Go back to the previous directory ($OLDPWD) and print it
cd ..Up one level; cd ../.. goes up two
cd ~/dirPath under your home; ~user means another user's home
-PPhysical: resolve symlinks, land in the real directory
-LLogical (default): keep symlink names in $PWD, .. undoes the link
CDPATH=/opt/appExtra base directories searched for relative cd targets
cd dir || exit 1Script idiom: stop if the directory change failed

Common mistakes

cd /var/www/old-site; rm -rf *

If cd fails (typo, missing directory), the ; still runs rm -rf * in whatever directory you were in, often your home or /.

cd /var/www/old-site && rm -rf ./* (or in scripts: cd "$dir" || exit 1)
sudo cd /root

cd is a shell builtin, so sudo can't run it ('sudo: cd: command not found'), and a child process could not change your shell's directory anyway.

sudo -i (root shell), or run the command directly: sudo ls /root
cd My Documents

The shell splits on spaces, so cd gets two arguments and bash says 'cd: too many arguments'.

cd "My Documents" or cd My\ Documents
Writing a script that only does cd somewhere and expecting your terminal to move

The script runs in a child bash process; its cd dies with it.

source ./goto.sh, or define a function/alias in ~/.bashrc

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Go to /etc/app, then jump back to your previous directory with one short command, then back again.

  2. 2

    From ~/app/src, get to ~/data using only a relative path (no ~ and no leading /).

  3. 3

    ~/applogs is a symlink. cd into it and print both the logical and the physical path.

  4. 4

    Try to cd into ~/shared/reports. Explain the error, then list it with root rights without opening a root shell.

Related commands

pwdPrint the current working directorylsList directory contentslnCreate hard links and symbolic linkssourceRun a file's commands in the current shell
PreviouslsNextpwd
Linux / File & Directory Management / cd

cd

Change the current working directory

Beginneressentialnavigationshell builtin

cd moves your shell into another directory, so relative paths and every command you start from then on work from there. It is a shell builtin, not a program, and it has a few shortcuts (~, -, ..) you will use hundreds of times a day.

Syntaxcd [-L | -P] [DIRECTORY] cd - cd

Watch it run

You are checking logs and configs on a server and need to hop between two directories quickly.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 6
pwd

You start in your home directory after login. pwd shows the full path; the prompt shows it shortened to ~.

Anatomy of a command

$
builtincd

A shell builtin: the shell itself calls chdir(). A separate program could never change your shell's directory.

How it works

Every process has a current working directory, held by the kernel as a reference to a directory inode. Relative paths (logs/app.log, ../etc) are resolved from it. cd must be built into the shell because it changes the shell's own process: it calls the chdir() system call, and a child program could only change its own directory, not its parent's. The kernel walks the path one component at a time, checking that each exists, is a directory, and that you have execute (search) permission on it. Read permission is not needed to enter, only to list. On success bash sets OLDPWD to the old value and PWD to the new one. By default bash keeps the logical path, so cd into a symlink shows the link name and cd .. goes back up the link; -P resolves to the physical directory. Children started afterwards inherit the directory (visible in /proc/PID/cwd).

Options you'll actually use

cdWith no argument: go to your home directory ($HOME)
cd -Go back to the previous directory ($OLDPWD) and print it
cd ..Up one level; cd ../.. goes up two
cd ~/dirPath under your home; ~user means another user's home
-PPhysical: resolve symlinks, land in the real directory
-LLogical (default): keep symlink names in $PWD, .. undoes the link
CDPATH=/opt/appExtra base directories searched for relative cd targets
cd dir || exit 1Script idiom: stop if the directory change failed

Common mistakes

cd /var/www/old-site; rm -rf *

If cd fails (typo, missing directory), the ; still runs rm -rf * in whatever directory you were in, often your home or /.

cd /var/www/old-site && rm -rf ./* (or in scripts: cd "$dir" || exit 1)
sudo cd /root

cd is a shell builtin, so sudo can't run it ('sudo: cd: command not found'), and a child process could not change your shell's directory anyway.

sudo -i (root shell), or run the command directly: sudo ls /root
cd My Documents

The shell splits on spaces, so cd gets two arguments and bash says 'cd: too many arguments'.

cd "My Documents" or cd My\ Documents
Writing a script that only does cd somewhere and expecting your terminal to move

The script runs in a child bash process; its cd dies with it.

source ./goto.sh, or define a function/alias in ~/.bashrc

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Go to /etc/app, then jump back to your previous directory with one short command, then back again.

  2. 2

    From ~/app/src, get to ~/data using only a relative path (no ~ and no leading /).

  3. 3

    ~/applogs is a symlink. cd into it and print both the logical and the physical path.

  4. 4

    Try to cd into ~/shared/reports. Explain the error, then list it with root rights without opening a root shell.

Related commands

pwdPrint the current working directorylsList directory contentslnCreate hard links and symbolic linkssourceRun a file's commands in the current shell
PreviouslsNextpwd