Discover how a simple command can save you minutes every day navigating your files!
Why cd (change directory) in Linux CLI? - Purpose & Use Cases
Imagine you have dozens of folders on your computer, each with important files. To open a file, you have to remember the full path and type it every time in your command line or file explorer.
Typing long folder paths manually is slow and easy to mess up. One wrong letter and you get an error or end up in the wrong place. It wastes time and causes frustration.
The cd command lets you quickly jump between folders without typing full paths. You just tell it where to go, and it moves you there instantly, saving time and avoiding mistakes.
ls /home/user/documents/work/project ls /home/user/documents/personal/photos
cd /home/user/documents/work/project ls cd ../personal/photos ls
With cd, you can navigate your computer's folders like a pro, making your work faster and smoother.
When working on a coding project, you often switch between source code, tests, and documentation folders. Using cd helps you jump between these folders quickly without typing long paths.
Manually typing full folder paths is slow and error-prone.
cd lets you change folders quickly and easily.
This saves time and reduces mistakes when working in the command line.
Practice
cd .. do in a Linux terminal?Solution
Step 1: Understand the meaning of
The..in directory paths..symbol means the parent directory, which is one level up from the current directory.Step 2: Apply
Usingcd ..commandcd ..changes the current directory to its parent directory, effectively moving you up one level.Final Answer:
Moves you up one directory level -> Option BQuick Check:
cd ..= move up one folder [OK]
- Thinking cd .. moves to home directory
- Confusing cd .. with cd / (root)
- Using cd .. to list files
Solution
Step 1: Identify the symbol for home directory
The tilde~is a shortcut for the current user's home directory in Linux.Step 2: Check the command syntax
cd ~correctly changes the directory to the home folder. Other options either go up one level, to root, or previous directory.Final Answer:
cd ~ -> Option AQuick Check:
cd ~= go home [OK]
- Using cd .. to go home instead of up one level
- Using cd /home which may not be correct for all users
- Using cd - which goes to previous directory
/home/user/docs and you run cd ../pictures, what will be your new directory?Solution
Step 1: Understand the starting directory and command
Current directory is/home/user/docs. The commandcd ../picturesmeans go up one level to/home/userthen intopictures.Step 2: Calculate the new directory path
Going up one level from/home/user/docsis/home/user. Addingpicturesgives/home/user/pictures.Final Answer:
/home/user/pictures -> Option CQuick Check:
cd ../pictures = up one + pictures folder [OK]
- Appending pictures inside current directory instead of parent
- Going to root or home by mistake
- Ignoring the .. and staying in same folder
cd myfolder but get an error: bash: cd: myfolder: No such file or directory. What is the most likely cause?Solution
Step 1: Understand the error message
The error says 'No such file or directory', meaning the folder named 'myfolder' is not found in the current directory.Step 2: Check the folder existence and path
Since 'myfolder' is not present, the command fails. Using relative or absolute path won't help unless the folder exists.Final Answer:
The folder 'myfolder' does not exist in the current directory -> Option DQuick Check:
Error means folder missing, so cd fails [OK]
- Assuming cd ./myfolder fixes missing folder error
- Thinking you can't cd into current folder
- Believing absolute path is always required
projects inside it. Which sequence of commands is correct?Solution
Step 1: Start in home directory
Usingcd ~moves the shell to the user's home directory regardless of current location.Step 2: Move into 'projects' folder inside home
After being in home,cd projectsmoves into the 'projects' folder located there.Final Answer:
cd ~\ncd projects -> Option AQuick Check:
Home then projects folder = cd ~ then cd projects [OK]
- Changing to projects before home, causing errors
- Using absolute path /projects which may not exist
- Using cd .. which goes up instead of home
Ready to prove it?
Pass the test and this pattern moves unknown
