cdChange the current working directory
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.
cd [-L | -P] [DIRECTORY] cd - cdYou are checking logs and configs on a server and need to hop between two directories quickly.
cdA shell builtin: the shell itself calls chdir(). A separate program could never change your shell's directory.
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).
cdWith no argument: go to your home directory ($HOME)cd -Go back to the previous directory ($OLDPWD) and print itcd ..Up one level; cd ../.. goes up twocd ~/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 linkCDPATH=/opt/appExtra base directories searched for relative cd targetscd dir || exit 1Script idiom: stop if the directory change failedcd /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 /rootcd 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 /rootcd My DocumentsThe shell splits on spaces, so cd gets two arguments and bash says 'cd: too many arguments'.
cd "My Documents" or cd My\ DocumentsWriting a script that only does cd somewhere and expecting your terminal to moveThe script runs in a child bash process; its cd dies with it.
source ./goto.sh, or define a function/alias in ~/.bashrcTry these on a Linux machine of your own. Each task has a hint and a solution.
Go to /etc/app, then jump back to your previous directory with one short command, then back again.
From ~/app/src, get to ~/data using only a relative path (no ~ and no leading /).
~/applogs is a symlink. cd into it and print both the logical and the physical path.
Try to cd into ~/shared/reports. Explain the error, then list it with root rights without opening a root shell.
cdChange the current working directory
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.
cd [-L | -P] [DIRECTORY] cd - cdYou are checking logs and configs on a server and need to hop between two directories quickly.
cdA shell builtin: the shell itself calls chdir(). A separate program could never change your shell's directory.
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).
cdWith no argument: go to your home directory ($HOME)cd -Go back to the previous directory ($OLDPWD) and print itcd ..Up one level; cd ../.. goes up twocd ~/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 linkCDPATH=/opt/appExtra base directories searched for relative cd targetscd dir || exit 1Script idiom: stop if the directory change failedcd /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 /rootcd 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 /rootcd My DocumentsThe shell splits on spaces, so cd gets two arguments and bash says 'cd: too many arguments'.
cd "My Documents" or cd My\ DocumentsWriting a script that only does cd somewhere and expecting your terminal to moveThe script runs in a child bash process; its cd dies with it.
source ./goto.sh, or define a function/alias in ~/.bashrcTry these on a Linux machine of your own. Each task has a hint and a solution.
Go to /etc/app, then jump back to your previous directory with one short command, then back again.
From ~/app/src, get to ~/data using only a relative path (no ~ and no leading /).
~/applogs is a symlink. cd into it and print both the logical and the physical path.
Try to cd into ~/shared/reports. Explain the error, then list it with root rights without opening a root shell.