QuillHub
QuillHub
EN

Missing Semester — Lecture 1: The Shell (Intro)

48:162 спикера
47:54
длительность
18
главы
2
спикеры
16
Термины
shell basicsfilesystem navigationI/O redirectionpipespermissionsroot and sudocommand-line tools

Спикеры

2
Спикер A99%3 реплики
Спикер B1%2 реплики

Основные идеи

7
  • The shell is a powerful composable interface for interacting with your computer beyond GUIs.
  • Learning textual tools and how to combine them yields much greater efficiency for development and research.
  • Most lectures are independent but assume basic shell knowledge introduced here.
  • Understand paths, permissions, environment variables (PATH), and standard streams to use the shell effectively.
  • Redirection and pipes let you rewire program input/output and build powerful pipelines.

Важные моменты

10
  • Course format: 11 one-hour lectures, mostly independent; notes and recordings posted after lectures.
  • Primary focus of this lecture: the shell (Bash) and command-line basics.
  • PATH environment variable determines where the shell searches for executables.
  • Paths: absolute vs relative, ~ expands to home, special entries . and ..
  • Common commands introduced: pwd, cd, ls, which, man, mkdir, mv, cp, rm, rmdir, cat, find, grep, curl, xdg-open.

Решения

4
  • Course will consist of 11 one-hour lectures covering specific topics; lectures mostly independent.
  • Lecture notes and recordings will be posted online after the lectures.
  • Office hours will be held after each lecture on the 9th floor of Building 32 (Gates/Stata).
  • Primary shell covered will be Bash (Linux-centric teaching approach while supporting other platforms).

Задачи

4
  • Check lecture notes and complete the exercises at the bottom of the lecture notes
  • Attend office hours on the 9th floor (Building 32) after each lecture for help
  • Review the next lecture’s prerequisites (shell basics) before tomorrow’s lecture
  • Use the course website email address to send questions (address listed on the website)

Термины

16
shell — A command-line/text-based interface (and small programming language) for interacting with the OS (Bash is a common shell).
PATH — Environment variable listing directories the shell searches to find executables by name.
environment variable — Named values provided to the shell and programs (e.g., HOME, PATH).
absolute path — Full path from the filesystem root (starts with / on Unix-like systems).
relative path — Path specified relative to the current working directory.

Основное содержание

18 глав · 47:54
0:00

1. Course overview and goals

Спикер A0:00

All right, everyone. Thanks for coming in. This is the missing semester of your CS education. At least that's what we chose to call the class. If you're not here for this class, then you're in the wrong room. We will be here for about an hour, just to set your expectations.

And I want to talk to you a little bit first about why we're doing this class. So this class stems out of an observation that Anish and Jose and I have made while TAing various classes at MIT, which is that basically all of us computer scientists, we know that computers are great at doing these repetitive tasks and automating things, but we often fail to realize that there are lots of tools that can make our own development processes better. We can be a lot more efficient about how we use our computers because we can use the computer as a tool for ourselves, not just for building websites or software, those sorts of things.

And this class is an attempt to address this. It's an attempt to show you some of the tools that you can use to great effect in your day-to-day, in your research, and in your studies. And it's going to be a class where we want to teach you both how to make the most of the tools that you already know, but also hopefully teach you about some tools you don't know from before, and how to combine those tools to produce more powerful things than you think you might be able to do with what you know today.

Um, the class is going to be structured as a series of 11 1-hour lectures, and each one is gonna cover a particular topic. You can see the website, which is also listed there, for the list of lecture topics and what date we'll do each one. They will mostly be independent, and so you can sort of show up for the ones that you're interested in. But we will sort of assume that you've been following along, so that as we get to later lectures, I'm not going to be teaching you Bash all over again, for example.

We are also going to post both the lecture notes and recordings of the lectures online. Exactly when we do that, we haven't established yet, but it will be after the lectures. Obviously, the videos have to be posted after. The class is going to be run by me, John, and Anish sitting over there, and José, who is not currently here but will be holding tomorrow's lecture.

And keep in mind that we're trying to cover a lot of ground over the course of just 11 1-hour lectures. And so we will be moving relatively rapidly, but please do stop us if there's anything where you feel like you're not following along. If you feel like there's something you wish we would spend more time on, just let us know. Please interrupt us with questions.

And also, after each lecture, we're going to hold office hours on the 9th floor of Building 32, the Stata Center, the computer science building. So if you show up in the 9th floor lounge there in the Gates Tower, then you can come and try some of the exercises that we give for each lecture, or just ask us other questions about things we've talked about in lecture or other things about using your computer efficiently.

Due to the limited time that we have available, we're not going to be able to cover all tools in full detail, and so what we'll try to do is highlight interesting tools and interesting ways to use them. We won't necessarily dig into the deep details about how all of it works or more elaborate use cases, but if you have questions about them, please come ask us about that too. Many of these tools are tools that we have used for years, and we might be able to point you to additional interesting things you can do with them. So, like, take advantage of the fact that we're here.

This class is going to, I don't want to say ramp up quickly, but what's going to happen over the course of this particular lecture is that we'll cover many of the basics that we assume that you will know for the rest of the semester. Things like how to use your shell and your terminal, and I'll explain what those are to those of you who are not familiar with them.

3:57

2. Introduction to the shell

Спикер A3:57

And then we'll pretty quickly ramp up into more advanced tools and how to use them. You can already see from the lecture notes the kind of topics that we're going to be covering. And so that brings us to today's lecture, in which we are going to cover the shell.

And the shell is going to be one of the primary ways that you interact with your computer once you want to do more things than what the sort of visual interfaces you might be used to allow you to do. The visual interfaces are sort of limited in what they allow you to do because you can only do the things that there are buttons for, sliders for, input fields for. Often, these textual tools are built to be both composable with one another, but also to have tons of different ways to combine them or ways to program and automate them.

And that is why in this class we will be focusing on these command-line or text-based tools. And the shell is the place that you will do most of this work.

4:46

3. Shells and platforms (Windows, macOS, Linux)

Спикер A4:46

So for those of you who are not familiar with the shell, most platforms provide some kind of shell. On Windows, this is often PowerShell, but there are also other shells available on Windows. On Linux, you will find tons of terminals. These are windows that allow you to display shells, and you also find many different types of shells. The most common of which is Bash, or the Bourne-again shell. Because it's such a common shell, it is the one we're primarily going to be covering in these lectures.

If you're on macOS, you will probably also have Bash, maybe an older version of it, if you open the Terminal app. And so if you want to follow along on any of these platforms, feel free, but keep in mind that most of this is going to be sort of Linux-centric in terms of how we teach it, even though most of these tools work on all the platforms. If you want to install a terminal and a shell, and you don't know how to do it, well, we're happy to show you at office hours, or it's usually very easy to just Google, like, your platform plus, like, terminal, and you will get one.

5:53

4. Shell prompt, commands, and quoting

Спикер A5:53

Now, when you open a terminal, you get something that looks a little bit like this. So it will usually have just a single line at the top, and this is what's known as the shell prompt. You can see that my shell prompt looks like this. It has my username, the name of the machine that I'm on, the current path I'm on, and we will talk about paths a little bit later. And then it's really just sort of blinking there, asking me for input. This is the shell prompt where you tell the shell what you want it to do.

You can customize this prompt a lot, and when you open it on your machine, it might not look exactly like this. It might look something like this, if you've configured it a little, or it might look all sorts of different ways. We won't go too much into customizing your shell in this particular lecture. We'll do that later. Here, we're just going to talk about how do you use this shell to do useful things. And this is sort of the main textual interface you have to your computer, is through the shell.

On the shell prompt, you get to write commands, and commands can be relatively straightforward things. Usually, it'll be something like executing programs with arguments. What does that look like? Well, one program we can execute is the date program. We just type date and press Enter, and then it will show you, unsurprisingly, the date and time.

You can also execute a program with arguments. This is one way to modify the behavior of the program. So, for example, there's a program called echo, and echo just prints out the arguments you give it, and arguments are just whitespace-separated things that follow the program name. So we can say hello, and then it will print hello back. Perhaps not terribly surprising, but this is the very basics of arguments.

One thing that you'll notice is that I said that arguments are separated by whitespace, and you might wonder, well, what if I want an argument that's multiple words? You can also quote things, so you can do things like echo hello space world, and now the echo program receives one argument that contains the string hello world with a space. You can also use single quotes for this, and the difference between single quotes and double quotes we'll get back to when we talk about bash scripting.

You can also just escape single characters. So for example, hello world, this will also work just fine. All of these rules about how you escape and how you parse and quote various arguments and variables we'll cover a little bit later. Hopefully, you won't run into too many oddities about this. Just keep in mind at least that spaces separate arguments.

8:25

5. Programs, PATH, and environment variables

Спикер A8:25

So if you want to do something like make a directory called my photos, you can't just type, like, mkdir my photos. It will create 2 directories, one called my and one called photos, and that is probably not what you want.

Now, one thing you might ask is, how does the shell know what these programs are? When I type date or when I type echo, how does it know what these programs are supposed to do? And the answer to this is your program— your computer has a bunch of built-in programs that come with the machine. Just like your machine might ship with like the terminal app, or it might ship with like Windows Explorer, or it might ship with some kind of browser, it also ships with a bunch of terminal-centric applications.

And these are stored on your file system. And your shell has a way to determine where a program is located. It basically has a way to search for programs. It does this through something called an environment variable. An environment variable is a variable like you might be used to for programming languages. It turns out that the shell, and the Bourne-again shell in particular, is really a programming language. This prompt that you're given here is not just able to run a program with arguments. You can also do things like while loops, for loops, conditionals, all of these—you can define functions, you can have variables, and all of these things you can do in the shell.

We'll cover a lot of that in the next lecture on shell scripting. For now though, let's just look at this particular environment variable. Environment variables are things that are set whenever you start your shell. They're not things you have to set every time you run your shell. There are a bunch of these that are set, things like where is your home directory, what is your username. And there's also one that's critical for this particular purpose, which is the PATH variable.

So if I echo out $PATH, this is going to show me all of the paths on my machine that the shell will search for programs. You'll notice that this is a list that is colon-separated. It might be kind of long and hard to read, but the essentials is that Bash, whenever you type the name of a program, is going to search through this list of paths on your machine, and it's going to look in each directory for a program or a file whose name matches the command you tried to run.

So in my case, when I try to run date or echo, it's going to walk through these one at a time until it finds one that contains a program called date or echo, and then it's going to run it. If we want to know which one it actually runs, there's a command called which, which lets us do that. So I can type which echo, and it will tell me that if I were to run a program called echo, I would run this one.

11:09

6. Paths and navigating the filesystem (pwd, cd)

Спикер A11:09

It's worth pausing here to talk about what paths are. So paths are a way to name the location of a file on your computer. On Linux and on macOS, these paths are separated by slashes, forward slashes. So you'll see here that this is in the root directory. So the slash at the very beginning indicates that this is starting from the top of the file system. Then look inside the directory called /usr, then look inside the directory /bin, and then look for the file called echo.

On Windows, paths like this are usually separated by backslashes instead. And where on Linux and macOS everything lives under the root namespace, so all paths start with a slash, or all absolute paths, on Windows there is one root for every partition. So you might have seen things like C:\ or D\. So Windows has separate sort of file system path hierarchies for each drive that you have. Whereas on Linux and macOS, these are all mounted under one namespace.

You'll notice that I said the word absolute path, and you might not know what that means. So absolute paths are paths that fully determine the location of a file. So in this case, this is saying—this is talking only about a specific echo file, and it's giving you the full path to that file. But there are also things known as relative paths. So a relative path is relative to where you currently are.

And so the way we find out where we currently are is you can type pwd for Present Working Directory. Present? Print Working Directory. Print Working Directory. So if I type pwd, it will print out the current path that I'm in, right? So currently I'm in the home directory under the root, and then John under that, and then dev under that, etc. From here, I can then choose to change my current working directory, and all relative paths are relative to the current working directory, which is basically where you currently are.

In this case, for example, I can do cd /home. cd is change directory. This is the way that I change what my current working directory is. In this case, I change to home, and I am now—you'll see my shell prompt changed to say that I am now in home. It just gives me the name of the last segment of the path, but you can also configure your terminal to give you the full path whenever you're anywhere.

And now if I type pwd again, it will tell me I'm in /home. There are also a couple of special directories that exist. There is dot and dot dot. Dot means the current directory. Dot dot means the parent directory. So this is a way that you can easily navigate around the system. For example, here, if I type cd.., it will tell me that I am now in slash. So I'm now in the root of the file system. I was in /home, now I'm in slash.

And indeed, if I type pwd, it will do that right thing. And I can also then use relative paths to go down into the file system, right? So I can do cd ./home, and this is gonna cd into the home directory under the current directory, right? So this will bring me back to /home. So if I now tried cd ./home again, it will say there's no such directory, because there is no home directory under the current directory I'm on, which I changed by doing cd, right?

And I can sort of cd all the way back to the place that I was using relative paths. And I can also do things like dot dot dot dot. What is cd..? ...to get back to somewhere deep in my file system. This happens to be all the way back to the root.

So here there's a bin directory, and under bin there's an echo file. And so then I could do world, and that runs the echo program under bin. Right, so this is a way that you can construct paths to arbitrarily traverse your file system. Sometimes you want absolute paths, and sometimes you want relative ones. Usually, you want to use whichever one is shorter.

But if you want to, for example, run a program or write a program that runs a program like echo or date, and you want it to be able to run— be run from anywhere, you either want to just give the name of the program, like date or echo, and let the shell use the path to figure out where it is, or you want to give its absolute path. Because if you gave a relative path, then if I ran it in my home directory and you ran it in some other directory, it might work for me but not for you.

In general, when we run a program, it is going to be operating on the current working directory, at least by default, unless we give it any other arguments. And this is really handy, because it means that often we don't have to give full paths for things. We can just use the name of files in the directory that we're currently in.

16:14

7. Listing files and useful navigation shortcuts

Спикер A16:14

One thing that's really useful is to figure out what is in the current directory we're in. So we already saw pwd, which prints where you currently are. There's a command called ls, which will show you— it will list the files in the current directory. So if I type ls here, this is all the files in the current directory, right? And this is a handy way to just quickly navigate through the file system.

You'll see that if I sort of cd.. and then do ls, it'll show me the files in that directory instead. But with ls, I can also give it ls.., like, I can give it a path, and then it will ls that file instead of the one that I'm currently in, or ls that directory. And you can see this if I go all the way to the root as well, right? Root has different files.

One handy trick you might not know about here is there are 2 other special things you can do. One is the tilde character. This character brings you to your home directory. So tilde always expands to the home directory, and you can do relative paths to it. So I can do ~/dev/pdos/classes/missing semester, and now I'm there, because tilde expanded to /home/john.

There is also, for cd in particular, a really handy argument you can give, which is dash. If you do cd-, it will cd to the directory you were previously in. So if I do cd-, I go back to root. If I do cd- again, I go back to missing-semester. So this is a handy way if you want to toggle between 2 different directories.

17:57

8. Command-line Arguments, Flags and --help

Спикер A17:57

In the case of ls, or in the case of cd, there might be arguments that you don't know about, right? Currently, we haven't really been doing anything except giving paths. But how do you even discover that you can give a path to ls in the first place? Well, most programs take what are known as arguments, like flags and options. These are things that usually start with a dash.

One of the handiest one of these is --help. Most programs implement this, and if you run, for example, ls --help, it'll helpfully print out a bunch of information about that command. And you'll see here that it says the usage is ls, and you can give some number of options, and you can give some number of files.

The way to read that usage line is triple dot means one, like, zero or one or more, and the square bracket means optional. So in this case, there's an optional number of options, and there's an optional number of files. And you'll see that it says what the program does, and also specifies a number of different types of flags and options you can give.

Usually, we call things that are a single dash and a single letter a flag, and anything that— or anything that doesn't take a value a flag, and anything that does take a value an option. So for example, -a and --all are both flags, and -c or --color is an option.

19:16

9. File Listings and Permissions (ls -l)

Спикер A19:16

One thing you'll see under here, if you scroll down far enough, is the -l flag. Oop, that's unhelpful. The -l flag uses a long listing format. Not that that's particularly helpful in and of itself, but let's see what it actually does. So if I do ls -l, it still prints the files in the current directory, but it gives me a lot more information about those files.

And this is something you'll find yourself using quite a lot. Because the additional information it gives you is often quite handy. Let's look at what some of that information is. Ah, so first of all, the D at the beginning of some of these entries indicates that something is a directory. So the _data entry here, for example, is a directory, whereas 404.html is not a directory, it's a file.

The following letters after that indicate the permissions that are set for that file. So this, like we saw earlier, I might not be able to open a given file, or I might not be able to cd into a given directory, and this is all dictated by the permissions on that particular file or directory.

The way to read these is that the first group of 3 are the permissions that are set for the owner of the file. All of these files you'll see are owned by me. The second group of 3 characters is for the permissions for the group that owns this file. In this case, all of these files are also owned by the john group. And a final group of 3 is a list of the permissions for everyone else, so anyone who's not a user owner or a group owner.

This directory is perhaps kind of boring because all of the things are owned by me. But if we do something like cd / and do ls -l, you'll see that here all of them are owned by root. We'll get back to what the root user is. But here you'll see some of the permissions are a little bit more interesting.

The groups of 3 are read, write, and execute. What these mean differs for files and for directories. For files, it's pretty straightforward. If you have read permissions on a file, then you can read its contents. If you have write permissions on a file, then you can save the file. You can add more to it, or you can replace it entirely. And if you have execute, the x bit, on a file, then you're allowed to execute that file.

So if we do ls -al in /bin— that's unhelpful— in /usr/bin, you'll see that all of them have the execute bit set, even for people who are not the owner of the file. And this is because the echo program, for example, we want everyone on the computer to be able to execute. There's no reason to say only certain users can run echo. That doesn't really make any sense.

For directories though, these permissions are a little bit different. So read translates to, are you allowed to see which files are inside this directory? So think of read as list for a directory. Are you allowed to list its contents? Write for a directory is whether you are allowed to rename, create, or remove files within that directory. So it's still kind of write, but notice that this means that if you have write permissions on a file, but you do not have write permissions on its directory, you cannot delete the file. You can empty it, but you cannot delete it, because that would require writing to the directory itself.

And finally, execute on directories is something that trips people up a lot. Execute on a directory is what's known as search, and that's not terribly helpful a name, but what that means is, are you allowed to enter this directory? If you want to get to a file, if you want to open it or read it or write it, whatever you want to do, basically to cd into a directory, you must have the execute permission on all parent directories of that directory and the directory itself.

So for example, for me to access a file inside /usr/bin, such as /usr/bin/echo, I must have execute on root, I must have execute on user, and I must have execute on bin. If I do not have all those execute bits, I will not be allowed to access that file, because I won't be able to enter the directories along the way.

There are a number of other bits that you might come across, like you might see S's or T's in these lists, you might see L's. Those we can talk about in office hours if you're curious. They will mostly not matter for anything you will do in this class, but they are handy to know about. So if you're curious about them, look them up on your own or come ask us in office hours.

There are some other programs that are handy to know about. Oh, sorry, there's one more thing I should mention. If you just have a dash, it means you do not have that permission. Right, so if it says, for example, r-x, it means that you have read and execute, but you do not have write.

24:24

10. File Operations and Getting Help (mv, cp, rm, mkdir, man)

Спикер A24:24

There are some other handy programs to know about at this point. One of them is move, or the mv command. So if I cd back to missing-semester here, mv lets me rename a file. And rename here takes 2 paths. It takes the old path and the new path. This means that move lets you both rename a file, like if you change the name of the file but not the directory, or it lets you move a file to a completely different directory.

It just— you give the path to the current file and the path to where you want that file to be, and that can change its location and its name. So for example, I can move .files.md to be foo.md, unhelpfully, right? And similarly, I can move it back.

There's also the cp command. The cp, or copy, is very similar. It lets you copy a file. cp also takes 2 arguments. It takes the path that you want to copy from and the path you want to copy to, and these are full paths. So I could use this, for example, to say I want to copy .file.md to ../food.md. Sure, food.md. And now if I do ls .., you'll see that there's a food.md file in that directory.

So, cp as well takes 2 paths. It does not have to be in the same directory. And similarly, there's the rm command, which lets you remove a file, and there too you can give paths. In this case, I'm removing ../food. You should be aware, for removing, especially on Linux, removal is by default not recursive. So you cannot remove a directory using rm. You can pass the -r flag, which lets you do a recursive remove, and then give a path that you want to remove, and it will remove everything below it.

There is also the rmdir command, which lets you remove a directory, but it only lets you remove that directory if it is empty. So the idea here is to sort of be a safety mechanism for you, so you don't accidentally throw away a bunch of your files. And the final little command that's handy to use is mkdir, which lets you create a new directory. And as we talked about before, you don't want to do something like this, because it will create 2 directories for you, one called my and one called photos. If you actually want to create a directory like this, you would either escape the space or quote the string.

If you ever want more information about how any command, basically, on these platforms work, there's a really handy command for that as well. There is the program called man, for manual pages. This program takes as an argument the name of another program and gives you its manual page. So for example, we could do man ls, and this shows us a manual page for ls. You'll notice that in the case of ls, it is fairly similar to what we got with ls --help, but it's a little easier to navigate, a little easier to read. Usually towards the bottom, you will also get examples, information about who wrote it, where you can find more information, and that sort of stuff.

One thing that can be confusing sometimes, at least until a recent version where they added this string at the bottom where it says Q to quit—it did not used to say this—you press Q to quit this program. It can be really hard to quit it if you do not know that. A handy keyboard shortcut here, by the way, is Ctrl+L, which lets you clear your terminal and go back to the top.

27:55

11. Input/Output Redirection and Pipelines

Спикер A27:55

So, so far, we've only talked about programs in isolation, but where much of the power of the shell really comes through is once you start combining different programs, right? So rather than just, like, running cd, running ls, etc., you might want to chain multiple programs together. You might want to interact with files and have files operate in between programs. And the way we can do this is using this notion of streams that the shell gives us.

Every program by default has—I'm gonna simplify a little and say 2 primary streams. It has an input stream and an output stream. By default, the input stream is your keyboard. Basically, the input stream is your terminal, and whatever you type into your terminal is gonna end up into the program. And it has a default output stream, which is whenever the program prints something, it's gonna print to that stream, and by default, that is also your terminal. This is why when I type echo hello, it gets printed back to my terminal.

But the shell gives you a way to rewire these streams, to change where the input and output of a program are pointed. The way—the most straightforward way you do this is using the angle bracket signs. So you can write something like this: Or you can write something like this. The left angle bracket indicates rewire the input for this program to be the contents of this file, and the end angle bracket means rewire the output of the preceding program into this file.

So let's look at an example of what that would look like. If I do echo hello, I can say I want that content to be stored in a file called hello.txt. And because I gave—this is a relative path, right? This will construct a file in the current directory called hello.txt, and at least in theory, its contents should be the word hello. So if I run this, notice that nothing got printed to my output. The previous time when I ran echo hello, it printed hello. Now that hello has gone into a file called hello.txt, and I can verify this by using the program called cat.

So cat prints the contents of a file. The contents of a file. So I can do cat hello.txt, and there it shows me hello. But cat is also something that supports this kind of wiring. So I can say cat, which by default just prints its input, it just duplicates its input to its output. I can say, I want you to take your input from hello.txt. What will happen in this case is that the shell is going to open hello.txt, take its contents and set that to be the input of cat, and then cat is gonna just print that to its output, which since I haven't rewired it, it's just gonna be my terminal. So this will just print hello to the output.

And I can use both of these at the same time. So, for example, if I want to copy a file and I don't want to use the cp command for some reason, I can do this. And in this case, I'm telling the cat program nothing at all. I'm just saying, do your normal thing, right? The cat program does not know anything about this redirection, but I'm telling the shell to use hello.txt as the input for cat, and to write anything that cat prints to hello2.txt.

Again, this prints nothing to my terminal, but if I cat hello2.txt, I get the output as I would have expected, which is a copy of the original file. There is also a double end bracket, which is append instead of just overwrite. So you'll notice that if I do cat hello.txt to hello2.txt again, and then I cat hello2.txt, it still just contains hello, even though it already contained hello. If I switch that to instead be a double end bracket, it means append, and if I now cat that file, it has hello twice.

These are pretty straightforward, and they're usually just ways to interact with files, but where it gets really interesting is an additional operator the shell gives you called the pipe character. So, pipe is just a vertical bar, and what pipe means is take the output of the program to the left and make it the input of the program to the right. Right? So, what does this look like? Well, let's take the example of ls/ or ls -l/. This prints a bunch of things. Let's say that I only wanted the last line of this output.

Well, there's a command called tail, and tail prints the last n lines of its input. And I can do -n 1. So, this is a flag called n. You can also use --lines if you want to use it as a longer option. But in this case, this is saying just print the last line. And I can wire these together. So, I can say ls -l/ | tail -n 1. And notice here that ls does not know about tail, and tail does not know about ls. They are different programs and have never been programmed to be compatible with one another.

All they know how to do is read from input and write to output. And then the pipe is what wires them together. And in this particular case, I'm saying I want the output of ls to be the input to tail, and then I want the output of tail to just go to my terminal, because I haven't rewired it. I could also rewire this to say I want the output to go to ls.txt, and in this case, if I cat ls.txt, I would get the appropriate output.

And it turns out you can do some really neat things with this. We're gonna cover this a lot more in the data wrangling lecture that will be in, I think, 4 days or something, on the kind of fancy stuff you can do when you start building more advanced pipelines. One— to give you one example, we can do something like curl --head silent google.com. So, just to show you what that looks like, this gives me all the HTTP headers for accessing google.com, and I can pipe that to grep --ignorecase or just -i.

What's the difference between if I want content-length? So, this is gonna print the content-length header. Grep is a program that we'll talk about later that lets you search in an input stream for a given keyword. We can pipe that through, say, the cut command, which takes a delimiter. Set that to be space, and I want the second field. And this prints just the content-length.

This is sort of a silly example, right? Like, this just lets you extract the content-length in bytes of google.com from the command line. It is not a very useful thing to do, but you can see how by chaining these together you can achieve a bunch of really interesting text manipulation effects. And it turns out pipes are not just for textual data. You can do this for things like images as well. You can have a program that manipulates a binary image on its input and writes a binary image to its output, and you can chain them together in this way.

And we'll talk about some of those kinds of examples later on too. You can even do this for video if you want. You can stream— this is, for example, a great way if you have a Chromecast at home, you can stream a video file like this by having the last program in your pipe be a Chromecast send program. So, you stream a video file into it and it streams over HTTP to your Chromecast.

35:16

12. Using the Terminal More Powerfully / Introduction to the Root User

Спикер A35:16

We'll take a— we'll talk a lot more about this in the data wrangling lecture, but there's one more thing that I wanted to talk to you about, about sort of how to use the terminal in a more interesting and perhaps more powerful way than you might be used to. And this is perhaps even going to be interesting for the ones of you who feel like you're already comfortable with the terminal.

But first, we need to cover an important topic when it comes to Linux systems and macOS systems in particular, which is the notion of the root user. The root user is sort of like the administrator user on Windows, and it has user ID 0. The root user is special because it is allowed to do whatever it wants on your system. Even if a file is, like, not readable by anyone, or if it's not writable by anyone, root can still access that file.

Root is sort of a superuser that gets to do whatever they want. And most of the time, you will not be operating as the superuser. You will not be root. You will be a user like John or whatever your name is, and that's gonna be the user you act with. Because if you were operating your computer as the root user at all times, if you ran the wrong program, it could just completely destroy your computer. You don't want that, right?

But every now and again, you want to do something that requires that you are root. Usually for these cases, you will use a program called sudo. S-U-D-O. Or, do as su, and su in this case is superuser. So this is a way to do the following thing as the superuser. Usually, the way sudo works is you write sudo, and then a command like you would normally on your terminal, and it will just run that command as if you were root, as opposed to the user you actually are.

Where might you need something like this? Well, there is a special— there are many special file systems on your computer, but in particular, there's one called sysfs. If you cd to /sys, this is a whole new world. This file system is— are not actually files on your computer. Instead, these are various kernel parameters. So the kernel is like basically the core of your computer. This is a way for you to access various kernel parameters through what looks like a file system.

You'll see here that if I cd into class, for example, it has directories for a bunch of different types of devices that I can interact with, or various queues I can access, or all sorts of weird knobs internally. And because they're exposed as files, it means we can also use all the tools we've been using so far in order to manipulate them. One example of this is if you go into /sys/class/backlight. So this backlight directory lets you configure the backlight on your laptop if you have one.

38:00

13. Kernel Interfaces and Redirection Permission Issues

Спикер A38:00

So I can cd into intel-backlight. This is an Intel laptop. Inside here, you'll see there's a file called brightness, and I can cat the brightness. This is the current brightness of my screen. But not only that, I can modify this too in order to change the brightness of my screen. So you might think that I could do— let's see what the max brightness is here. Okay, so it's currently set to the max brightness. You might imagine that I could do something like, if I do echo— let's do half or something— echo 500 to brightness.

If I do this, it says permission denied. I'm not allowed to modify brightness because in order to— basically in order to change things in the kernel, you need to be the administrator. And you might imagine that the way to solve this is to write sudo echo 500, but I still get a permission denied error. But why is that? It's because, as I mentioned before, these redirections of input and output is not something the programs know about.

When we piped ls into tail, tail did not know about ls, and ls did not know about tail. The pipe and the redirection was set up by the shell. So in this case, what's happening is I'm telling my shell, run the program sudo with the arguments echo and 500, and send its output to the file called brightness. But the shell is what is opening the brightness file. It is not the sudo program.

So in this case, the shell, which was running as me, tries to open the brightness file for writing, and it's not allowed to do that, and therefore I get a permission denied error. You might have seen this if you, like, search for something, end up on Stack Overflow, and it tells just run this command, and you'll see that it does something like— they give you instructions like 1 to sys— what's an example? Net IPv4 forward, for example. This is something you may have seen if you're setting up a firewall.

And this command is intended to work because this little pound symbol indicates run this as root. This is something that is very rarely explained, but that is what the pound symbol means. You'll see on my prompt there's a dollar symbol instead, and the dollar indicates you are not running as root. So the question is, how do I get around this?

Well, I could switch into a root terminal. So one way to do this is to run sudo su. sudo su is saying run the following command as root, and su is a complicated command that effectively gets you a shell as the superuser. So if I do this, type a password, then now you'll see that the username at the beginning changed from John to root, and the prompt changed from a dollar to a pound.

If I now echo into that file, if I do echo 500 to brightness, my screen got a little dimmer, but you can't see it. You just have to trust me. And now I didn't get an error. And this is because the shell is now running as root. It is not running as John, and the root user is allowed to open this file.

But given our knowledge that we have of the terminal now, there's actually a way for us to do this without having to drop to a root shell. And that is as follows. Let's, I guess, restore it to 1060. So do you see why this is different? Here, I'm telling my shell to run the echo 1060 command, which is going to echo 1060, and I'm telling it to run the sudo tee brightness command, and I'm telling you to send the output of echo into sudo tee.

41:32

14. Using sudo with tee and Controlling Hardware (Brightness & LEDs)

Спикер A41:32

In order to understand this, you need to know what the tee command does. The tee command takes its input and writes it to a file, but also to stdout. So, tee is a convenient way if you have, say, a log file that you want to, like, send to a file to store for later, but you also want to see it yourself. Then you can pipe it through tee, give it the name of a file, and it will write whatever its input is both to that file and to your screen.

And here I'm taking advantage of that program. I'm saying run tee as root and have tee write into the brightness file. And so, in this case, the tee program, which is what is opening the brightness file, is running as root. And so it is allowed to do this. If I run this, it will now—again, you can't see, but the brightness has now been turned on on my laptop, and I don't get any errors, and I never had to drop into a root shell and run commands there, which can often be somewhat dangerous.

If you want to explore this file system a little bit more, there's a lot of interesting stuff in here. If you just sort of start browsing around, you can find all sorts of fun things. So, for example, we noticed that there was a fun brightness command here. I wonder what other kinds of brightness I can set. So, I can use the find command, which we'll also talk about in a coming lecture. I want to look at any file whose name is a little like brightness in the current directory.

Ah, that's unhelpful. Maybe they're not files. Did I misspell brightness? Why is it being annoying? Eh? Well, apparently it does not want to search for brightness for me. Oh well. Luckily for you, I know of one already. Handy that. There is a subdirectory called LEDs, and LEDs have brightness too. What kind of LEDs are there? Ooh, lots of things. For example, the Scroll Lock LED.

Now, most of you probably don't know what the Scroll Lock LED is, or much less what Scroll Lock is. But you might have seen a key on your keyboard named Scroll Lock. Basically, no one knows what it means anymore. No one really uses it for anything. It's mostly just a dead key and also a dead LED. What if you wanted to configure it so that every time you get email, your Scroll Lock LED lights up? Because there's no other reason why it would light up.

Well, if we cd into this particular directory, there's a brightness place, and it's set to 0. Well, what happens if I write 1 into it? You probably should not just be writing random numbers into random files in this directory, because you are affecting your kernel directly. Like, look up what the files do. In this particular case, I have worn safety goggles, and I've done my research.

So now you can't tell, but on my keyboard, the Scroll Lock LED is now lit. So now if I wrote a program that did some checking of mail and stuff, I could have it at the end run a program that echoes 1 into this file, and now I have a way for my LED to my keyboard to indicate when I have new email. At this point, you should know roughly your way around the terminal, around the shell, and know enough to accomplish at least basic tasks.

44:55

15. Opening Files from the Terminal and Next Lectures

Спикер A44:55

At least in theory now, you shouldn't need to use, like, point-and-click interfaces to find files anymore. There's one remaining trick you might need, and that is the ability to open a file. So far, I've only really given you ways to find files, but one thing you should know about is... ls -l. Amazing semester.

xdg-open. This will probably only work on Linux. On macOS, I think it's just called open. On Windows, who knows. xdg-open, you give the name of a file, and it will open it in the appropriate program. So if you open—if you do xdg-open an HTML file, it will open your browser and open that file. And once you have that program, in theory, you should no longer need to open like a Finder window ever again.

You might want to for other reasons, but in theory, you can accomplish it all using the tools that we've learned today. This might all seem relatively basic for some of you, but as I mentioned, this is sort of the ramp-up period of now we all know how the shell works, and a lot of what we'll be doing in future lectures is using this knowledge to do really interesting things using the shell.

That's sort of—this is learning the interface that we're going to be using, and so it's important we all know it. We're gonna talk a lot more in the next lecture about how to automate tasks like this, how to write scripts that run a bunch of programs for you, and how to do things like conditionals and loops and stuff in your terminal, and do things like run a program until it fails, which can be handy in classes where you want to run something until your test suite fails, for example.

So that's the topic for next week's lecture. Did you have a question?

46:35

16. Q&A: sys filesystem and Windows Subsystem for Linux

Спикер B46:35

Yeah, just related to what you've been demoing, the sys directory, that presumably will only work if you're dual booting into Linux, not if you're running Windows Subsystem for Linux?

Спикер A46:47

That is a good question. I don't know whether the Windows Subsystem for Linux will expose the sys file system. If it does, it probably only exposes a very small number of things. It might, because there are—

Спикер B47:01

The LEDs or the screen brightness, for example?

47:04

17. Lecture resources, exercises, and office hours

Спикер A47:04

I don't know. Check it out. One thing you'll see is the lecture notes for this lecture are already online, and at the very bottom of the file, there are a bunch of exercises. Some of them are relatively easy, some of them are a little bit harder, and what we encourage you to do is to take a stab at going through them.

If you know this stuff already, it should go really quickly. If you don't, it might teach you a bunch of things that you might not realize you didn't know. And for the office hours that we're gonna do right after this lecture, we will happily help you get through all of those. Or if there are other commands you learn in the process you want to know how to use more efficiently.

And then in the next lecture, which is tomorrow, we'll basically be assuming that you know the kind of stuff that the exercise is going to teach you. There's also an email address on the website where you can send us questions if you think of something like after the office hours have finished.

47:54

18. Closing remarks and meeting logistics

Спикер A47:54

Are there any questions before we end today? No? No? All right. Well, we will have office hours on the 9th floor of the Gates Building of Building 32 in like 5 minutes. Sweet. See you there. Oh, hello.