Up: The Shell

Files and Directories

slide 01 Hello, and welcome to the second episode of the Software Carpentry lecture on the Unix shell. In this episode, we’ll have a look at how files and directories are organized, and how to navigate around them.
slide 02 As we said in the last episode, a computer has four main jobs: run programs, store data, communicate with other computers, and interact with us.
slide 03 One way the computer can interact with us is through a command shell: we type commands, the shell tells the computer to run programs on our behalf, and then the shell shows us the output from those programs.
slide 04 Some of the commands we will use most often are ones related to storing data on disk.
slide 05 The subsystem reponsible for this is called the file system.
slide 06 It organizes our data into files, which hold information…
slide 07 …and directories, which hold files or other directories.
slide 08 In the next few minutes, we’ll see how we can use the shell to view what’s in the file system.
slide 09 Or to be more precise, how we can use the shell to run other programs that will show us what’s in the file system.
slide 10 Let’s start by logging in to the computer.
slide 11 Here, we’re showing the shell’s prompt in bold.
slide 12 And explanatory text (like this message) in blue.
slide 13 Type our user ID—we’ll show user input in green.
slide 14 And then our password. Most systems will print stars to obscure it, or nothing at all, in case some evildoer is shoulder surfing behind us.
slide 15 Once we have logged in, we’ll see a shell prompt, which is usually just a dollar sign (but which may show extra information, like our user ID).
slide 16 The shell prompt is exactly like Python’s >>> prompt: it signals that the shell is waiting for us to type something in.
slide 17 Type “whoami”, followed by “enter”. This command prints out the ID of the current user, i.e., shows us who the shell thinks we are.
slide 18 When we enter it, the shell finds a program called whoami
slide 19 …runs it…
slide 20 …displays its output…
slide 21 …and then displays a new prompt, telling us that it’s ready for more commands.
slide 22 Now that we know who we are, we can find out where we are using pwd, which stands for “print working directory”.
slide 23 This is our current default directory, i.e., the directory the computer assumes we want to use unless we specify something else explicitly.
slide 24 The computer’s response is /users/vlad. To understand what this means, let’s have a look at how the file system as a whole is organized.
slide 25 At the very top of the file system is a directory called the root directory that holds everything else the computer is storing.
slide 26 When we want to refer to it, we just use a slash character /.
slide 27 This is the leading slash in /users/vlad.
slide 28 Inside that directory (or underneath it, if you’re drawing a tree) are several other directories, such as bin (which is where some built-in programs are stored)…
slide 29 data
slide 30 users (where users’ personal directories are located)…
slide 31 tmp (for temporary files that don’t need to be stored long-term), and so on.
slide 32 We know that our current working directory, /users/vlad, is stored inside /users because /users is the first part of its name. Similarly, we know that /users is stored inside the root directory / because its name begins with /.
slide 33 Underneath /users, we find one directory for each user with an account on this machine. The mummy’s files are stored in /users/imhotep, the Wolfman’s in /users/larry
slide 34 …and ours in /users/vlad
slide 35 …which is why vlad is the last part of the directory’s name.
slide 36 Notice, by the way, that there are two meanings for the / character. When it appears at the front of a file or directory name, it refers to the root directory. When it appears inside a name, it’s just a separator.
slide 37 Let’s see what’s inside Vlad’s home directory by running ls, which stands for “listing”.
slide 38 It’s not a particularly memorable name, but as we’ll see, many others are unfortunately even more cryptic.
slide 39 ls prints the names of all the files and directories in the current directory in alphabetical order, arranged neatly into columns.
slide 40 To make its output more comprehensible, we can give it the argument, or flag, ls -F.
slide 41 This tells ls to add a trailing / to the names of directories. As you can see, there are seven of these. The names without slashes—notes.txt, pizza.cfg, and solar.pdf—are plain old files.
slide 42 Here’s that output again, with a picture of what it’s showing us.
slide 43 You may have noticed that the files’ names are all something dot something. By convention, the second part, called the filename extension, indicates what type of data the file holds.
slide 44 .txt signals a plain text file, .pdf indicates a PDF document, .cfg is a configuration file full of parameters for some program or other, and so on.
slide 45 However, this is only a convention, and not a guarantee. Files contain bytes, nothing more; it’s up to us and our programs to interpret those bytes according to the rules for PDF documents, images, and so on.
slide 46 Now let’s run the command ls -F data, which tells ls to give us a listing of what’s in our data directory.
slide 47 The output shows us that there are four text files and two directories. This hierarchical organization helps us keep our work organized.
slide 48 Notice while we’re here how we spelled the directory name data. Since it doesn’t begin with a slash, it’s a relative path
slide 49 …i.e., it’s interpreted relative to the current working directory.
slide 50 If we run ls -F /data, we get a different answer…
slide 51 …because /data is an absolute path.
slide 52 The leading / tells the computer to follow the path from the root of the filesystem…
slide 53 …so it always refers to exactly one directory, no matter where we are when we run the command.
slide 54 What if we want to change our current working directory? pwd shows us that we’re still “in” /users/vlad
slide 55 …and ls without any arguments shows us its contents.
slide 56 We can use cd followed by a directory name to change our working directory.
slide 57 cd stands for “change directory”…
slide 58 …which is a bit misleading: the command doesn’t change the directory…
slide 59 …it changes the shell’s idea of what directory we are in.
slide 60 cd doesn’t print anything, but if we run pwd after it, we can see that we are now “in” /users/vlad/data.
slide 61 If we run ls without arguments now, it lists the contents of /users/vlad/data
slide 62 …because that’s where we now are.
slide 63 OK, we can go down the directory tree: how do we go up? If we’re still in /users/vlad/data
slide 64 …we can use cd .. to up one level.
slide 65 .. is a special directory name meaning “the directory containing this one”.
slide 66 Or more succinctly, the parent of the current directory.
slide 67 Sure enough, if we run pwd after running cd .., we’re back in /users/vlad.
slide 68 The special directory .. doesn’t usually show up when we run ls.
slide 69 If we add the -a flag, though, it will be displayed.
slide 70 -a stands for “show all”.
slide 71 It forces ls to show us directory names that begin with ., such as ..
slide 72 (which, if we’re in /users/vlad, points to the /users directory)
slide 73 and also another special directory that’s just called ., which is the directory we’re currently in. It may seem redundant to have a name for where we are, but we’ll see some uses for it in later episodes.
slide 74 Everything we have seen so far works on Unix and its descendents, such as Linux and Mac OS X. Things are a bit different on Windows.
slide 74 Here’s a typical directory path on a Windows 7 machine.
slide 74 The first part, C:, is a drive letter. This notation dates back to the days of floppy drives…
slide 74 …and even today, each drive is a completely separate filesystem.
slide 74 Instead of a forward slash, Windows uses backslash to separate the names in a path.
slide 74 This causes headaches because Unix uses backslash to escape special characters. For example, if you want to put a space in a filename, you would write it as \ (backslash followed by space). Please don’t ever do this, though: if you put spaces, question marks, and other special characters in filenames on Unix, you’re likely to confuse the shell and a lot of other tools.
slide 74 Finally, Windows filenames and directory names are case insensitive: upper and lower case letters mean the same thing.
slide 74 This means that the path name C:\Users\Vlad could be spelled in 1024 different ways. Some people argue that this is more natural—after all, “VLAD” in all upper case and “Vlad” spelled normally refer to the same person—but it does cause some headaches for programmers, and can be difficult for people whose first language doesn’t use a cased alphabet to understand.
slide 74 The Cygwin package tries to make Windows paths look more like Unix paths by allowing us to refer to the C drive as /cygdrive/c/ instead of as C: (although the latter does usually work too).
slide 74 It also allows us to use forward slash instead of backslash as a separator.
slide 74 But paths are still case insensitive…
slide 74 …which means that if you try to copy files called backup.txt (in all lower case) and Backup.txt (with a capital ‘B’) into the same directory, the second will overwrite the first.
slide 74 To summarize, here are the three commands, and two special directory names, that we saw in this episode.
slide 74 In the next episode, we’ll see how to create, rename, and delete files and directories.

  1. No comments yet.
  1. No trackbacks yet.