0
0
Linux CLIscripting~15 mins

cd (change directory) in Linux CLI - Deep Dive

Choose your learning style9 modes available
Overview - cd (change directory)
What is it?
The cd command in Linux is used to change the current working directory in the command line interface. It lets you move between folders so you can work with files in different locations. Without cd, you would be stuck in one place and unable to access other directories easily. It is a basic but essential command for navigating the file system.
Why it matters
Without the ability to change directories, users would have to specify full paths for every file operation, making command line work slow and error-prone. cd solves this by letting you move your focus to the folder you want to work in, just like walking into a different room to find what you need. This makes managing files and running scripts much faster and more intuitive.
Where it fits
Before learning cd, you should understand what a directory (folder) is and how the file system is organized. After mastering cd, you can learn related commands like ls (list files), pwd (print working directory), and how to use relative and absolute paths effectively.
Mental Model
Core Idea
cd changes your current location in the file system so commands run in the right folder.
Think of it like...
Using cd is like walking through rooms in a house to reach the one where you want to work or find something.
Current directory: /home/user
  |
  +-- cd documents --> /home/user/documents
  |
  +-- cd ..        --> /home/user (go up one level)
  |
  +-- cd /etc      --> /etc (go to absolute path)
Build-Up - 7 Steps
1
FoundationUnderstanding the current directory
🤔
Concept: Learn what the current directory means and how to see it.
When you open a terminal, you start in a folder called the current directory. You can see where you are by typing pwd, which prints the full path of this folder. For example, it might show /home/user.
Result
/home/user
Knowing your starting point is essential because cd moves you relative to this location.
2
FoundationBasic cd usage with folder names
🤔
Concept: Use cd followed by a folder name to move into that folder.
If you want to go into a folder inside your current directory, type cd foldername. For example, cd documents moves you into the documents folder inside your current directory.
Result
Now your current directory is /home/user/documents
This simple step lets you navigate deeper into the file system one folder at a time.
3
IntermediateUsing cd with relative paths
🤔Before reading on: do you think 'cd ../folder' moves you up one level then into 'folder', or directly into a sibling folder?
Concept: cd can use relative paths like .. to move up or ./ to stay in the same folder.
The .. means 'go up one directory'. So cd .. moves you up one level. You can combine this with folder names like cd ../folder to go up one level then into 'folder'. The ./ means 'current directory' and is often optional.
Result
If you were in /home/user/documents, cd ../pictures moves you to /home/user/pictures
Understanding relative paths lets you move around efficiently without typing full paths.
4
IntermediateUsing cd with absolute paths
🤔Before reading on: does cd /etc always take you to the same place regardless of your current directory?
Concept: Absolute paths start from the root / and specify the full location.
If you type cd /etc, you go directly to the /etc folder no matter where you are. Absolute paths always start with / and show the full route from the root folder.
Result
Your current directory becomes /etc
Absolute paths give you a shortcut to any folder without depending on your current location.
5
IntermediateSpecial shortcuts: ~ and -
🤔Before reading on: does cd ~ always take you home, and cd - take you to the previous directory?
Concept: The ~ symbol means your home folder, and - means the last directory you were in.
Typing cd ~ takes you to your home directory, like /home/user. Typing cd - switches you back to the previous directory you were in, which is useful to toggle between two folders.
Result
cd ~ moves to /home/user; cd - moves back to the last directory
These shortcuts save time and keystrokes when moving around common locations.
6
AdvancedUsing cd in scripts and automation
🤔Before reading on: do you think cd affects only the script or the whole terminal session when used in a script?
Concept: cd changes the working directory for commands that follow, but in scripts, its effect depends on how the script is run.
In a script, cd changes the directory for commands inside that script. But once the script ends, your terminal stays where it was before. To change your terminal's directory permanently, you must run cd directly in the shell, not in a separate script process.
Result
Scripts can run commands in different folders without changing your terminal's location.
Knowing how cd behaves in scripts prevents confusion about where commands run and where you end up.
7
ExpertHow cd affects environment and subshells
🤔Before reading on: does cd in a subshell affect the parent shell's directory?
Concept: cd changes the directory only in the current shell process; subshells have their own directory state.
When you run a command in a subshell (like in parentheses), cd changes the directory only inside that subshell. The parent shell remains in its original directory. This isolation helps scripts run safely without side effects.
Result
cd inside subshells does not change your main shell's directory.
Understanding shell processes and their environments explains why cd sometimes seems to 'not work' as expected.
Under the Hood
cd is a shell builtin command that changes the shell's internal current working directory variable. The shell tracks this directory to resolve relative paths for commands. When you type cd, the shell updates this variable and the prompt reflects the new location. External programs inherit this directory as their starting point. Because cd changes the shell's state, it must be built into the shell rather than an external program.
Why designed this way?
cd was designed as a builtin because changing directories affects the shell's environment, which external programs cannot modify for the parent process. This design ensures efficient navigation without spawning new processes. Alternatives like external cd commands would fail to change the shell's directory, making navigation impossible.
Shell process
  ├─ current directory variable
  ├─ cd command updates this variable
  ├─ prompt shows current directory
  └─ commands run relative to this directory
Myth Busters - 4 Common Misconceptions
Quick: Does running 'cd' inside a script change your terminal's directory after the script ends? Commit to yes or no.
Common Belief:Running cd inside a script changes the terminal's directory permanently.
Tap to reveal reality
Reality:cd inside a script only changes the directory for that script's process; your terminal stays where it was.
Why it matters:Expecting the terminal to move can cause confusion and errors when running scripts that change directories.
Quick: Does 'cd ..' always take you to the root directory? Commit to yes or no.
Common Belief:cd .. always takes you to the root folder.
Tap to reveal reality
Reality:cd .. moves you up one level, not necessarily to root unless you are already one level below root.
Why it matters:Misunderstanding this can cause navigation mistakes and frustration.
Quick: Does 'cd /folder' work the same as 'cd folder' regardless of your current directory? Commit to yes or no.
Common Belief:cd /folder and cd folder do the same thing.
Tap to reveal reality
Reality:cd /folder goes to an absolute path from root; cd folder goes to a folder inside the current directory.
Why it matters:Confusing absolute and relative paths leads to navigation errors and wasted time.
Quick: Does 'cd -' take you to your home directory? Commit to yes or no.
Common Belief:cd - always takes you home.
Tap to reveal reality
Reality:cd - switches to the previous directory you were in, not necessarily home.
Why it matters:Misusing cd - can cause unexpected directory changes and confusion.
Expert Zone
1
cd updates the shell's internal state but does not affect other running processes or shells.
2
Using cd with symbolic links changes the current directory to the link's target, but the shell may remember the path with links for prompt display.
3
In some shells, cd can trigger hooks or scripts (like cdspell or chpwd) to automate tasks on directory change.
When NOT to use
cd is not suitable for changing directories in other processes or remote systems; use commands like ssh or scp for remote navigation. For temporary directory changes in scripts, consider pushd/popd for stack-based navigation.
Production Patterns
In automation scripts, cd is used carefully to set context before running commands. Scripts often check directory existence before cd to avoid errors. Advanced users combine cd with environment variables and shell hooks to create dynamic workflows.
Connections
Filesystem Hierarchy
cd operates within the filesystem hierarchy structure.
Understanding the filesystem layout helps predict where cd commands will take you and why certain paths exist.
Environment Variables
cd interacts with environment variables like HOME and PWD.
Knowing how environment variables influence cd behavior helps customize navigation and scripting.
Human Navigation and Wayfinding
cd mimics how humans move through physical spaces by changing locations.
Recognizing this connection clarifies why changing directories is fundamental to organizing and accessing resources.
Common Pitfalls
#1Trying to cd into a folder that does not exist.
Wrong approach:cd nonexistentfolder
Correct approach:mkdir nonexistentfolder cd nonexistentfolder
Root cause:Assuming the folder exists without checking causes errors and stops navigation.
#2Using cd in a script expecting the terminal to stay in the new directory after the script ends.
Wrong approach:echo 'cd /tmp' > script.sh bash script.sh pwd
Correct approach:cd /tmp # run commands directly in the terminal or source the script with '. script.sh'
Root cause:Not understanding that scripts run in subshells and do not affect the parent shell's directory.
#3Confusing relative and absolute paths when using cd.
Wrong approach:cd /documents
Correct approach:cd documents
Root cause:Misunderstanding that /documents is an absolute path starting at root, which may not exist.
Key Takeaways
cd changes your current working directory so you can work in different folders easily.
It supports relative paths like .. to move up and absolute paths starting with / to go anywhere.
Special shortcuts like ~ take you home, and - switches to the previous directory.
cd is a shell builtin because it changes the shell's internal state, which external programs cannot do.
Understanding how cd works in scripts and subshells prevents confusion about where commands run.