Newer
Older
# Navigating
Back to the directory structure... this time we will learn to `navigate between directories` and the difference between **absolute** and **relative** paths.
??? quote "First click and follow the instructions below only if you start the course at this stage! Otherwise skip this step!"
{%
include-markdown "pages/bash/bash-0-setup.md"
%}
## Where are we?
**Below is represented the sub-part of the directory structure we will work on (keep this picture aside, it will help!):**
{: style="height:500px"}
!!! warning
`Home` is for Linux, it will be called `Users` on MacOSX
!!! question "Could you tell where our terminal is located currently?"
??? example "Click to show the solution"
To check where we are located we must use the `pwd` command.
!!! tip
By default the termnial open in your `home`. The complete path is `/home/username`.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
!!! question "In unix world, it exists a special character which is a shortcut for your home. Which character is that?"
??? example "Click to show the solution"
Yes the `~` charcater is also your home!
## Move into a child directory (Move down)
Check what files and directorys are presents in the current directory using the `ls` command.
Check if you can see the {{extra.working_directory}} directory. If you see it you can move in it using the `cd` command. This command means `Change Directory`.
```bash
cd {{extra.working_directory}}
```
!!! question "How can you be sure you are in the {{extra.working_directory}} now?"
??? example "Click to show the solution"
You can look at your prompt or check with the `pwd` command.
## File or directory?
Do you remember the `ls` command to list the content of a directory? Good! Did you remember how you differentiate a file from a directory? Run the `ls` command if you are unsure.
??? example "Click to show the solution"
Well actually it is not possible like that.
Bu using the `-F` you can tell ls to classify the output by adding a marker to file and directory names to indicate what they are:
* a trailing / indicates that this is a directory
* @ indicates a link
* \* indicates an executable
!!! tip
Depending on your shell’s default settings, the shell might also use colors to indicate whether each entry is a file or directory.
!!! question "Is there any file then in our current directory?"
??? example "Click to show the solution"
Yes there is a `README` file.
## The hidden files and directories
`ls` command has many options. The `-a` allows you to display the hidden files and directories. They are prefixed with a `.`.
```bash
ls -F -a
```
!!! question "What special directory do you observe?"
??? example "Click to show the solution"
We can see a `.` directory as well as a `..` directory.
`.` means the **current working directory**
`..` means the **parent directory**
!!! tip
Most command line tools accept multiple options by combining them with a single - and no spaces between. `ls -F -a` is equivalent to `ls -Fa`.
## Move in the parent directory (Move up)
Now try to move in the parent directory.
??? example "Click to show the solution"
`cd ..`
You should be now back to your home.
!!! tip
`cd ..` brings you up in the directory structure
## Move back
To move into a directory we have seen we can use the command `cd` and a directory name. If you don't remember which directory you come from you can use `cd -`. It will mover back to the previous directory.
!!! tip
`cd -` brings you back in the directory structure.
If you execute `cd -` twice, you will end up back in the starting directory.
## Relative path
A `relative` path is a path that specifies the location of a file or directory with respect to the current working directory. Any path that does not begin with a separator character ("/") is a relative path.
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
!!! tip
Relative path:
* Path related to the present working directory (where the user is working)
* Never starts with `/`
* Depends on where the user is working
At this point you should be in your `home` directory. You can check using `pwd` or move into your home with `cd ~` to be sure.
Now try to move into the `data` directory.
{: style="height:250px"}
??? example "Click to show the solution"
`cd {{extra.working_directory}}/data`
Actually what we have done so far is to use a relative path.
Let's continue and move into the `script` directory. How would you do it?
{: style="height:250px"}
!!! tip
It is impossible from the `data` directory to move into `{{extra.working_directory}}` using `cd {{extra.working_directory}}`!! To move up you must use `..`!
??? example "Click to show the solution"
`cd ../script`
## Absolute path
An `absolute` path that refers to a particular location in a file system. Absolute paths are usually written with respect to the file system’s root directory, and begin with `/` in Unix machines.
!!! tip
Absolute path:
* Complete path of a file starting from the root directory `/`
* starts always with `/`
* always good wherever user is working
Let's do the same exercise as for relative path. To do so we need to start by being in the `home` directory. How you do that already?
??? example "Click to show the solution"
`cd ~`
Now try to move into the `data` directory using an absolute path.
{: style="height:250px"}
??? example "Click to show the solution"
`cd /home/<userName>/{{extra.working_directory}}/data`
Let's now move into the `script` directory using the absolute path. How would you do it?
{: style="height:250px"}
??? example "Click to show the solution"
`cd /home/<userName>/{{extra.working_directory}}/script`
## Excercise relative vs absolute path
We are now in the `script` directory.
{: style="height:250px"}
!!! question "How would you move into your home directory?"
1. cd .
2. cd ..
3. cd ../..
5. cd ~
6. cd home
7. cd ~/data/..
9. cd /
??? example "Click to show the solution"
3,4,5
It exist a wonderful behavior in Bash called the autocompletion. What it is?
Autocompletion refers to a feature that allows you to automatically complete the names of commands and **file paths** by pressing the Tab key. This feature can significantly improve efficiency and reduce the need to type out long or complex names manually.
Bash can complete file paths based on what exists in the current directory or any specified path.
As you type a file or directory name and press Tab, Bash will complete the name if it's unique. If not, it will show a list of possible matches.
!!! exercise
Move to `data` directory via the absolute path using the autocompletion.
## A final navigation tip
You are supposed to be in the `data` directory now. Try the command `cd` without any argument. What happens? (check with `pwd`)
??? example "Click to show the solution"
Right you moved in the `home` directory. This command is even shorter than `cd ~`.
!!! Success "Quick recap"
In this section we've learned:
- How to navigate in the Unix directory structure
- The difference between absolute and relative path
- Special character `~`, `..`, `.`