Objectives & prerequisites
- Navigate through the directories in your filesystem.
- Use an absolute path to change into a directory.
- Use a relative path to change into a directory.
- Demonstrate how to change into directories with spaces in their name.
None.
Changing directories
Use cd to move in and out of directories.
Activity
Can you remember the difference between absolute and relative paths?
- Absolute paths start at the root directory (
/). They always start with a/or~. - Relative paths start at your current directory.
Example: using an absolute path to change into a directory
sarah@laptop ~/Downloads$ cd /var/log
sarah@laptop /var/log$Example: moving down one directory
sarah@laptop ~/documents$ cd research
sarah@laptop ~/documents/research$Example: moving down more than one directory
sarah@laptop ~/$ cd documents/research
sarah@laptop ~/documents/research$Example: moving up one directory
sarah@laptop ~/documents/research$ cd ..
sarah@laptop ~/documents$Example: moving up one directory, again
sarah@laptop ~/documents$ cd ..
sarah@laptop ~$Example: moving up more than one directory
sarah@laptop ~/documents/research$ cd ../..
sarah@laptop ~$Example: moving up 2 directories, and then into a subdirectory
sarah@laptop ~/documents/research$ cd ../../backups/logs
sarah@laptop ~/backups/logs$The above command has the same result as:
sarah@laptop ~/documents/research$ cd ~/backups/logs
sarah@laptop ~/backups/logs$The above command has the same result as:
sarah@laptop ~/documents/research$ cd /home/sarah/backups/logs
sarah@laptop ~/backups/logs$The above command has the same result as:
sarah@laptop ~/documents/research$ cd ..
sarah@laptop ~/documents$ cd ..
sarah@laptop ~$ cd backups/logs
sarah@laptop ~/backups/logs$Directories with spaces in their name
There are two ways to work with directories that have spaces in their name:
Use single or double quotes
sarah@laptop:~/Documents$ cd "Updated reports"Use backslash to escape each whitespace character
sarah@laptop:~/Documents$ cd Updated\ reportsBoth methods are equivalent; feel free to use the one you are most comfortable with.
Activity
You have learned how to navigate to the parent directory using a double dot.
Can you think of two ways to navigate to the grandparent directory (two levels up?)
- Run
cd ..twice. - Run
cd ../..once.
Activity
Which command would you run to navigate to the root directory?
cd /
(The single forward slash is the absolute path to the topmost directory, or root directory)
Activity
You are located in your home directory, which contains a photos subdirectory, which contains a Delhi subdirectory, which contains a Conference 2023 subdirectory.
Give two commands to change into the Conference 2023 directory, each using a different method to handle spaces.
cd "photos/Delhi/Conference 2023"cd photos/Delhi/Conference\ 2023
Activity
Which single command can you always run to navigate to your home directory, regardless of your current location and username?
cd ~
(The tilde represents the absolute path to your home directory.)
Summary
- Use
cdto move in and out of directories. - You can use absolute or relative paths.
- If a directory name contains whitespaces, wrap the name in quotes, or escape each whitespace character with a backslash(
\)