Technical skill

Linux command: cd

Subject: .../LINUX

Move from one folder to another

Imagine folders as a tree of rooms: a folder can contain smaller folders, and each folder has a path showing how to reach it. The shell, the program that reads commands you type, keeps track of one current folder. The cd command moves that shell to another folder. After the move, a short relative path starts from the new location.

A path follows a branching tree of folders to a file inside a child folder.

Moving to a directory changes which branch of the folder tree the shell uses.

cd must run in the shell you want to move. A separate child program could change only its own folder; it cannot change its parent shell's location. When cd succeeds, later commands use the new location. When it fails, the shell stays where it was and the command returns a nonzero status.

A path beginning with / is absolute, so it starts from the filesystem root. A relative path starts from the current folder. In Bash, cd without an argument goes to the folder named by HOME; cd - returns to the location stored in OLDPWD and prints it. This sequence shows the change:

pwd
cd ~/projects
pwd
cd -

pwd reports the current location. A path may pass through symbolic links, so the name the shell uses can differ from the physical path it reaches.

Bash uses logical paths by default. cd -L path asks it to keep the logical path; cd -P path resolves symbolic links while choosing the destination. pwd -L and pwd -P show logical and physical forms. If ~/current points to /srv/project, cd ~/current may leave PWD containing the alias while pwd -P prints the resolved path. Both names reach the same folder, but a script comparing path strings may treat them differently.

Use cd .. to move to the parent folder and cd / to move to the filesystem root. Quote a path with spaces, as in cd "$HOME/Project Notes", so Bash passes it as one argument. The shell expands a leading ~ to a home folder; the kernel does not treat a tilde inside an arbitrary path as a home-directory marker.

Bash may consult CDPATH when looking for a relative directory name. If it uses a matching CDPATH entry, cd can print the selected path. That output can surprise scripts expecting silence, so use predictable paths and account for the shell's environment. help cd describes Bash's builtin behavior. After a failed cd /somewhere, run pwd and check the path and permissions. A child shell loses its directory change when it exits; the interactive shell's own cd changes the location used by later commands.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Skyeng Senior Backend Developer preview
image descriptionSkyeng Senior Backend Developer
by Artem Naumenko (slack2)
39 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng QA Automation Hard Skillset v09.21 preview
image descriptionSkyeng QA Automation Hard Skillset v09.21
by Aleksandra Kulikova
16 users
Skillset Skyeng SRE preview
image descriptionSkyeng SRE
by Yuliya Marochkina
12 users
Show more
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful