|
| 1 | +--- |
| 2 | +title: Navigating Files and Directories |
| 3 | +teaching: 30 |
| 4 | +exercises: 20 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::::: objectives |
| 8 | + |
| 9 | +- Use a single command to navigate multiple steps in your directory structure, including moving backwards (one level up). |
| 10 | +- Perform operations on files in directories outside your working directory. |
| 11 | +- Work with hidden directories and hidden files. |
| 12 | +- Interconvert between absolute and relative paths. |
| 13 | +- Employ navigational shortcuts to move around your file system. |
| 14 | + |
| 15 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 16 | + |
| 17 | +:::::::::::::::::::::::::::::::::::::::: questions |
| 18 | + |
| 19 | +- How can I perform operations on files outside of my working directory? |
| 20 | +- What are some navigational shortcuts I can use to make my work more efficient? |
| 21 | + |
| 22 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 23 | + |
| 24 | +## Moving around the file system |
| 25 | + |
| 26 | +We've learned how to use `pwd` to find our current location within our file system. |
| 27 | +We've also learned how to use `cd` to change locations and `ls` to list the contents |
| 28 | +of a directory. Now we're going to learn some additional commands for moving around |
| 29 | +within our file system. |
| 30 | + |
| 31 | +Use the commands we've learned so far to navigate to the `shell_data/untrimmed_fastq` directory, if |
| 32 | +you're not already there. |
| 33 | + |
| 34 | +```bash |
| 35 | +$ cd |
| 36 | +$ cd shell_data |
| 37 | +$ cd untrimmed_fastq |
| 38 | +``` |
| 39 | + |
| 40 | +What if we want to move back up and out of this directory and to our top level |
| 41 | +directory? Can we type `cd shell_data`? Try it and see what happens. |
| 42 | + |
| 43 | +```bash |
| 44 | +$ cd shell_data |
| 45 | +``` |
| 46 | + |
| 47 | +```output |
| 48 | +-bash: cd: shell_data: No such file or directory |
| 49 | +``` |
| 50 | + |
| 51 | +Your computer looked for a directory or file called `shell_data` within the |
| 52 | +directory you were already in. It didn't know you wanted to look at a directory level |
| 53 | +above the one you were located in. |
| 54 | + |
| 55 | +We have two special directory names `.` and `..`; `.` refers to the current directory you are in and `..` refers to the directory one level above the current directory. We can use `..` with `cd` to move up one directory level. |
| 56 | + |
| 57 | +```bash |
| 58 | +$ cd .. |
| 59 | +``` |
| 60 | + |
| 61 | +Now we can use `pwd` to make sure that we are in the directory we intended to navigate |
| 62 | +to, and `ls` to check that the contents of the directory are correct. |
| 63 | + |
| 64 | +```bash |
| 65 | +$ pwd |
| 66 | +``` |
| 67 | + |
| 68 | +```output |
| 69 | +/home/dcuser/shell_data |
| 70 | +``` |
| 71 | + |
| 72 | +```bash |
| 73 | +$ ls |
| 74 | +``` |
| 75 | + |
| 76 | +```output |
| 77 | +sra_metadata untrimmed_fastq |
| 78 | +``` |
| 79 | + |
| 80 | +From this output, we can see that `..` did indeed take us back one level in our file system. |
| 81 | + |
| 82 | +You can chain these together like so: |
| 83 | + |
| 84 | +```bash |
| 85 | +$ ls ../../ |
| 86 | +``` |
| 87 | + |
| 88 | +prints the contents of `/home`. |
| 89 | + |
| 90 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 91 | + |
| 92 | +## Finding hidden directories |
| 93 | + |
| 94 | +First navigate to the `shell_data` directory. There is a hidden directory within this directory. Explore the options for `ls` to |
| 95 | +find out how to see hidden directories. List the contents of the directory and |
| 96 | +identify the name of the text file in that directory. |
| 97 | + |
| 98 | +Hint: hidden files and folders in Unix start with `.`, for example `.my_hidden_directory` |
| 99 | + |
| 100 | +::::::::::::::: solution |
| 101 | + |
| 102 | +## Solution |
| 103 | + |
| 104 | +First use the `man` command to look at the options for `ls`. |
| 105 | + |
| 106 | +```bash |
| 107 | +$ man ls |
| 108 | +``` |
| 109 | + |
| 110 | +The `-a` option is short for `all` and says that it causes `ls` to "not ignore |
| 111 | +entries starting with ." This is the option we want. |
| 112 | + |
| 113 | +```bash |
| 114 | +$ ls -a |
| 115 | +``` |
| 116 | + |
| 117 | +```output |
| 118 | +. .. .hidden sra_metadata untrimmed_fastq |
| 119 | +``` |
| 120 | + |
| 121 | +The name of the hidden directory is `.hidden`. We can navigate to that directory |
| 122 | +using `cd`. |
| 123 | + |
| 124 | +```bash |
| 125 | +$ cd .hidden |
| 126 | +``` |
| 127 | + |
| 128 | +And then list the contents of the directory using `ls`. |
| 129 | + |
| 130 | +```bash |
| 131 | +$ ls |
| 132 | +``` |
| 133 | + |
| 134 | +```output |
| 135 | +youfoundit.txt |
| 136 | +``` |
| 137 | + |
| 138 | +The name of the text file is `youfoundit.txt`. |
| 139 | + |
| 140 | +::::::::::::::::::::::::: |
| 141 | + |
| 142 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 143 | + |
| 144 | +In most commands the flags can be combined together in no particular order to obtain the desired results/output. |
| 145 | + |
| 146 | +``` |
| 147 | +$ ls -Fa |
| 148 | +$ ls -laF |
| 149 | +``` |
| 150 | + |
| 151 | +## Examining the contents of other directories |
| 152 | + |
| 153 | +By default, the `ls` commands lists the contents of the working |
| 154 | +directory (i.e. the directory you are in). You can always find the |
| 155 | +directory you are in using the `pwd` command. However, you can also |
| 156 | +give `ls` the names of other directories to view. Navigate to your |
| 157 | +home directory if you are not already there. |
| 158 | + |
| 159 | +```bash |
| 160 | +$ cd |
| 161 | +``` |
| 162 | + |
| 163 | +Then enter the command: |
| 164 | + |
| 165 | +```bash |
| 166 | +$ ls shell_data |
| 167 | +``` |
| 168 | + |
| 169 | +```output |
| 170 | +sra_metadata untrimmed_fastq |
| 171 | +``` |
| 172 | + |
| 173 | +This will list the contents of the `shell_data` directory without |
| 174 | +you needing to navigate there. |
| 175 | + |
| 176 | +The `cd` command works in a similar way. |
| 177 | + |
| 178 | +Try entering: |
| 179 | + |
| 180 | +```bash |
| 181 | +$ cd |
| 182 | +$ cd shell_data/untrimmed_fastq |
| 183 | +``` |
| 184 | + |
| 185 | +This will take you to the `untrimmed_fastq` directory without having to go through |
| 186 | +the intermediate directory. |
| 187 | + |
| 188 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 189 | + |
| 190 | +## Navigating practice |
| 191 | + |
| 192 | +Navigate to your home directory. From there, list the contents of the `untrimmed_fastq` |
| 193 | +directory. |
| 194 | + |
| 195 | +::::::::::::::: solution |
| 196 | + |
| 197 | +## Solution |
| 198 | + |
| 199 | +```bash |
| 200 | +$ cd |
| 201 | +$ ls shell_data/untrimmed_fastq/ |
| 202 | +``` |
| 203 | + |
| 204 | +```output |
| 205 | +SRR097977.fastq SRR098026.fastq |
| 206 | +``` |
| 207 | + |
| 208 | +::::::::::::::::::::::::: |
| 209 | + |
| 210 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 211 | + |
| 212 | +## Full vs. Relative Paths |
| 213 | + |
| 214 | +The `cd` command takes an argument which is a directory |
| 215 | +name. Directories can be specified using either a *relative* path or a |
| 216 | +full *absolute* path. The directories on the computer are arranged into a |
| 217 | +hierarchy. The full path tells you where a directory is in that |
| 218 | +hierarchy. Navigate to the home directory, then enter the `pwd` |
| 219 | +command. |
| 220 | + |
| 221 | +```bash |
| 222 | +$ cd |
| 223 | +$ pwd |
| 224 | +``` |
| 225 | + |
| 226 | +You will see: |
| 227 | + |
| 228 | +```output |
| 229 | +/home/dcuser |
| 230 | +``` |
| 231 | + |
| 232 | +This is the full name of your home directory. This tells you that you |
| 233 | +are in a directory called `dcuser`, which sits inside a directory called |
| 234 | +`home` which sits inside the very top directory in the hierarchy. The |
| 235 | +very top of the hierarchy is a directory called `/` which is usually |
| 236 | +referred to as the *root directory*. So, to summarize: `dcuser` is a |
| 237 | +directory in `home` which is a directory in `/`. More on `root` and |
| 238 | +`home` in the next section. |
| 239 | + |
| 240 | +Now enter the following command: |
| 241 | + |
| 242 | +```bash |
| 243 | +$ cd /home/dcuser/shell_data/.hidden |
| 244 | +``` |
| 245 | + |
| 246 | +This jumps forward multiple levels to the `.hidden` directory. |
| 247 | +Now go back to the home directory. |
| 248 | + |
| 249 | +```bash |
| 250 | +$ cd |
| 251 | +``` |
| 252 | + |
| 253 | +You can also navigate to the `.hidden` directory using: |
| 254 | + |
| 255 | +```bash |
| 256 | +$ cd shell_data/.hidden |
| 257 | +``` |
| 258 | + |
| 259 | +These two commands have the same effect, they both take us to the `.hidden` directory. |
| 260 | +The first uses the absolute path, giving the full address from the home directory. The |
| 261 | +second uses a relative path, giving only the address from the working directory. A full |
| 262 | +path always starts with a `/`. A relative path does not. |
| 263 | + |
| 264 | +A relative path is like getting directions from someone on the street. They tell you to |
| 265 | +"go right at the stop sign, and then turn left on Main Street". That works great if |
| 266 | +you're standing there together, but not so well if you're trying to tell someone how to |
| 267 | +get there from another country. A full path is like GPS coordinates. It tells you exactly |
| 268 | +where something is no matter where you are right now. |
| 269 | + |
| 270 | +You can usually use either a full path or a relative path depending on what is most convenient. |
| 271 | +If we are in the home directory, it is more convenient to enter the full path. |
| 272 | +If we are in the working directory, it is more convenient to enter the relative path |
| 273 | +since it involves less typing. |
| 274 | + |
| 275 | +Over time, it will become easier for you to keep a mental note of the |
| 276 | +structure of the directories that you are using and how to quickly |
| 277 | +navigate amongst them. |
| 278 | + |
| 279 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 280 | + |
| 281 | +## Relative path resolution |
| 282 | + |
| 283 | +Using the filesystem diagram below, if `pwd` displays `/Users/thing`, |
| 284 | +what will `ls ../backup` display? |
| 285 | + |
| 286 | +1. `../backup: No such file or directory` |
| 287 | +2. `2012-12-01 2013-01-08 2013-01-27` |
| 288 | +3. `2012-12-01/ 2013-01-08/ 2013-01-27/` |
| 289 | +4. `original pnas_final pnas_sub` |
| 290 | + |
| 291 | +{alt='File System for Challenge Questions'} |
| 292 | + |
| 293 | +::::::::::::::: solution |
| 294 | + |
| 295 | +## Solution |
| 296 | + |
| 297 | +1. No: there *is* a directory `backup` in `/Users`. |
| 298 | +2. No: this is the content of `Users/thing/backup`, |
| 299 | + but with `..` we asked for one level further up. |
| 300 | +3. No: see previous explanation. |
| 301 | + Also, we did not specify `-F` to display `/` at the end of the directory names. |
| 302 | +4. Yes: `../backup` refers to `/Users/backup`. |
| 303 | + |
| 304 | +::::::::::::::::::::::::: |
| 305 | + |
| 306 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 307 | + |
| 308 | +### Navigational Shortcuts |
| 309 | + |
| 310 | +The root directory is the highest level directory in your file |
| 311 | +system and contains files that are important for your computer |
| 312 | +to perform its daily work. While you will be using the root (`/`) |
| 313 | +at the beginning of your absolute paths, it is important that you |
| 314 | +avoid working with data in these higher-level directories, as |
| 315 | +your commands can permanently alter files that the operating |
| 316 | +system needs to function. In many cases, trying to run commands |
| 317 | +in `root` directories will require special permissions which are |
| 318 | +not discussed here, so it's best to avoid them and work within your |
| 319 | +home directory. Dealing with the `home` directory is very common. |
| 320 | +The tilde character, `~`, is a shortcut for your home directory. |
| 321 | +In our case, the `root` directory is **two** levels above our |
| 322 | +`home` directory, so `cd` or `cd ~` will take you to |
| 323 | +`/home/dcuser` and `cd /` will take you to `/`. Navigate to the |
| 324 | +`shell_data` directory: |
| 325 | + |
| 326 | +```bash |
| 327 | +$ cd |
| 328 | +$ cd shell_data |
| 329 | +``` |
| 330 | + |
| 331 | +Then enter the command: |
| 332 | + |
| 333 | +```bash |
| 334 | +$ ls ~ |
| 335 | +``` |
| 336 | + |
| 337 | +```output |
| 338 | +R r_data shell_data |
| 339 | +``` |
| 340 | + |
| 341 | +This prints the contents of your home directory, without you needing to |
| 342 | +type the full path. |
| 343 | + |
| 344 | +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. |
| 345 | + |
| 346 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 347 | + |
| 348 | +- The `/`, `~`, and `..` characters represent important navigational shortcuts. |
| 349 | +- Hidden files and directories start with `.` and can be viewed using `ls -a`. |
| 350 | +- Relative paths specify a location starting from the current location, while absolute paths specify a location from the root of the file system. |
| 351 | + |
| 352 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 353 | + |
| 354 | + |
0 commit comments