|
| 1 | +--- |
| 2 | +title: Navigating the filesystem |
| 3 | +teaching: 20 |
| 4 | +exercises: 10 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::::: objectives |
| 8 | + |
| 9 | +- Use shell commands to work with directories and files |
| 10 | +- Use shell commands to find and manipulate data |
| 11 | + |
| 12 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 13 | + |
| 14 | +:::::::::::::::::::::::::::::::::::::::: questions |
| 15 | + |
| 16 | +- How do you move around the filesystem in the shell? |
| 17 | + |
| 18 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 19 | + |
| 20 | +## Navigating the shell |
| 21 | + |
| 22 | +We will begin with the basics of navigating the Unix shell. |
| 23 | + |
| 24 | +Let's start by opening the shell. This likely results in seeing a black or white window with a cursor flashing next to a dollar sign. This is our command line, and the `$` is the command **prompt** to show that the system is ready for our input. The appearance of the prompt will vary from system to system, depending on how the set up has been configured. Other common prompts include the `%` or `#` signs, but we will use `$` in this lesson to represent the prompt generally. |
| 25 | + |
| 26 | +When working in the shell, you are always *somewhere* in the computer's file system, in some folder (directory). We will therefore start by finding out where we are by using the `pwd` command, which you can use whenever you are unsure about where you are. It stands for "print working directory" and the result of the command is printed to your standard output, which is the screen. |
| 27 | + |
| 28 | +Let's type `pwd` and press enter to execute the command (Note that the `$` sign is used to indicate a command to be typed on the command prompt, |
| 29 | +but we never type the `$` sign itself, just what follows after it.): |
| 30 | + |
| 31 | +```bash |
| 32 | +$ pwd |
| 33 | +``` |
| 34 | + |
| 35 | +```output |
| 36 | +/Users/riley |
| 37 | +``` |
| 38 | + |
| 39 | +The output will be a path to your home directory. Let's check if we recognise it by looking at the contents of the directory. To do that, we use the `ls` command. This stands for "list" and the result is a print out of all the contents in the directory: |
| 40 | + |
| 41 | +```bash |
| 42 | +$ ls |
| 43 | +``` |
| 44 | + |
| 45 | +```output |
| 46 | +Applications Documents Library Music Public |
| 47 | +Desktop Downloads Movies Pictures |
| 48 | +``` |
| 49 | + |
| 50 | +We may want more information than just a list of files and directories. We can get this by specifying various **flags** (also known as `options`, `parameters`, or, most frequently, `arguments`) to go with our basic commands. Arguments modify the workings of the command by telling the computer what sort of output or manipulation we want. |
| 51 | + |
| 52 | +If we type `ls -l` and press enter, the computer returns a list of files that contains information similar to what we would find in our Finder (Mac) or Explorer (Windows): the size of the files in bytes, the date it was created or last modified, and the file name. |
| 53 | + |
| 54 | +```bash |
| 55 | +$ ls -l |
| 56 | +``` |
| 57 | + |
| 58 | +```output |
| 59 | +total 0 |
| 60 | +drwx------+ 6 riley staff 204 Jul 16 11:50 Desktop |
| 61 | +drwx------+ 3 riley staff 102 Jul 16 11:30 Documents |
| 62 | +drwx------+ 3 riley staff 102 Jul 16 11:30 Downloads |
| 63 | +drwx------@ 46 riley staff 1564 Jul 16 11:38 Library |
| 64 | +drwx------+ 3 riley staff 102 Jul 16 11:30 Movies |
| 65 | +drwx------+ 3 riley staff 102 Jul 16 11:30 Music |
| 66 | +drwx------+ 3 riley staff 102 Jul 16 11:30 Pictures |
| 67 | +drwxr-xr-x+ 5 riley staff 170 Jul 16 11:30 Public |
| 68 | +``` |
| 69 | + |
| 70 | +In everyday usage we are more accustomed to units of measurement like kilobytes, megabytes, and gigabytes. Luckily, there's another flag `-h` that when used with the -l option, prints unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to three or fewer using base 2 for sizes. |
| 71 | + |
| 72 | +Now `ls -h` won't work on its own. When we want to combine two flags, we can just run them together. So, by typing `ls -lh` and pressing enter we receive an output in a human-readable format (note: the order here doesn't matter). |
| 73 | + |
| 74 | +```bash |
| 75 | +$ ls -lh |
| 76 | +``` |
| 77 | + |
| 78 | +```output |
| 79 | +total 0 |
| 80 | +drwx------+ 6 riley staff 204B Jul 16 11:50 Desktop |
| 81 | +drwx------+ 3 riley staff 102B Jul 16 11:30 Documents |
| 82 | +drwx------+ 3 riley staff 102B Jul 16 11:30 Downloads |
| 83 | +drwx------@ 46 riley staff 1.5K Jul 16 11:38 Library |
| 84 | +drwx------+ 3 riley staff 102B Jul 16 11:30 Movies |
| 85 | +drwx------+ 3 riley staff 102B Jul 16 11:30 Music |
| 86 | +drwx------+ 3 riley staff 102B Jul 16 11:30 Pictures |
| 87 | +drwxr-xr-x+ 5 riley staff 170B Jul 16 11:30 Public |
| 88 | +``` |
| 89 | + |
| 90 | +We've now spent a great deal of time in our home directory. Let's go somewhere else. We can do that through the `cd` or Change Directory command: (Note: On Windows and Mac, by default, the case of the file/directory doesn't matter. On Linux it does.) |
| 91 | + |
| 92 | +```bash |
| 93 | +$ cd Desktop |
| 94 | +``` |
| 95 | + |
| 96 | +Notice that the command didn't output anything. This means that it was carried out successfully. Let's check by using `pwd`: |
| 97 | + |
| 98 | +```bash |
| 99 | +$ pwd |
| 100 | +``` |
| 101 | + |
| 102 | +```output |
| 103 | +/Users/riley/Desktop |
| 104 | +``` |
| 105 | + |
| 106 | +If something had gone wrong, however, the command would have told you. Let's test that by trying to move into a non-existent directory: |
| 107 | + |
| 108 | +```bash |
| 109 | +$ cd "things to learn about the shell" |
| 110 | +``` |
| 111 | + |
| 112 | +```output |
| 113 | +bash: cd: things to learn about the shell: No such file or directory |
| 114 | +``` |
| 115 | + |
| 116 | +Notice that we surrounded the name by quotation marks. The *arguments* given to any shell command are separated by spaces, so a way to let them know that we mean 'one single thing called "things to learn about the shell"', not 'six different things', is to use (single or double) quotation marks. |
| 117 | + |
| 118 | +We've now seen how we can go 'down' through our directory structure (as in into more nested directories). If we want to go back, we can type `cd ..`. This moves us 'up' one directory, putting us back where we started. **If we ever get completely lost, the command `cd` without any arguments will bring |
| 119 | +us right back to the home directory, the place where we started.** |
| 120 | + |
| 121 | +::::::::::::::::::::::::::::::::::::::::: callout |
| 122 | + |
| 123 | +## Previous Directory |
| 124 | + |
| 125 | +To switch back and forth between two directories use `cd -`. |
| 126 | + |
| 127 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 128 | + |
| 129 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 130 | + |
| 131 | +## Try exploring |
| 132 | + |
| 133 | +Move around the computer, get used to moving in and out of directories, see how different file types appear in the Unix shell. Be sure to use the `pwd` and `cd` commands, and the different flags for the `ls` command you learned so far. |
| 134 | + |
| 135 | +If you run Windows, also try typing `explorer .` to open Explorer for the current directory (the single dot means "current directory"). If you're on a Mac, try `open .` and for Linux try `xdg-open .` to open their graphical file manager. |
| 136 | + |
| 137 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 138 | + |
| 139 | +Being able to navigate the file system is very important for using the Unix shell effectively. |
| 140 | +As we become more comfortable, we can get very quickly to the directory that we want. |
| 141 | + |
| 142 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 143 | + |
| 144 | +## Getting help |
| 145 | + |
| 146 | +Use the `man` command to invoke the manual page (documentation) for a shell command. For example, `man ls` displays all the arguments available to you - which saves you remembering them all! Try this for each command you've learned so far. Use the <kbd>spacebar</kbd> to navigate the manual pages. Use <kbd>q</kbd> at any time to quit. |
| 147 | + |
| 148 | +***Note*: this command is for Mac and Linux users only**. It does not work directly for Windows users. If you use Windows, you can search for the shell command on [http://man.he.net/](https://man.he.net/), and view the associated manual page. In some systems the command name followed by `--help` will work, e.g. `ls --help`. |
| 149 | + |
| 150 | +Also, the manual lists commands individually, e.g., although `-h` can only be used together with the `-l` option, you'll find it listed as `-h` in the manual, not as `-lh`. |
| 151 | + |
| 152 | +::::::::::::::: solution |
| 153 | + |
| 154 | +## Answer |
| 155 | + |
| 156 | +```bash |
| 157 | +$ man ls |
| 158 | +``` |
| 159 | + |
| 160 | +```output |
| 161 | +LS(1) BSD General Commands Manual LS(1) |
| 162 | +
|
| 163 | +NAME |
| 164 | + ls -- list directory contents |
| 165 | +
|
| 166 | +SYNOPSIS |
| 167 | + ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...] |
| 168 | +
|
| 169 | +DESCRIPTION |
| 170 | + For each operand that names a file of a type other than directory, ls |
| 171 | + displays its name as well as any requested, associated information. For |
| 172 | + each operand that names a file of type directory, ls displays the names |
| 173 | + of files contained within that directory, as well as any requested, asso- |
| 174 | + ciated information. |
| 175 | +
|
| 176 | + If no operands are given, the contents of the current directory are dis- |
| 177 | + played. If more than one operand is given, non-directory operands are |
| 178 | + displayed first; directory and non-directory operands are sorted sepa- |
| 179 | + rately and in lexicographical order. |
| 180 | +
|
| 181 | + The following options are available: |
| 182 | +
|
| 183 | + -@ Display extended attribute keys and sizes in long (-l) output. |
| 184 | +
|
| 185 | + -1 (The numeric digit ``one''.) Force output to be one entry per |
| 186 | + line. This is the default when output is not to a terminal. |
| 187 | +
|
| 188 | + -A List all entries except for . and ... Always set for the super- |
| 189 | + user. |
| 190 | +
|
| 191 | +...several more pages... |
| 192 | +
|
| 193 | +BUGS |
| 194 | + To maintain backward compatibility, the relationships between the many |
| 195 | + options are quite complex. |
| 196 | +
|
| 197 | +BSD May 19, 2002 BSD |
| 198 | +
|
| 199 | +``` |
| 200 | + |
| 201 | +::::::::::::::::::::::::: |
| 202 | + |
| 203 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 204 | + |
| 205 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 206 | + |
| 207 | +## Find out about advanced `ls` commands |
| 208 | + |
| 209 | +Find out, using the manual page, how to list the files in a directory ordered by their filesize. Try it out in different directories. Can you combine it with the `-l` *argument* you learned before? |
| 210 | + |
| 211 | +Afterwards, find out how you can order a list of files based on their last modification date. Try ordering files in different directories. |
| 212 | + |
| 213 | +::::::::::::::: solution |
| 214 | + |
| 215 | +## Answer |
| 216 | + |
| 217 | +To order files in a directory by their filesize, in combination with the `-l` argument: |
| 218 | + |
| 219 | +```bash |
| 220 | +ls -lS |
| 221 | +``` |
| 222 | + |
| 223 | +Note that the `S` is **case-sensitive!** |
| 224 | + |
| 225 | +To order files in a directory by their last modification date, in combination with the `-l` argument: |
| 226 | + |
| 227 | +```bash |
| 228 | +ls -lt |
| 229 | +``` |
| 230 | + |
| 231 | +::::::::::::::::::::::::: |
| 232 | + |
| 233 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 234 | + |
| 235 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 236 | + |
| 237 | +- Knowing where you are in your directory structure is key to working with the shell |
| 238 | + |
| 239 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 240 | + |
| 241 | + |
0 commit comments