|
| 1 | +# 3. Absolute vs Relative Paths |
| 2 | + |
| 3 | + |
| 4 | +The `cd` command takes an argument which is a directory name. Directories can be specified using either a _relative_ path or a |
| 5 | +full _absolute_ path. The directories on the computer are arranged into a hierarchy. The full path tells you where a directory is in that |
| 6 | +hierarchy. Navigate to the home directory, then enter the `pwd` command. |
| 7 | + |
| 8 | + |
| 9 | +!!! terminal "code" |
| 10 | + |
| 11 | + ```bash |
| 12 | + $ cd |
| 13 | + $ pwd |
| 14 | + ``` |
| 15 | + |
| 16 | + You will see: |
| 17 | + |
| 18 | + ```output |
| 19 | + /users/group/user |
| 20 | + ``` |
| 21 | + |
| 22 | +This is the full name of your home directory. This tells you that you |
| 23 | +are in a directory called `training`, which sits inside a directory called |
| 24 | +`home` which sits inside the very top directory in the hierarchy. The |
| 25 | +very top of the hierarchy is a directory called `/` which is usually |
| 26 | +referred to as the _root directory_. So, to summarize: `training` is a |
| 27 | +directory in `home` which is a directory in `/`. More on `root` and |
| 28 | +`home` in the next section. |
| 29 | + |
| 30 | +!!! terminal-2 "Now enter the following command:" |
| 31 | + |
| 32 | + ```bash |
| 33 | + $ cd /users/group/user/shell_data/.hidden |
| 34 | + ``` |
| 35 | + |
| 36 | + This jumps forward multiple levels to the `.hidden` directory. |
| 37 | + Now go back to the home directory. |
| 38 | + |
| 39 | + ```bash |
| 40 | + $ cd |
| 41 | + ``` |
| 42 | + |
| 43 | +You can also navigate to the `.hidden` directory using: |
| 44 | + |
| 45 | +!!! terminal "code" |
| 46 | + |
| 47 | + ```bash |
| 48 | + $ cd ~/shell_data/.hidden |
| 49 | + ``` |
| 50 | + |
| 51 | +These two commands have the same effect, they both take us to the `.hidden` directory. |
| 52 | +The first uses the absolute path, giving the full address from the home directory. The |
| 53 | +second uses a relative path, giving only the address from the working directory. A full |
| 54 | +path always starts with a `/`. A relative path does not. |
| 55 | + |
| 56 | +A relative path is like getting directions from someone on the street. They tell you to |
| 57 | +"go right at the stop sign, and then turn left on Main Street". That works great if |
| 58 | +you're standing there together, but not so well if you're trying to tell someone how to |
| 59 | +get there from another country. A full path is like GPS coordinates. It tells you exactly |
| 60 | +where something is no matter where you are right now. |
| 61 | + |
| 62 | +You can usually use either a full path or a relative path depending on what is most convenient. |
| 63 | +If we are in the home directory, it is more convenient to enter the full path. |
| 64 | +If we are in the working directory, it is more convenient to enter the relative path |
| 65 | +since it involves less typing. |
| 66 | + |
| 67 | +Over time, it will become easier for you to keep a mental note of the |
| 68 | +structure of the directories that you are using and how to quickly |
| 69 | +navigate amongst them. |
| 70 | + |
| 71 | +# Relative path resolution |
| 72 | + |
| 73 | +!!! dumbbell "Using the filesystem diagram below, if `pwd` displays `/Users/thing`," what will `ls ../backup` display?" |
| 74 | + |
| 75 | + 1. `../backup: No such file or directory` |
| 76 | + 2. `2012-12-01 2013-01-08 2013-01-27` |
| 77 | + 3. `2012-12-01/ 2013-01-08/ 2013-01-27/` |
| 78 | + 4. `original pnas_final pnas_sub` |
| 79 | + |
| 80 | + {alt='File System for Challenge Questions'} |
| 81 | + |
| 82 | + |
| 83 | + ??? success "Solution" |
| 84 | + |
| 85 | + 1. No: there _is_ a directory `backup` in `/Users`. |
| 86 | + 2. No: this is the content of `Users/thing/backup`, |
| 87 | + but with `..` we asked for one level further up. |
| 88 | + 3. No: see previous explanation. |
| 89 | + Also, we did not specify `-F` to display `/` at the end of the directory names. |
| 90 | + 4. Yes: `../backup` refers to `/Users/backup`. |
| 91 | + |
| 92 | + |
| 93 | +### Navigational Shortcuts |
| 94 | + |
| 95 | +The root directory is the highest level directory in your file |
| 96 | +system and contains files that are important for your computer |
| 97 | +to perform its daily work. While you will be using the root (`/`) |
| 98 | +at the beginning of your absolute paths, it is important that you |
| 99 | +avoid working with data in these higher-level directories, as |
| 100 | +your commands can permanently alter files that the operating |
| 101 | +system needs to function. In many cases, trying to run commands |
| 102 | +in `root` directories will require special permissions which are |
| 103 | +not discussed here, so it's best to avoid them and work within your |
| 104 | +home directory. Dealing with the `home` directory is very common. |
| 105 | +The tilde character, `~`, is a shortcut for your home directory. |
| 106 | +In our case, the `root` directory is **two** levels above our |
| 107 | +`home` directory, so `cd` or `cd ~` will take you to |
| 108 | +`/home/<username>` and `cd /` will take you to `/`. Navigate to the |
| 109 | +`shell_data` directory: |
| 110 | + |
| 111 | +!!! terminal "code" |
| 112 | + |
| 113 | + ```bash |
| 114 | + $ cd |
| 115 | + $ cd ~/shell_data |
| 116 | + ``` |
| 117 | + |
| 118 | + Then enter the command: |
| 119 | + |
| 120 | + ```bash |
| 121 | + $ ls ~ |
| 122 | + ``` |
| 123 | + |
| 124 | + ```output |
| 125 | + shell_data |
| 126 | + ``` |
| 127 | + |
| 128 | +This prints the contents of your home directory, without you needing to |
| 129 | +type the full path. |
| 130 | + |
| 131 | +!!! quote "" |
| 132 | + |
| 133 | + The commands `cd`, and `cd ~` are very useful for quickly navigating back to your home directory. We will be using the `~` character in later lessons to specify our home directory. |
| 134 | + |
| 135 | +!!! graduation-cap "Summary" |
| 136 | + |
| 137 | + - The `/`, `~`, and `..` characters represent important navigational shortcuts. |
| 138 | + - Hidden files and directories start with `.` and can be viewed using `ls -a`. |
| 139 | + - Relative paths specify a location starting from the current location, while absolute paths specify a location from the root of the file system. |
0 commit comments