< Return to Video

Lecture 1: Course Overview + The Shell (2020)

  • 0:01 - 0:08
    all right everyone thanks for coming in.
  • 0:02 - 0:08
    This is the missing semester of your CS Education;
  • 0:08 - 0:12
    at least that's what we chose to call
  • 0:10 - 0:14
    the class. if you're not here for this
  • 0:12 - 0:16
    class then you're in the wrong room. We
  • 0:14 - 0:20
    will be here for about an hour just to
  • 0:16 - 0:21
    set your expectations and I want to talk
  • 0:20 - 0:24
    to you a little bit first about why
  • 0:21 - 0:26
    we're doing this class. So this class
  • 0:24 - 0:30
    stems out of an observation that Anish
  • 0:26 - 0:33
    and Jose and I have made while TA-ing various
  • 0:30 - 0:35
    classes at MIT which is that basically
  • 0:31 - 0:35
    all of us computer scientists, we know
  • 0:35 - 0:38
    that computers are great at doing these
  • 0:36 - 0:41
    repetitive tasks and automating things
  • 0:38 - 0:43
    but we often fail to realize that there
  • 0:41 - 0:46
    are lots of tools that can make our own
  • 0:43 - 0:48
    development processes better. We can be a
  • 0:46 - 0:50
    lot more efficient about how we use our
  • 0:48 - 0:52
    computers because we can use the
  • 0:50 - 0:54
    computer as a tool for ourselves not
  • 0:52 - 0:58
    just for building websites or software
  • 0:54 - 1:00
    those sorts of things and this class is
  • 0:58 - 1:03
    an attempt to address - this is an attempt
  • 1:00 - 1:05
    to show you some of the tools that you
  • 1:03 - 1:08
    can use to great effect in your day to
  • 1:05 - 1:12
    day in your research and in your studies
  • 1:08 - 1:15
    and it's going to be a class where we
  • 1:10 - 1:15
    want you to teach you both how to make
  • 1:15 - 1:19
    the most of the tools that you already
  • 1:16 - 1:21
    know but also hopefully teach you about
  • 1:19 - 1:24
    some tools so you don't know from before
  • 1:21 - 1:26
    and how to combine those tools to
  • 1:24 - 1:28
    produce more powerful things than you
  • 1:26 - 1:31
    think you might be able to do with what
  • 1:28 - 1:33
    you know today the class is going to be
  • 1:31 - 1:37
    structured as a series of 11 one-hour
  • 1:33 - 1:39
    lectures and each one is going to cover
  • 1:37 - 1:41
    a particular topic you can see the
  • 1:39 - 1:43
    website which is also listed there for
  • 1:41 - 1:46
    the list of lecture topics and what date
  • 1:43 - 1:48
    will do each one they will mostly be
  • 1:46 - 1:49
    independent and so you can sort of show
  • 1:48 - 1:51
    up for the ones that you're interested
  • 1:49 - 1:54
    in but we will sort of assume that
  • 1:51 - 1:56
    you've been following along so that as
  • 1:54 - 1:57
    we get to later lectures I'm not going
  • 1:56 - 2:02
    to be teaching you bash all over again
  • 1:57 - 2:05
    for example we we are also going to post
  • 2:02 - 2:08
    both the lecture notes and recordings of
  • 2:05 - 2:10
    the lectures online exactly when we do
  • 2:08 - 2:12
    that we haven't established yet but it
  • 2:10 - 2:14
    will be after the lectures obviously the
  • 2:12 - 2:17
    videos have to be posted after
  • 2:14 - 2:19
    the class is gonna be run by me John and
  • 2:17 - 2:21
    Anne each sitting over there and Jose
  • 2:19 - 2:24
    who is not currently here but we'll be
  • 2:21 - 2:26
    holding tomorrow's lecture and keep in
  • 2:24 - 2:28
    mind that we're trying to cover a lot of
  • 2:26 - 2:31
    ground over the course of just 11
  • 2:28 - 2:33
    one-hour lectures and so we will be
  • 2:31 - 2:35
    moving relatively rapidly but please do
  • 2:33 - 2:37
    stop us if there's anything where you
  • 2:35 - 2:38
    feel like you're not following along if
  • 2:37 - 2:40
    you feel like there's something you wish
  • 2:38 - 2:42
    we would spend more time on just let us
  • 2:40 - 2:45
    know please interrupt us with questions
  • 2:42 - 2:47
    and also after each lecture we're going
  • 2:45 - 2:50
    to hold office hours on the ninth floor
  • 2:47 - 2:52
    of building 30 to the Stata Center of
  • 2:50 - 2:53
    the computer science building as if you
  • 2:52 - 2:56
    show up up in the ninth floor lounge
  • 2:53 - 2:58
    there and the gates tower then you can
  • 2:56 - 3:01
    come and try some of the exercises that
  • 2:58 - 3:03
    we give for each lecture or just ask us
  • 3:01 - 3:04
    other questions about things we've
  • 3:03 - 3:08
    talked about in lecturer or other things
  • 3:04 - 3:08
    about using your computer efficiently
  • 3:08 - 3:14
    due to the limited time that we have
  • 3:12 - 3:17
    available we're not going to be able to
  • 3:14 - 3:19
    cover all tools and full detail and so
  • 3:17 - 3:21
    we'll try to do is highlight interesting
  • 3:19 - 3:23
    tools and interesting ways to use them
  • 3:21 - 3:25
    we won't necessarily dig into the deep
  • 3:23 - 3:28
    details about how all of it works or
  • 3:25 - 3:29
    more elaborate use cases but if you have
  • 3:28 - 3:31
    questions about them please come ask us
  • 3:29 - 3:34
    about that too many of these tools are
  • 3:31 - 3:35
    tools that we have used for years and we
  • 3:34 - 3:37
    might be able to point you to additional
  • 3:35 - 3:39
    interesting things you can do with them
  • 3:37 - 3:43
    sort of like take advantage of the fact
  • 3:39 - 3:45
    that we're here this class is going to I
  • 3:43 - 3:47
    don't wanna say ramped up quickly but
  • 3:45 - 3:49
    what's going to happen over the course
  • 3:47 - 3:51
    of this particular lecture is that we'll
  • 3:49 - 3:53
    cover many of the basics that we assume
  • 3:51 - 3:54
    that you will know for the rest of the
  • 3:53 - 3:56
    semester things like how to use your
  • 3:54 - 3:57
    shell and your terminal and I'll explain
  • 3:56 - 4:00
    what those are - those who you're not
  • 3:57 - 4:02
    familiar with them and then will pretty
  • 4:00 - 4:04
    quickly ramp up into more advanced tools
  • 4:02 - 4:05
    and how to use them you can already see
  • 4:04 - 4:07
    from the lecture notes the kind of
  • 4:05 - 4:10
    topics that we're going to be covering
  • 4:07 - 4:12
    and so that brings us to today's lecture
  • 4:10 - 4:15
    in which we are going to cover the shell
  • 4:12 - 4:17
    and the shell is going to be one of the
  • 4:15 - 4:19
    primary ways that you interact with your
  • 4:17 - 4:21
    computer once you want to do more things
  • 4:19 - 4:25
    than what the sort of visual interfaces
  • 4:21 - 4:26
    you might be used to allow you to do the
  • 4:25 - 4:27
    visual interfaces are sort of limited in
  • 4:26 - 4:29
    what
  • 4:27 - 4:30
    they allow you to do because you can
  • 4:29 - 4:32
    only do the things that there are
  • 4:30 - 4:35
    buttons for sliders for input fields for
  • 4:32 - 4:37
    often these textual tools are built to
  • 4:35 - 4:40
    be both composable with one another but
  • 4:37 - 4:41
    also to have tons of different ways to
  • 4:40 - 4:43
    combine them or ways to program and
  • 4:41 - 4:45
    automate them and that is why in this
  • 4:43 - 4:48
    class we will be focusing on these
  • 4:45 - 4:50
    command line or text-based tools and the
  • 4:48 - 4:52
    shell is the place that you would do
  • 4:50 - 4:54
    most of this work so for those of you
  • 4:52 - 4:57
    are not familiar with the shell most
  • 4:54 - 4:59
    platform provides some kind of shell on
  • 4:57 - 5:01
    Windows this is often PowerShell but
  • 4:59 - 5:04
    there are also other shells available on
  • 5:01 - 5:06
    Windows on Linux you will find tons of
  • 5:04 - 5:08
    terminals these are windows that allow
  • 5:06 - 5:10
    you to display shells and you'll also
  • 5:08 - 5:13
    find many different types of shells the
  • 5:10 - 5:16
    most common of which is bash or the born
  • 5:13 - 5:17
    again shell because it's such a common
  • 5:16 - 5:19
    shell it is the one we're primarily
  • 5:17 - 5:21
    going to be covering in these lectures
  • 5:19 - 5:24
    if you're on Mac OS you will probably
  • 5:21 - 5:26
    also have bash maybe an older version of
  • 5:24 - 5:27
    it if you open the terminal app and so
  • 5:26 - 5:29
    if you want to follow along on any of
  • 5:27 - 5:30
    these platforms feel free but keep in
  • 5:29 - 5:33
    mind that most of this is going to be
  • 5:30 - 5:34
    sort of Linux centric in terms of how we
  • 5:33 - 5:39
    teach it even though most of these tools
  • 5:34 - 5:40
    work on all the platforms if you want to
  • 5:39 - 5:43
    install a terminal and a shell and you
  • 5:40 - 5:45
    don't know how to do it well we're happy
  • 5:43 - 5:47
    to show you at office hours or it's
  • 5:45 - 5:52
    usually very easy to just Google like
  • 5:47 - 5:57
    your platform plus like terminal and you
  • 5:52 - 5:59
    will get one now when you open a
  • 5:57 - 6:01
    terminal you get something that looks a
  • 5:59 - 6:04
    little bit like this so it will usually
  • 6:01 - 6:06
    have just a single line at the top and
  • 6:04 - 6:08
    this is what's known as the shell prompt
  • 6:06 - 6:10
    you can see that my shell prompt looks
  • 6:08 - 6:12
    like this it has my user name the name
  • 6:10 - 6:14
    of the machine that I'm on the current
  • 6:12 - 6:17
    path I'm on and we will talk about paths
  • 6:14 - 6:19
    a little bit later and then it's really
  • 6:17 - 6:21
    just sort of blinking they're asking me
  • 6:19 - 6:22
    for input and this is the shell prompt
  • 6:21 - 6:26
    where you tell the shell what you want
  • 6:22 - 6:27
    it to do and you can customize this
  • 6:26 - 6:29
    prompt a lot and when you open it on
  • 6:27 - 6:31
    your machine it might not look exactly
  • 6:29 - 6:34
    like this it might look something like
  • 6:31 - 6:35
    this if you've configured it a little or
  • 6:34 - 6:38
    it might look all sorts of different
  • 6:35 - 6:40
    ways we won't go too much into
  • 6:38 - 6:40
    customizing your shell in this
  • 6:40 - 6:42
    particularly
  • 6:40 - 6:44
    we'll do that later here we're just
  • 6:42 - 6:48
    going to talk about how do you use this
  • 6:44 - 6:50
    shell to do useful things and this is
  • 6:48 - 6:52
    our the main textual interface you have
  • 6:50 - 6:53
    to your computer's through this shell on
  • 6:52 - 6:56
    the shell prompt you get to write
  • 6:53 - 6:59
    commands and commands can be relatively
  • 6:56 - 7:01
    straightforward things usually it'll be
  • 6:59 - 7:03
    something like executing programs with
  • 7:01 - 7:04
    arguments what does that look like well
  • 7:03 - 7:06
    one program we can execute is the date
  • 7:04 - 7:07
    program we just type date and press
  • 7:06 - 7:10
    enter and then it will show you
  • 7:07 - 7:12
    unsurprisingly the date and time you can
  • 7:10 - 7:15
    also execute a program with arguments
  • 7:12 - 7:18
    this is one way to modify the behavior
  • 7:15 - 7:21
    of the program so for example there is a
  • 7:18 - 7:22
    program called echo and echo just prints
  • 7:21 - 7:25
    out the arguments that you give it and
  • 7:22 - 7:27
    arguments are just white space separated
  • 7:25 - 7:30
    things that follow the program name so
  • 7:27 - 7:31
    we can say hello and then it will print
  • 7:30 - 7:33
    hello back
  • 7:31 - 7:36
    perhaps not terribly surprising but this
  • 7:33 - 7:37
    is the very basics of arguments one
  • 7:36 - 7:39
    thing that you'll notice is that I said
  • 7:37 - 7:40
    that arguments are separated by
  • 7:39 - 7:42
    whitespace and you might wonder well
  • 7:40 - 7:44
    what if I want an argument as multiple
  • 7:42 - 7:47
    words you can also quote things so you
  • 7:44 - 7:50
    can do things like echo hello space
  • 7:47 - 7:53
    world and now the echo program receives
  • 7:50 - 7:55
    one argument that contains the string
  • 7:53 - 7:58
    hello world with a space well you can
  • 7:55 - 7:59
    also use single quotes for this and the
  • 7:58 - 8:00
    difference between single quotes and
  • 7:59 - 8:03
    double quotes will get back to and when
  • 8:00 - 8:06
    we talk about bash scripting you can
  • 8:03 - 8:09
    also just escape single characters so
  • 8:06 - 8:12
    for example hello world this will also
  • 8:09 - 8:16
    work just fine all of these rules about
  • 8:12 - 8:17
    how you escape and how you parse and
  • 8:16 - 8:20
    quote various arguments and variables
  • 8:17 - 8:21
    we'll cover a little bit later hopefully
  • 8:20 - 8:24
    you won't run into too many we oughta
  • 8:21 - 8:26
    tease about this just keep in mind at
  • 8:24 - 8:28
    least that spaces separate arguments so
  • 8:26 - 8:30
    if you want to do something like make a
  • 8:28 - 8:32
    directory called my photos
  • 8:30 - 8:34
    you can't just type like make directory
  • 8:32 - 8:36
    my photos it will create two directories
  • 8:34 - 8:40
    one called my and one called photos and
  • 8:36 - 8:43
    that is probably not what you want now
  • 8:40 - 8:46
    one thing you might ask is how does the
  • 8:43 - 8:49
    shell know what these programs are when
  • 8:46 - 8:51
    I type date or when I type echo how does
  • 8:49 - 8:53
    it know what these programs are supposed
  • 8:51 - 8:54
    to do and the answer to this is your
  • 8:53 - 8:55
    program your
  • 8:54 - 8:57
    computer has a bunch of built-in
  • 8:55 - 9:00
    programs that comes with the machine
  • 8:57 - 9:03
    just like you your machine my chip with
  • 9:00 - 9:05
    like the terminal app or it might chip
  • 9:03 - 9:08
    with like Windows Explorer or at my chip
  • 9:05 - 9:10
    with some kind of browser it also ships
  • 9:08 - 9:12
    with a bunch of terminal centric
  • 9:10 - 9:15
    applications and these are stored on
  • 9:12 - 9:18
    your file system and your shell has a
  • 9:15 - 9:20
    way to determine where a program is
  • 9:18 - 9:22
    located basically has a way to search
  • 9:20 - 9:25
    for programs it does this through
  • 9:22 - 9:27
    something called an invariant
  • 9:25 - 9:30
    environment variable an environment
  • 9:27 - 9:32
    variable is a variable like you might be
  • 9:30 - 9:34
    used to for programming languages it
  • 9:32 - 9:35
    turns out that the shell and the
  • 9:34 - 9:37
    bourne-again shell in particular is
  • 9:35 - 9:39
    really a programming language this
  • 9:37 - 9:42
    prompt that you're given here is not
  • 9:39 - 9:43
    just able to run a program with
  • 9:42 - 9:46
    arguments. You can also do things like
  • 9:43 - 9:48
    while loops, for loops, conditionals...
  • 9:46 - 9:50
    All of these - you can define
  • 9:48 - 9:51
    functions, you can have variables, and all
  • 9:50 - 9:53
    of these things you can do in the shell.
  • 9:51 - 9:56
    We'll cover a lot of that in the next
  • 9:53 - 9:58
    lecture, on shell scripting. For now,
  • 9:56 - 10:00
    though, let's just look at this
  • 9:58 - 10:01
    particular environment variable.
  • 10:00 - 10:03
    Environment variables are things that
  • 10:01 - 10:05
    are set whenever you start your shell
  • 10:03 - 10:07
    they're not things you have to set every
  • 10:05 - 10:08
    time you run your shell there are a
  • 10:07 - 10:10
    bunch of these that are set things like
  • 10:08 - 10:12
    where is your home directory what is
  • 10:10 - 10:13
    your username and there's also one
  • 10:12 - 10:16
    that's critical for this particular
  • 10:13 - 10:19
    purpose which is the path variable so if
  • 10:16 - 10:23
    I echo out dollar path this is going to
  • 10:19 - 10:25
    show me all of the paths on my machine
  • 10:23 - 10:28
    that the shell will search for programs
  • 10:25 - 10:31
    you'll notice that this is a list that
  • 10:28 - 10:33
    is colon separated it might be kind of
  • 10:31 - 10:36
    long and hard to read but the essentials
  • 10:33 - 10:38
    is that - whenever you type the name of
  • 10:36 - 10:42
    a program is gonna search through this
  • 10:38 - 10:44
    list of paths on your machine and it's
  • 10:42 - 10:47
    gonna look in each directory for a
  • 10:44 - 10:49
    program or a file whose name matches the
  • 10:47 - 10:51
    command you try to run so in my case
  • 10:49 - 10:53
    when I try to run date or echo it's
  • 10:51 - 10:55
    gonna walk through these one at a time
  • 10:53 - 10:56
    until it finds one that contains the
  • 10:55 - 10:58
    program called date or echo and then
  • 10:56 - 11:00
    it's gonna run it if we want to know
  • 10:58 - 11:02
    which one it actually runs there's a
  • 11:00 - 11:05
    command called which which lets us do
  • 11:02 - 11:08
    that so I can type which echo and will
  • 11:05 - 11:11
    tell me that if I were to run a program
  • 11:08 - 11:13
    called echo I would run this one it's
  • 11:11 - 11:16
    worth pausing here to talk about what
  • 11:13 - 11:19
    paths are so paths are a way to name the
  • 11:16 - 11:22
    location of a file on your computer on
  • 11:19 - 11:25
    Linux and Oh Mac OS these paths are
  • 11:22 - 11:28
    separated by slashes forward slashes so
  • 11:25 - 11:31
    you'll see here that this is in the in
  • 11:28 - 11:33
    the root directory so the slash at the
  • 11:31 - 11:35
    very beginning indicates that this is
  • 11:33 - 11:38
    starting from the top of the file system
  • 11:35 - 11:41
    then look inside the directory called
  • 11:38 - 11:44
    USR then look inside the directory bin
  • 11:41 - 11:46
    and then look for the file called echo
  • 11:44 - 11:49
    on windows paths like this are usually
  • 11:46 - 11:52
    separated by back slashes instead and
  • 11:49 - 11:54
    we're on Linux and Mac OS everything
  • 11:52 - 11:56
    lives under the root name space so all
  • 11:54 - 12:00
    paths start with a slash or all absolute
  • 11:56 - 12:02
    paths on Windows there is one root for
  • 12:00 - 12:05
    every partition so you might have seen
  • 12:02 - 12:08
    things like C colon backslash or D colon
  • 12:05 - 12:11
    backslash so Windows has separate sort
  • 12:08 - 12:13
    of file system path hierarchies for each
  • 12:11 - 12:15
    each Drive that you have
  • 12:13 - 12:19
    whereas on Linux and Mac OS these are
  • 12:15 - 12:20
    all mounted under one namespace you'll
  • 12:19 - 12:22
    notice that I said the word absolute
  • 12:20 - 12:27
    path and you might not know what that
  • 12:22 - 12:29
    means so absolute paths are paths that
  • 12:27 - 12:32
    fully determine the location of a file
  • 12:29 - 12:34
    so in this case this is saying this is
  • 12:32 - 12:37
    talking only about a specific echo file
  • 12:34 - 12:39
    and it's giving you the the full path to
  • 12:37 - 12:43
    that file but there are also things
  • 12:39 - 12:45
    known as relative paths so a relative
  • 12:43 - 12:49
    path is relative to where you currently
  • 12:45 - 12:52
    are and so the way we find out where we
  • 12:49 - 12:56
    currently are is you can type PWD for
  • 12:52 - 13:00
    present working directory present print
  • 12:56 - 13:03
    working directory so if I type PWD it
  • 13:00 - 13:06
    will print out the current path that I'm
  • 13:03 - 13:08
    in right so currently I'm in the home
  • 13:06 - 13:11
    directory under the root and then John
  • 13:08 - 13:15
    under that and then dev under that etc
  • 13:11 - 13:17
    from here I can then choose to change my
  • 13:15 - 13:19
    current working directory and all
  • 13:17 - 13:20
    relative paths are relative to the
  • 13:19 - 13:21
    current working directory which is
  • 13:20 - 13:25
    basically where you
  • 13:21 - 13:29
    they are in this case for example I can
  • 13:25 - 13:30
    do CD / home CDs change directory this
  • 13:29 - 13:33
    is the way that I change what my current
  • 13:30 - 13:35
    working directory is in this case I
  • 13:33 - 13:38
    change to home and I am now you'll see
  • 13:35 - 13:41
    my my shell prompt change to say that I
  • 13:38 - 13:43
    am now in home it just gives me the name
  • 13:41 - 13:44
    of the last segment of the path but you
  • 13:43 - 13:46
    can also configure your terminal to give
  • 13:44 - 13:50
    you the full path whenever you're
  • 13:46 - 13:53
    anywhere and now if I type PWD again it
  • 13:50 - 13:55
    will tell me I'm in slash home there are
  • 13:53 - 13:59
    also a couple of special directories
  • 13:55 - 14:03
    that exist there is dot and dot dot dot
  • 13:59 - 14:05
    means the current directory dot dot
  • 14:03 - 14:07
    means the parent directory so this is a
  • 14:05 - 14:10
    way that you can easily navigate around
  • 14:07 - 14:13
    the system for example here if I type CD
  • 14:10 - 14:15
    dot dot it will tell me that I am now in
  • 14:13 - 14:18
    slash so I'm now in the root of the file
  • 14:15 - 14:22
    system I was in slash home now I'm in
  • 14:18 - 14:25
    slash and indeed if I type PWD well it
  • 14:22 - 14:27
    will do that right thing and I can also
  • 14:25 - 14:29
    then use relative paths to go down into
  • 14:27 - 14:32
    the file system right so I can do CD dot
  • 14:29 - 14:34
    slash home and this is gonna CD into the
  • 14:32 - 14:37
    home directory under the current
  • 14:34 - 14:40
    directory right so this will bring me
  • 14:37 - 14:42
    back to slash home if I now tried CD dot
  • 14:40 - 14:44
    slash home again it will say there's no
  • 14:42 - 14:46
    such directory because there is no home
  • 14:44 - 14:49
    directory under the current directory
  • 14:46 - 14:54
    I'm on which I changed by doing CD right
  • 14:49 - 14:58
    and I can sort of see de all the way
  • 14:54 - 15:01
    back to the place that I was using
  • 14:58 - 15:07
    relative paths and I can also do things
  • 15:01 - 15:09
    like dot dot dot dot dot to get back to
  • 15:07 - 15:11
    somewhere deep in my file system this
  • 15:09 - 15:13
    happens to be all the way back to the
  • 15:11 - 15:16
    root so here there's a bin directory and
  • 15:13 - 15:18
    another bin there's an echo file and so
  • 15:16 - 15:20
    then I could do world and that runs the
  • 15:18 - 15:22
    echo program under bin alright so this
  • 15:20 - 15:24
    is a way that you can construct paths to
  • 15:22 - 15:27
    arbitrarily traverse your filesystem
  • 15:24 - 15:28
    sometimes you want to absolute paths and
  • 15:27 - 15:30
    sometimes you want relative ones usually
  • 15:28 - 15:33
    you want to use whichever one is shorter
  • 15:30 - 15:34
    but if you want to for example run a
  • 15:31 - 15:34
    program or write
  • 15:34 - 15:39
    a program that runs the program like
  • 15:37 - 15:42
    echo or date and you want it to be able
  • 15:39 - 15:44
    to run be run from anywhere you either
  • 15:42 - 15:46
    want to just give the name of the
  • 15:44 - 15:48
    program like date or echo and let the the
  • 15:46 - 15:50
    shell use the path to figure out where
  • 15:48 - 15:52
    it is or you want to give its absolute
  • 15:50 - 15:54
    path because if you gave a relative path
  • 15:52 - 15:56
    then if I ran it in my home directory
  • 15:54 - 16:02
    and you ran it in some other directory
  • 15:56 - 16:05
    it might work for me but not for you in
  • 16:02 - 16:06
    general when we run a program it is
  • 16:05 - 16:08
    going to be operating on the current
  • 16:06 - 16:10
    working directory at least by default
  • 16:08 - 16:12
    unless we give it any other arguments
  • 16:10 - 16:14
    and this is really handy because it
  • 16:12 - 16:16
    means that often we don't have to give
  • 16:14 - 16:18
    full paths for things we can just use
  • 16:16 - 16:20
    the name of files and in the directory
  • 16:18 - 16:23
    that we're currently in one thing that's
  • 16:20 - 16:24
    really useful is to figure out what is
  • 16:23 - 16:26
    in the current directory we're in so we
  • 16:24 - 16:28
    already saw PWD which prints where you
  • 16:26 - 16:31
    currently are there's a command called
  • 16:28 - 16:33
    LS which will show you it will list the
  • 16:31 - 16:36
    files in the current directory so if i
  • 16:33 - 16:38
    type LS here this is all the files in
  • 16:36 - 16:40
    the current directory right and this is
  • 16:38 - 16:43
    a handy way to just quickly navigate
  • 16:40 - 16:47
    through the filesystem you'll see that
  • 16:43 - 16:48
    if I sort of CD dot and then do LS it'll
  • 16:47 - 16:53
    show me the files in that directory
  • 16:48 - 16:56
    instead but with LS I can also give it
  • 16:53 - 16:58
    LS dot dot like I can give it a path and
  • 16:56 - 17:00
    then will LS that file instead of the
  • 16:58 - 17:04
    one that I'm currently in or LS that
  • 17:00 - 17:06
    directory and you can see this if I go
  • 17:04 - 17:09
    all the way to the root as well right
  • 17:06 - 17:12
    root has different files one handy trick
  • 17:09 - 17:14
    you might not know about here is there
  • 17:12 - 17:17
    are two other special things you can do
  • 17:14 - 17:18
    one is the tilde character this
  • 17:17 - 17:21
    character brings you to your home
  • 17:18 - 17:23
    directory so tilde always expands to the
  • 17:21 - 17:26
    home directory and you can do relative
  • 17:23 - 17:30
    paths to it so I can do tilde slash dev
  • 17:26 - 17:33
    slash P DOS classes missing semester and
  • 17:30 - 17:37
    now I'm there because tilde expanded to
  • 17:33 - 17:39
    slash home slash John there is also for
  • 17:37 - 17:42
    CD in particular a really handy argument
  • 17:39 - 17:45
    you can give which is - if you do CD -
  • 17:42 - 17:47
    it will CD to the directory you were
  • 17:45 - 17:48
    previously in so if I do CD - I go back
  • 17:47 - 17:52
    to root
  • 17:48 - 17:54
    if I do CD - again I go back to missing
  • 17:52 - 17:55
    semester so this is a handy way if you
  • 17:54 - 18:01
    want to toggle between two different
  • 17:55 - 18:04
    directories in the case of LS or in the
  • 18:01 - 18:06
    case of CD there might be arguments do
  • 18:04 - 18:08
    you don't know about right currently we
  • 18:06 - 18:10
    haven't really been doing anything
  • 18:08 - 18:12
    except giving paths but how do you even
  • 18:10 - 18:14
    discover that you can give a path to LS
  • 18:12 - 18:17
    in the first place well most programs
  • 18:14 - 18:19
    take what are known as arguments like
  • 18:17 - 18:22
    flags and options these are things that
  • 18:19 - 18:25
    usually start with a - one of the Hemi
  • 18:22 - 18:27
    is one of these is - help most programs
  • 18:25 - 18:30
    implement this and if you run for
  • 18:27 - 18:34
    example LS - help it all helpfully print
  • 18:30 - 18:36
    out a bunch of information about that
  • 18:34 - 18:38
    command and you'll see here that it says
  • 18:36 - 18:40
    the usage is LS and you can give some
  • 18:38 - 18:42
    number of options and you can give some
  • 18:40 - 18:46
    number of files the way to read that
  • 18:42 - 18:49
    usage line is triple dot means one like
  • 18:46 - 18:51
    zero or one or more and the square
  • 18:49 - 18:53
    bracket means optional so in this case
  • 18:51 - 18:54
    there's an optional number of options
  • 18:53 - 18:57
    and there's an optional number of files
  • 18:54 - 18:59
    and you'll see that it says what the
  • 18:57 - 19:01
    program does and also specifies a number
  • 18:59 - 19:03
    of different types of flags and options
  • 19:01 - 19:05
    you can give usually we call things that
  • 19:03 - 19:08
    are a single dash in a single letter a
  • 19:05 - 19:10
    flag and anything that or anything that
  • 19:08 - 19:12
    doesn't take a value a flag and anything
  • 19:10 - 19:17
    that does take a value an option so for
  • 19:12 - 19:22
    example - a and - all are both flags and
  • 19:17 - 19:24
    - C or - color R is an option one thing
  • 19:22 - 19:30
    you'll see under here if you scroll down
  • 19:24 - 19:37
    far enough is the - L flag and that's
  • 19:30 - 19:39
    unhelpful the - L flag the - L flag uses
  • 19:37 - 19:41
    a long listing format now that's
  • 19:39 - 19:43
    particularly helpful in and of itself
  • 19:41 - 19:47
    but let's see what it actually does so
  • 19:43 - 19:48
    if I do LS dash L it still prints the
  • 19:47 - 19:50
    files in the current directory but it
  • 19:48 - 19:52
    gives me a lot more information about
  • 19:50 - 19:55
    those files and this is something you'll
  • 19:52 - 19:56
    find yourself using quite a lot because
  • 19:55 - 20:01
    the additional information it gives you
  • 19:56 - 20:02
    is often quite handy let's look at what
  • 20:01 - 20:09
    some of that information
  • 20:02 - 20:10
    ah so first of all the D at the
  • 20:09 - 20:12
    beginning of some of these entries
  • 20:10 - 20:15
    indicate that something is a directory
  • 20:12 - 20:16
    so the underscore data entry here for
  • 20:15 - 20:19
    example is a directory
  • 20:16 - 20:23
    whereas for for hTML is not a directory
  • 20:19 - 20:25
    it's a file the following letters after
  • 20:23 - 20:29
    that indicate the permissions that are
  • 20:25 - 20:31
    set for that file so this like we saw
  • 20:29 - 20:33
    earlier I might not be able to open a
  • 20:31 - 20:34
    given file or I might not be able to CD
  • 20:33 - 20:36
    into a given directory and this is all
  • 20:34 - 20:38
    dictated by the permissions on that
  • 20:36 - 20:41
    particular file or directory the way to
  • 20:38 - 20:44
    read these is that the the first group
  • 20:41 - 20:46
    of three are the permissions are set for
  • 20:44 - 20:50
    the owner of the file all of these files
  • 20:46 - 20:52
    you'll see are owned by me the second
  • 20:50 - 20:54
    group of three characters is for the
  • 20:52 - 20:56
    permissions for the group that owns this
  • 20:54 - 20:59
    file in this case all of these files are
  • 20:56 - 21:02
    also known by the john group and a final
  • 20:59 - 21:04
    group of three is a list of the
  • 21:02 - 21:06
    permissions for everyone else so anyone
  • 21:04 - 21:09
    who's not a user owner or a group owner
  • 21:06 - 21:11
    this directory is perhaps kind of boring
  • 21:09 - 21:14
    because all of the things are owned by
  • 21:11 - 21:17
    me but if we do something like CD to
  • 21:14 - 21:20
    slash and do LS dash L you'll see that
  • 21:17 - 21:23
    here all of them are owned by root we'll
  • 21:20 - 21:24
    get back to what the root user is but
  • 21:23 - 21:26
    here you see some of the permissions are
  • 21:24 - 21:31
    a little bit more interesting the groups
  • 21:26 - 21:34
    of three are read write and execute what
  • 21:31 - 21:36
    these mean differs for files and for
  • 21:34 - 21:38
    directories for files it's pretty
  • 21:36 - 21:40
    straightforward if you have read
  • 21:38 - 21:42
    permissions on a file then you can read
  • 21:40 - 21:44
    its contents if you have write
  • 21:42 - 21:46
    permissions on a file then you can save
  • 21:44 - 21:48
    the file you can add more to it or you
  • 21:46 - 21:51
    can replace it entirely and if you have
  • 21:48 - 21:53
    execute to the X bit on a file then
  • 21:51 - 21:57
    you're allowed to execute that file so
  • 21:53 - 22:00
    if we do LS al in slash bin that's a
  • 21:57 - 22:03
    novel and user bin you'll see that all
  • 22:00 - 22:05
    of them have the execute bit set even
  • 22:03 - 22:06
    for people who are not the owner of the
  • 22:05 - 22:08
    file and this is because the echo
  • 22:06 - 22:09
    program for example we want everyone on
  • 22:08 - 22:12
    the computer to be able to execute
  • 22:09 - 22:14
    there's no reason to say only certain
  • 22:12 - 22:16
    users can run echo that doesn't really
  • 22:14 - 22:18
    make any make any sense
  • 22:16 - 22:21
    for directories though these permissions
  • 22:18 - 22:24
    are a little bit different so read
  • 22:21 - 22:26
    translates - are you allowed to see
  • 22:24 - 22:29
    which files are inside this directory so
  • 22:26 - 22:31
    think of read as lists for a directory
  • 22:29 - 22:33
    are you allowed to list its contents
  • 22:31 - 22:36
    write for a directory is whether you are
  • 22:33 - 22:39
    allowed to rename create or remove files
  • 22:36 - 22:42
    within that directory so it's still kind
  • 22:39 - 22:45
    of right but notice that this means that
  • 22:42 - 22:48
    if you have write permissions on a file
  • 22:45 - 22:50
    but you do not have write permissions on
  • 22:48 - 22:52
    its directory you cannot delete the file
  • 22:50 - 22:54
    you can empty it but you cannot delete
  • 22:52 - 22:58
    it because that would require writing to
  • 22:54 - 22:59
    the directory itself and finally execute
  • 22:58 - 23:02
    on directories is something that trips
  • 22:59 - 23:05
    people up a lot execute on a directory
  • 23:02 - 23:07
    is what's known as search and that's not
  • 23:05 - 23:10
    terribly helpful a name but what that
  • 23:07 - 23:13
    means is are you allowed to enter this
  • 23:10 - 23:15
    directory if you want to get to a file
  • 23:13 - 23:17
    if you want to open it or read it or
  • 23:15 - 23:20
    write it whatever you want to do
  • 23:17 - 23:23
    basically to CD into a directory you
  • 23:20 - 23:25
    must have the execute permission on all
  • 23:23 - 23:28
    parent directories of that directory and
  • 23:25 - 23:30
    the directory itself so for example for
  • 23:28 - 23:34
    me to access a file inside slash user
  • 23:30 - 23:36
    slash bin such as user bin echo I must
  • 23:34 - 23:38
    have executed on route I must have
  • 23:36 - 23:40
    execute on user and I must have execute
  • 23:38 - 23:42
    on bin if I do not have all those
  • 23:40 - 23:45
    execute bits I will not be allowed to
  • 23:42 - 23:48
    access that file because I won't be able
  • 23:45 - 23:51
    to enter the directories along the way
  • 23:48 - 23:53
    there are a number of other bits that
  • 23:51 - 23:55
    you might come across like you might see
  • 23:53 - 23:58
    esses or T's in these lists you might
  • 23:55 - 23:59
    see LS those we can talk about in office
  • 23:58 - 24:01
    hours if you're curious
  • 23:59 - 24:04
    they will mostly not matter for anything
  • 24:01 - 24:06
    you will do in this class but they are
  • 24:04 - 24:08
    handy to know about so if you're curious
  • 24:06 - 24:11
    about them look them up on your own or
  • 24:08 - 24:13
    come ask us in office hours there are
  • 24:11 - 24:15
    some other programs that are handy to
  • 24:13 - 24:16
    know about oh sorry there's one more
  • 24:15 - 24:17
    thing as I mentioned if you just have a
  • 24:16 - 24:19
    dash it means you do not have that
  • 24:17 - 24:22
    permission right so if it says for
  • 24:19 - 24:23
    example our dash X it means that you
  • 24:22 - 24:27
    have read and execute but you do not
  • 24:23 - 24:29
    have right there are some other handy
  • 24:27 - 24:32
    programs to know about at this point
  • 24:29 - 24:35
    one of them is move or the MV command so
  • 24:32 - 24:39
    if I CD back to missing semester here MV
  • 24:35 - 24:43
    lets me rename a file and rename here
  • 24:39 - 24:45
    takes two paths it takes the old path in
  • 24:43 - 24:48
    the new path this is means that move
  • 24:45 - 24:50
    lets you both rename a file like if you
  • 24:48 - 24:52
    change the name of the file but not the
  • 24:50 - 24:54
    directory or it lets you move a file to
  • 24:52 - 24:56
    a completely different directory it just
  • 24:54 - 24:58
    you give the path to the current file
  • 24:56 - 25:00
    and the path to where you want that file
  • 24:58 - 25:03
    to be and that can change its location
  • 25:00 - 25:05
    and its name so for example I can move
  • 25:03 - 25:09
    dot files dot MD to be food MD
  • 25:05 - 25:16
    unhelpfully right and similarly I can
  • 25:09 - 25:19
    move it back there's also the CP command
  • 25:16 - 25:22
    the CP or copy is very similar it lets
  • 25:19 - 25:24
    you copy a file CP also takes two
  • 25:22 - 25:26
    arguments it takes the path you want to
  • 25:24 - 25:28
    copy from and the path you want to copy
  • 25:26 - 25:31
    to and these are full paths so I could
  • 25:28 - 25:33
    use this for example to say I want to
  • 25:31 - 25:38
    copy dot files out MD - dot dot slash
  • 25:33 - 25:40
    food MD sure food MD and now if I do LS
  • 25:38 - 25:43
    dot you'll see that there's a food MD
  • 25:40 - 25:45
    file in that directory so CP as well
  • 25:43 - 25:48
    take two paths it does not have to be in
  • 25:45 - 25:50
    the same directory and similarly there's
  • 25:48 - 25:52
    the RM command which lets you remove a
  • 25:50 - 25:54
    file and there - you can give paths in
  • 25:52 - 25:57
    this case I'm removing dot dot slash
  • 25:54 - 25:59
    food you should be aware for removing
  • 25:57 - 26:02
    especially on Linux removal is by
  • 25:59 - 26:05
    default not recursive so you cannot
  • 26:02 - 26:07
    remove a directory using RM you can pass
  • 26:05 - 26:09
    the - our flag which lets you do a
  • 26:07 - 26:11
    recursive remove and then give a path
  • 26:09 - 26:13
    that you want to remove and it will
  • 26:11 - 26:16
    remove everything below it there is also
  • 26:13 - 26:18
    the RM dr dir command which lets you
  • 26:16 - 26:21
    remove a directory but it only lets you
  • 26:18 - 26:23
    remove that directory if it is empty so
  • 26:21 - 26:24
    the idea here is to sort of be a safety
  • 26:23 - 26:25
    mechanism for you so you don't
  • 26:24 - 26:29
    accidentally throw away a bunch of your
  • 26:25 - 26:31
    files and the final little command
  • 26:29 - 26:33
    that's handy to use is make there which
  • 26:31 - 26:34
    lets you create a new directory and as
  • 26:33 - 26:36
    we talked about before you don't want to
  • 26:34 - 26:38
    do something like this because it will
  • 26:36 - 26:41
    create two directories for you one
  • 26:38 - 26:42
    called my and one called photos if you
  • 26:41 - 26:43
    actually want to create a directory like
  • 26:42 - 26:50
    this you would either
  • 26:43 - 26:53
    escape the space or quote the string if
  • 26:50 - 26:55
    you ever want more information about how
  • 26:53 - 26:57
    any command to basically on these
  • 26:55 - 26:58
    platforms work there's a really handy
  • 26:57 - 27:01
    command for that as well
  • 26:58 - 27:05
    there is the program called man for
  • 27:01 - 27:06
    manual pages this program takes as an
  • 27:05 - 27:09
    argument the name of another program and
  • 27:06 - 27:12
    gives you its manual page so for example
  • 27:09 - 27:15
    we could do man LS and this shows us a
  • 27:12 - 27:17
    manual page for LS you'll notice that in
  • 27:15 - 27:19
    the case of LS it is fairly similar to
  • 27:17 - 27:21
    what we got with LS - help but it's a
  • 27:19 - 27:24
    little easier to navigate a little
  • 27:21 - 27:26
    easier to read usually towards the
  • 27:24 - 27:29
    bottom you will also get examples
  • 27:26 - 27:30
    information about who wrote it where you
  • 27:29 - 27:32
    can find more information and that sort
  • 27:30 - 27:33
    of stuff one thing that can be confusing
  • 27:32 - 27:35
    sometimes
  • 27:33 - 27:36
    at least until a recent version where
  • 27:35 - 27:39
    they added this three at the bottom
  • 27:36 - 27:41
    which says Q to quit they do not use to
  • 27:39 - 27:43
    say this you press Q to quit this
  • 27:41 - 27:48
    program it can be really hard to quit it
  • 27:43 - 27:51
    if you do not know that a handy keyboard
  • 27:48 - 27:53
    shortcut here by the way is ctrl L which
  • 27:51 - 27:57
    lets you clear your terminal and go back
  • 27:53 - 27:59
    to the top so so far we've only talked
  • 27:57 - 28:01
    about programs in isolation but where
  • 27:59 - 28:03
    much of the power of the shell really
  • 28:01 - 28:06
    comes through is once you start
  • 28:03 - 28:08
    combining different programs right so
  • 28:06 - 28:11
    rather than just like running CDE
  • 28:08 - 28:12
    running LS and etc you might want to
  • 28:11 - 28:14
    chain multiple programs together you
  • 28:12 - 28:16
    might want to interact with files and
  • 28:14 - 28:19
    have files operate in between programs
  • 28:16 - 28:21
    and the way we can do this is using this
  • 28:19 - 28:25
    notion of streams that the shell gives
  • 28:21 - 28:26
    us every program by default has I'm
  • 28:25 - 28:29
    gonna simplify a little and say two
  • 28:26 - 28:32
    primary streams it has an input stream
  • 28:29 - 28:34
    and an output stream by default the
  • 28:32 - 28:35
    input stream is your keyboard basically
  • 28:34 - 28:37
    the input stream is your terminal and
  • 28:35 - 28:39
    whatever you type into your terminal is
  • 28:37 - 28:41
    going to end up into the program and it
  • 28:39 - 28:42
    has a default output stream which is
  • 28:41 - 28:44
    whenever the program prints something
  • 28:42 - 28:46
    it's gonna print to that stream and by
  • 28:44 - 28:48
    default that is also your terminal this
  • 28:46 - 28:51
    is why when I type echo hello it gets
  • 28:48 - 28:54
    printed back to my terminal but the
  • 28:51 - 28:56
    shell gives you a way to rewire these
  • 28:54 - 28:59
    streams to change where the input
  • 28:56 - 29:01
    output of a programmer pointed the way
  • 28:59 - 29:06
    the most straightforward way you do this
  • 29:01 - 29:08
    is using the angle bracket signs so you
  • 29:06 - 29:12
    can write something like this or you can
  • 29:08 - 29:14
    write something like this the left angle
  • 29:12 - 29:16
    bracket indicates rewire the input for
  • 29:14 - 29:21
    this program to be the contents of this
  • 29:16 - 29:24
    file and the end angle bracket means
  • 29:21 - 29:28
    rewire the output of the preceding
  • 29:24 - 29:29
    program into this file so let's look at
  • 29:28 - 29:32
    an example of what that would look like
  • 29:29 - 29:34
    if I do echo hello I can say I want that
  • 29:32 - 29:37
    context the content to be stored in a
  • 29:34 - 29:39
    file called hello dot text and because I
  • 29:37 - 29:41
    gave this is a relative path right this
  • 29:39 - 29:43
    will construct a file in the current
  • 29:41 - 29:45
    directory called hello text and at least
  • 29:43 - 29:48
    in theory its contents should be the
  • 29:45 - 29:50
    word hello so if I run this notice that
  • 29:48 - 29:52
    nothing got printed to my output the
  • 29:50 - 29:53
    previous time when I ran echo hello it
  • 29:52 - 29:55
    printed hello
  • 29:53 - 29:58
    now that hello is going gone into a file
  • 29:55 - 30:01
    called hello text and I can verify this
  • 29:58 - 30:04
    by using the program called cat so cat
  • 30:01 - 30:07
    prints the contents of a file so I can
  • 30:04 - 30:11
    do cat hello dot txt and there it shows
  • 30:07 - 30:13
    me hello but cat is a is also something
  • 30:11 - 30:16
    that supports this kind of wiring so I
  • 30:13 - 30:18
    can say cat which by default just prints
  • 30:16 - 30:21
    its input it just duplicates its input
  • 30:18 - 30:24
    to its output I can say I want you to
  • 30:21 - 30:25
    take your input from Hello text what
  • 30:24 - 30:27
    will happen in this case is that the
  • 30:25 - 30:30
    shell is going to open hello dot txt
  • 30:27 - 30:33
    take its contents and set that to be the
  • 30:30 - 30:34
    input of cat and then cat is going to
  • 30:33 - 30:36
    just print that to its output which
  • 30:34 - 30:38
    since I haven't rewired it is gonna be
  • 30:36 - 30:42
    my terminal so this will just print
  • 30:38 - 30:44
    hello to the output and I can use both
  • 30:42 - 30:45
    of these at the same time so for example
  • 30:44 - 30:47
    if I want to copy a file and I don't
  • 30:45 - 30:52
    want to use the CP command for some
  • 30:47 - 30:54
    reason I can do this and in this case
  • 30:52 - 30:57
    I'm telling the cat program nothing at
  • 30:54 - 30:58
    all I'm just saying do your normal thing
  • 30:57 - 31:00
    right the cat program does not know
  • 30:58 - 31:02
    anything about this redirection but I'm
  • 31:00 - 31:04
    telling the shell to use hello dot txt as
  • 31:02 - 31:08
    the input for cat and to write anything
  • 31:04 - 31:09
    that cat prints - hello - dot txt again
  • 31:08 - 31:10
    this prints nothing to my terminal
  • 31:09 - 31:13
    but if
  • 31:10 - 31:16
    cat hello to text I get the output as I
  • 31:13 - 31:21
    would have expected which is a copy of
  • 31:16 - 31:23
    the original file there is also a double
  • 31:21 - 31:27
    end bracket which is append instead of
  • 31:23 - 31:27
    just overwrite so you'll notice that I
  • 31:27 - 31:30
    if I do
  • 31:27 - 31:32
    cat hello dot txt - hello - dot txt
  • 31:30 - 31:34
    again and then I cat hello2 dot txt it
  • 31:32 - 31:36
    still just contains hello even though it
  • 31:34 - 31:39
    already contained hello if I switch that
  • 31:36 - 31:41
    to instead be a double end bracket it
  • 31:39 - 31:46
    means append and if I now cat that file
  • 31:41 - 31:48
    it has a hello twice these are pretty
  • 31:46 - 31:50
    straightforward they're usually just
  • 31:48 - 31:52
    ways to interact with files but where it
  • 31:50 - 31:53
    gets really interesting is an additional
  • 31:52 - 31:56
    operator the shell gives you called the
  • 31:53 - 32:00
    pipe character so pipe is just a
  • 31:56 - 32:03
    vertical bar and what pipe means is take
  • 32:00 - 32:05
    the output of the program to the left
  • 32:03 - 32:10
    and make it the input of the program to
  • 32:05 - 32:14
    the right right so what does this look
  • 32:10 - 32:17
    like well let's take the example of ls /
  • 32:14 - 32:20
    or ls dash l / this prints a bunch of
  • 32:17 - 32:22
    things let's say that I only wanted the
  • 32:20 - 32:26
    last line of this output well there's a
  • 32:22 - 32:29
    command called tail and tail prints the
  • 32:26 - 32:32
    last n lines of its input and I can do
  • 32:29 - 32:35
    -n1 so this is a flag called n you
  • 32:32 - 32:37
    can also use dash dash lines if you want
  • 32:35 - 32:39
    to use it as a longer option but in this
  • 32:37 - 32:42
    case this is saying just print the last
  • 32:39 - 32:47
    line and I can wire these together so I
  • 32:42 - 32:51
    can say ls dash l / pipe tail -n1
  • 32:47 - 32:53
    and notice here that ls does not know
  • 32:51 - 32:55
    about tail and tail does not know about
  • 32:53 - 32:57
    ls they are different programs and have
  • 32:55 - 32:59
    never been programmed to be compatible
  • 32:57 - 33:01
    with one another all they know how to do
  • 32:59 - 33:03
    is read from input and write to output
  • 33:01 - 33:05
    and then the pipe is what wires them
  • 33:03 - 33:08
    together and in this particular case I'm
  • 33:05 - 33:09
    saying I want the output of LS to be the
  • 33:08 - 33:11
    input to tail and then I want the output
  • 33:09 - 33:12
    of tail to just go to my terminal
  • 33:11 - 33:14
    because I haven't rewired it
  • 33:12 - 33:17
    I could also rewire this to say I want
  • 33:14 - 33:19
    the output to go to ls dot text and in this
  • 33:17 - 33:21
    case if I cat ls dot text I would get
  • 33:19 - 33:23
    the appropriate output and it turns out
  • 33:21 - 33:22
    you can do some really neat things with this
  • 33:23 - 33:28
    gonna cover this a lot more in the data
  • 33:25 - 33:31
    wrangling lecture there will be in like
  • 33:28 - 33:32
    four days or something on the kind of
  • 33:31 - 33:36
    fancy stuff you can do when you start
  • 33:32 - 33:38
    building more advanced pipelines one to
  • 33:36 - 33:42
    give you one example we can do something
  • 33:38 - 33:46
    like curl - -head --silent google.com
  • 33:42 - 33:49
    so just to show you what that looks
  • 33:46 - 33:53
    like this gives me all the HTTP headers
  • 33:49 - 33:57
    for accessing google.com and I can pipe
  • 33:53 - 34:01
    that to grep a - like a --ignore-case
  • 33:57 - 34:04
    or just -i if I want content
  • 34:01 - 34:06
    length so this is gonna print the
  • 34:04 - 34:08
    content length header, grep is a program
  • 34:06 - 34:09
    that we'll talk about later they'll let
  • 34:08 - 34:13
    you search in an input stream for a
  • 34:09 - 34:16
    given keyword we can pipe that through
  • 34:13 - 34:20
    say the cut command which takes a
  • 34:16 - 34:22
    delimiter set that to be space and I
  • 34:20 - 34:24
    want the second field and this prints
  • 34:22 - 34:26
    just the content length this is sort of
  • 34:24 - 34:28
    a silly example right like this just
  • 34:26 - 34:30
    lets you extract the content length in
  • 34:28 - 34:33
    bytes of google.com from the command
  • 34:30 - 34:35
    line it's not a very useful thing to do
  • 34:33 - 34:36
    but you can see how by chaining these
  • 34:35 - 34:38
    together you can achieve a bunch of
  • 34:36 - 34:41
    really interesting text manipulation
  • 34:38 - 34:43
    effects and it turns out pipes are not
  • 34:41 - 34:45
    just for textual data you can do this
  • 34:43 - 34:48
    for things like images as well you can
  • 34:45 - 34:50
    have a program that manipulates a binary
  • 34:48 - 34:51
    image on its input and writes a binary
  • 34:50 - 34:53
    image to its output and you can chain
  • 34:51 - 34:55
    them together in this way and we'll talk
  • 34:53 - 34:57
    about some of those kinds of examples
  • 34:55 - 34:59
    later on - you can even do this for
  • 34:57 - 35:01
    video if you want you can stream this is
  • 34:59 - 35:03
    for example a great way if you have a
  • 35:01 - 35:06
    chromecast at home you can stream a
  • 35:03 - 35:08
    video file like this by having the last
  • 35:06 - 35:11
    program in your pipe be a chromecast
  • 35:08 - 35:13
    send program so you stream a video file
  • 35:11 - 35:19
    into it and it streams or HTTP to your
  • 35:13 - 35:20
    chromecast we'll take we'll talk a lot
  • 35:19 - 35:22
    more about this in the data wrangling
  • 35:20 - 35:25
    lecture but there's one more thing that
  • 35:22 - 35:28
    I wanted to talk to you about about sort
  • 35:25 - 35:30
    of how to use the terminal and a more
  • 35:28 - 35:32
    interesting and perhaps more powerful
  • 35:30 - 35:34
    way that you might be used to and this
  • 35:32 - 35:36
    is perhaps even going to be interesting
  • 35:34 - 35:38
    for the ones of you who feel like you're
  • 35:36 - 35:41
    already comfortable with the term
  • 35:38 - 35:43
    but first we need to cover a important
  • 35:41 - 35:45
    topic when it comes to Linux systems and
  • 35:43 - 35:48
    Mac OS systems in particular which is
  • 35:45 - 35:49
    the notion of the root user the root
  • 35:48 - 35:52
    user is sort of like the administrator
  • 35:49 - 35:55
    user on Windows and it has user IDs zero
  • 35:52 - 35:57
    the root user is special because it is
  • 35:55 - 36:00
    allowed to do whatever it wants on your
  • 35:57 - 36:02
    system even if a file is like not
  • 36:00 - 36:04
    readable by anyone or if it's not
  • 36:02 - 36:06
    writable by anyone root can still access
  • 36:04 - 36:09
    that file root is sort of a super user
  • 36:06 - 36:11
    that gets to do whatever they want and
  • 36:09 - 36:13
    most of the time you will not be
  • 36:11 - 36:16
    operating as the super user you will not
  • 36:13 - 36:17
    be root you will be a user like John or
  • 36:16 - 36:20
    whatever your name is and that's going
  • 36:17 - 36:21
    to be the user you act with because if
  • 36:20 - 36:23
    you were operating your computer as the
  • 36:21 - 36:25
    root user at all times if you ran the
  • 36:23 - 36:27
    wrong program they could just completely
  • 36:25 - 36:30
    destroy your computer and you don't want
  • 36:27 - 36:32
    that right but every now and again you
  • 36:30 - 36:34
    want to do something that requires that
  • 36:32 - 36:39
    you are root usually for these cases you
  • 36:34 - 36:43
    will use a program called sudo su do or
  • 36:39 - 36:45
    do as su and su in this case is Super
  • 36:43 - 36:47
    User so this is a way to do the
  • 36:45 - 36:50
    following thing as the super user
  • 36:47 - 36:51
    usually the way sudo works is you write
  • 36:50 - 36:53
    sudo and then a command like you would
  • 36:51 - 36:55
    normally on your terminal and it will
  • 36:53 - 36:56
    just run that command as if you were
  • 36:55 - 37:01
    root as opposed to the user you actually
  • 36:56 - 37:05
    are where might you need something like
  • 37:01 - 37:06
    this well there is a special there are
  • 37:05 - 37:08
    many special file system on your
  • 37:06 - 37:11
    computer but in particular there's one
  • 37:08 - 37:15
    called sysfs if you CD to slash sys
  • 37:11 - 37:17
    this is a whole new world this file
  • 37:15 - 37:20
    system is are not actually files on your
  • 37:17 - 37:22
    computer instead these are various
  • 37:20 - 37:24
    kernel parameters so the kernel is like
  • 37:22 - 37:27
    basically the the core of your computer
  • 37:24 - 37:30
    this is a way for you to access various
  • 37:27 - 37:33
    kernel parameters through what looks
  • 37:30 - 37:36
    like a file system you'll see here that
  • 37:33 - 37:38
    if I CD into class for example it has
  • 37:36 - 37:40
    directories for a bunch of different
  • 37:38 - 37:44
    types of devices that I can interact
  • 37:40 - 37:48
    with or various queues I can access or all
  • 37:44 - 37:50
    sorts of weird knobs internally and
  • 37:48 - 37:50
    because they're exposed as files it means
  • 37:50 - 37:54
    we can also use all the tools have been
  • 37:52 - 37:58
    using so far in order to manipulate them
  • 37:54 - 38:01
    one example of this is if you go into
  • 37:58 - 38:02
    sys class backlight so this backlight
  • 38:01 - 38:04
    directly and lets you configure the
  • 38:02 - 38:07
    backlight on your laptop if you have one
  • 38:04 - 38:09
    so I can CD in to intel backlight this
  • 38:07 - 38:11
    is an Intel laptop inside here you'll
  • 38:09 - 38:13
    see there's a file called brightness and
  • 38:11 - 38:16
    I can cat the brightness this is the
  • 38:13 - 38:19
    current brightness of my screen but not
  • 38:16 - 38:21
    only that I can modify this too in order
  • 38:19 - 38:24
    to change the brightness of my screen so
  • 38:21 - 38:26
    you might think that I could do let's
  • 38:24 - 38:27
    see what the max brightness is here okay
  • 38:26 - 38:30
    so it's currently set to the max
  • 38:27 - 38:32
    brightness you might imagine that I
  • 38:30 - 38:35
    could do something like if I do echo
  • 38:32 - 38:39
    let's do half or something
  • 38:35 - 38:38
    echo 500 to brightness
  • 38:38 - 38:39
    if I do this it says
  • 38:39 - 38:43
    permission denied I'm not allowed to
  • 38:41 - 38:45
    modify brightness because in order to
  • 38:43 - 38:47
    basically in order to change things in
  • 38:45 - 38:48
    the kernel you need to be the
  • 38:47 - 38:51
    administrator and you might imagine that
  • 38:48 - 38:53
    the way to solve this is to write sudo
  • 38:51 - 38:56
    echo 500 but I still get a permission
  • 38:53 - 38:58
    denied error but why is that it's
  • 38:56 - 39:01
    because as I mentioned before these
  • 38:58 - 39:03
    redirections of input and output is not
  • 39:01 - 39:05
    something the programs know about when
  • 39:03 - 39:07
    we piped Elison to tail tail did not
  • 39:05 - 39:10
    know about LS and LS did not know about
  • 39:07 - 39:12
    tail the pipe and the redirection was
  • 39:10 - 39:14
    set up by the shell so in this case
  • 39:12 - 39:18
    what's happening is I'm telling my shell
  • 39:14 - 39:21
    run the program sudo with the arguments
  • 39:18 - 39:24
    echo and 500 and send its output to the
  • 39:21 - 39:26
    file called brightness but the shell is
  • 39:24 - 39:29
    what is opening the brightness file it
  • 39:26 - 39:32
    is not the sudo program so in this case
  • 39:29 - 39:34
    the shell which is running as me tries
  • 39:32 - 39:35
    to open the brightness file for writing
  • 39:34 - 39:37
    and it's not allowed to do that and
  • 39:35 - 39:40
    therefore I get a permission down error
  • 39:37 - 39:42
    you might have seen this if you like
  • 39:40 - 39:44
    search for something end up on Stack
  • 39:42 - 39:45
    Overflow and it tells just run this
  • 39:44 - 39:47
    command and you'll see that it does
  • 39:45 - 39:53
    something like they give you
  • 39:47 - 39:58
    instructions like one two sis what's an
  • 39:53 - 40:00
    example net ipv4 for word for example
  • 39:58 - 40:03
    this is something you may have seen if
  • 40:00 - 40:04
    you're setting up a firewall and this
  • 40:03 - 40:06
    command is intended to work
  • 40:04 - 40:08
    because this little pound symbol
  • 40:06 - 40:10
    indicates run this as root this is
  • 40:08 - 40:11
    something that is very rarely explained
  • 40:10 - 40:13
    but that is what the pound symbol means
  • 40:11 - 40:15
    you'll see on my prompt there's a dollar
  • 40:13 - 40:18
    symbol instead and the dollar indicates
  • 40:15 - 40:19
    you are not running as root so the
  • 40:18 - 40:22
    question is how do I get around this
  • 40:19 - 40:25
    well I could switch into a root terminal
  • 40:22 - 40:27
    so one way to do this is to run sudo su
  • 40:25 - 40:32
    sudo su is saying run the following
  • 40:27 - 40:34
    command s root and su is a complicated
  • 40:32 - 40:37
    command that effectively gets you a
  • 40:34 - 40:41
    shell as the super user so if I do this
  • 40:37 - 40:42
    type of password then now you'll see
  • 40:41 - 40:45
    that the username at the beginning
  • 40:42 - 40:47
    changed from jon to root and the prompt
  • 40:45 - 40:49
    changed from a dollar to a pound if I
  • 40:47 - 40:52
    now I come in to that file if I do echo
  • 40:49 - 40:54
    500 to brightness my screen got a little
  • 40:52 - 40:57
    dimmer but you can't see it you just
  • 40:54 - 40:59
    have to trust me and now I didn't get an
  • 40:57 - 41:02
    error and this is because the shell is
  • 40:59 - 41:05
    now running as root it is not running as
  • 41:02 - 41:07
    Jon and the root user is allowed to open
  • 41:05 - 41:10
    this file but given our knowledge that
  • 41:07 - 41:11
    we have of the terminal now there's
  • 41:10 - 41:14
    actually a way for us to do this without
  • 41:11 - 41:19
    having to drop to a root shell and that
  • 41:14 - 41:23
    is as follows that's I guess restore it
  • 41:19 - 41:27
    to 1060 so do you see why this is
  • 41:23 - 41:29
    different here I'm telling my shell to
  • 41:27 - 41:31
    run the echo 1060 command which is gonna
  • 41:29 - 41:33
    echo 1060 and I'm telling it to run the
  • 41:31 - 41:36
    sudo tee brightness command and I'm
  • 41:33 - 41:39
    telling you to send the output of echo
  • 41:36 - 41:41
    into sudo tee in order to understand this
  • 41:39 - 41:43
    you need to know what the tee command does
  • 41:41 - 41:47
    the tee command takes its input and writes
  • 41:43 - 41:50
    it to a file but also to standard out so
  • 41:47 - 41:52
    tee is a convenient way if you have say a
  • 41:50 - 41:54
    log file that you want to like send to a
  • 41:52 - 41:56
    file to store for later but you also
  • 41:54 - 41:58
    want to see it to yourself then you can
  • 41:56 - 42:00
    pipe it through tee give it the name of a
  • 41:58 - 42:02
    file and it will write whatever its
  • 42:00 - 42:04
    input is both to that file and to your
  • 42:02 - 42:08
    screen and here I'm taking advantage of
  • 42:04 - 42:10
    that program I'm saying run tee as route
  • 42:08 - 42:13
    and have tee right into the brightness
  • 42:10 - 42:14
    file and so in this case the tee program
  • 42:13 - 42:16
    which is what is opening the brightness
  • 42:14 - 42:17
    file is running as root and so it is
  • 42:16 - 42:20
    allowed to do
  • 42:17 - 42:21
    if I run this it will now again you
  • 42:20 - 42:24
    can't see but the brightness and I've
  • 42:21 - 42:26
    been turned on by a laptop and I don't
  • 42:24 - 42:27
    get any errors and I never had to drop
  • 42:26 - 42:31
    into a root shell and run commands there
  • 42:27 - 42:32
    which can often be somewhat dangerous if
  • 42:31 - 42:33
    you want to explore this filesystem a
  • 42:32 - 42:36
    little bit more there's a lot of
  • 42:33 - 42:39
    interesting stuff in here if you just
  • 42:36 - 42:41
    sort of start browsing around you can
  • 42:39 - 42:43
    find all sorts of fun things so for
  • 42:41 - 42:45
    example we noticed that there was a fun
  • 42:43 - 42:49
    brightness command here I wonder what
  • 42:45 - 42:50
    other kinds of brightness I can set so I
  • 42:49 - 42:52
    can use the find command which we will
  • 42:50 - 42:56
    also talk about in a coming lecture I
  • 42:52 - 42:59
    wouldn't look on any file whose name
  • 42:56 - 43:02
    it's a little like brightness in the
  • 42:59 - 43:05
    current directory that's unhelpful maybe
  • 43:02 - 43:09
    they're not files did I misspell
  • 43:05 - 43:17
    brightness yeah why is it being annoying
  • 43:09 - 43:20
    Oh apparently it does not want to search
  • 43:17 - 43:24
    for brightness for me how well luckily
  • 43:20 - 43:27
    for you I know of one already handy that
  • 43:24 - 43:30
    there is a subdirectory called LEDs and
  • 43:27 - 43:32
    LEDs have brightness too what kind of
  • 43:30 - 43:37
    LEDs are there ooh lots of things for
  • 43:32 - 43:38
    example the scroll lock led now most of
  • 43:37 - 43:40
    you probably don't know what the scroll
  • 43:38 - 43:43
    lock LED is or much less what scroll
  • 43:40 - 43:46
    lock is you might have seen a key on
  • 43:43 - 43:48
    your keyboard neighbor named scroll lock
  • 43:46 - 43:50
    basically no one knows what it means
  • 43:48 - 43:52
    anymore no one really uses it for
  • 43:50 - 43:56
    anything it's mostly just a dead key and
  • 43:52 - 43:57
    also a dead LED what if you wanted to
  • 43:56 - 43:59
    configure it so that every time you get
  • 43:57 - 44:01
    email your scroll lock LED lights up
  • 43:59 - 44:03
    because there's no other reason why it
  • 44:01 - 44:06
    would light up well if we seed you into
  • 44:03 - 44:08
    this particular directory that has a
  • 44:06 - 44:12
    brightness place and it's set to zero
  • 44:08 - 44:12
    well what happens if I write one into it
  • 44:13 - 44:17
    you probably should not just be writing
  • 44:15 - 44:19
    random numbers into random files in this
  • 44:17 - 44:20
    directory because you are affecting your
  • 44:19 - 44:23
    kernel directly like look up what the
  • 44:20 - 44:24
    files do in this particular case I have
  • 44:23 - 44:28
    warned safety goggles and I've done my
  • 44:24 - 44:31
    research so now you can't tell but on my
  • 44:28 - 44:34
    keyboard the scroll lock LED is now lit
  • 44:31 - 44:35
    so now if I wrote a program that like
  • 44:34 - 44:38
    did some checking of mail and stuff I
  • 44:35 - 44:40
    could have it at the end run a program
  • 44:38 - 44:43
    that echoes one into this file and now I
  • 44:40 - 44:47
    have a way for my led to my keyboard to
  • 44:43 - 44:50
    indicate when I've new email at this
  • 44:47 - 44:51
    point you should know roughly your way
  • 44:50 - 44:54
    around
  • 44:51 - 44:56
    the terminal around the shell and and
  • 44:54 - 44:59
    know enough to accomplish these basic
  • 44:56 - 45:01
    tasks at least in theory now you
  • 44:59 - 45:03
    shouldn't need to use like
  • 45:01 - 45:03
    point-and-click interfaces to find files
  • 45:03 - 45:05
    anymore
  • 45:03 - 45:07
    there's one remaining trick you might
  • 45:05 - 45:11
    need and that is the ability to open a
  • 45:07 - 45:14
    file so far I've only really given you
  • 45:11 - 45:18
    ways to find files but one thing you
  • 45:14 - 45:21
    should know about is missing semester
  • 45:18 - 45:24
    xdg-open this will probably only work
  • 45:21 - 45:27
    on Linux on Mac OS I think it's just
  • 45:24 - 45:29
    called open on Windows who knows
  • 45:27 - 45:32
    xdg-open you give the name of a file and
  • 45:29 - 45:35
    it will open it in the appropriate program
  • 45:32 - 45:36
    so if you open if you do xdg-open an
  • 45:35 - 45:39
    HTML file that will open your browser
  • 45:36 - 45:41
    and open that file and once you have
  • 45:39 - 45:43
    that program in theory you should no
  • 45:41 - 45:45
    longer need to open like a Finder window
  • 45:43 - 45:47
    ever again you might want to for other
  • 45:45 - 45:48
    reasons but in theory you can accomplish
  • 45:47 - 45:51
    at all using the tools that we've
  • 45:48 - 45:54
    learned today this might all seem
  • 45:51 - 45:56
    relatively basic for some of you but as
  • 45:54 - 45:57
    I mentioned this is sort of the ramp-up
  • 45:56 - 45:59
    period of now we all know how the shell
  • 45:57 - 46:02
    works and a lot of what we'll be doing
  • 45:59 - 46:03
    in future lectures is using this
  • 46:02 - 46:07
    knowledge to do really interesting
  • 46:03 - 46:09
    things using the shell that sort of this
  • 46:07 - 46:11
    is learning the the interface that we're
  • 46:09 - 46:15
    going to be using and so it's important
  • 46:11 - 46:17
    we all know it we're gonna talk a lot
  • 46:15 - 46:18
    more in the next lecture about how to
  • 46:17 - 46:20
    automate tasks like this how to write
  • 46:18 - 46:22
    scripts that run a bunch of programs for
  • 46:20 - 46:24
    you and have to do things like
  • 46:22 - 46:27
    conditionals and loops and stuff in your
  • 46:24 - 46:29
    terminal and do things like run a
  • 46:27 - 46:30
    program until it fails which can be
  • 46:29 - 46:32
    handy in classes where you want to run
  • 46:30 - 46:35
    something until your test suite fails
  • 46:32 - 46:39
    for example so that's the topic for next
  • 46:35 - 46:40
    week's lecture did you have a question
  • 46:39 - 46:42
    it's what you've been demoing this
  • 46:40 - 46:50
    assist directory that presumably will
  • 46:42 - 46:53
    only work if you're running that is a
  • 46:50 - 46:55
    good question I don't know whether the
  • 46:53 - 46:58
    windows subsystem for Linux will expose
  • 46:55 - 46:59
    the sis file system if it does it
  • 46:58 - 47:03
    probably only exposes a very small
  • 46:59 - 47:08
    number of things it might because there
  • 47:03 - 47:10
    are I don't know check it out
  • 47:08 - 47:12
    one thing you'll see is the lecture
  • 47:10 - 47:14
    notes for this lecture are already
  • 47:12 - 47:17
    online and at the very bottom of the
  • 47:14 - 47:18
    file there are a bunch of exercises some
  • 47:17 - 47:20
    of them are relatively easy some of them
  • 47:18 - 47:23
    are a little bit harder and what we
  • 47:20 - 47:24
    encourage you to do is to take a stab at
  • 47:23 - 47:25
    going through them if you know this
  • 47:24 - 47:28
    stuff already it should go really
  • 47:25 - 47:29
    quickly if you don't it might teach you
  • 47:28 - 47:32
    a bunch of things that you might not
  • 47:29 - 47:33
    realize you didn't know and for the
  • 47:32 - 47:35
    office hours that we're gonna do right
  • 47:33 - 47:37
    after this lecture we will happily help
  • 47:35 - 47:38
    you get through all of those or if there
  • 47:37 - 47:40
    are other commands and you learn in the
  • 47:38 - 47:42
    process you want to know how to use more
  • 47:40 - 47:45
    efficiently and then in next lecture
  • 47:42 - 47:47
    which is tomorrow will basically be
  • 47:45 - 47:48
    assuming that you know the kind of stuff
  • 47:47 - 47:52
    that the exercise is going to teach you
  • 47:48 - 47:53
    there's also a an email address on the
  • 47:52 - 47:56
    website where you can send us questions
  • 47:53 - 47:59
    if you think of something like after the
  • 47:56 - 48:05
    office hours are finished are there any
  • 47:59 - 48:07
    questions before we end today no no all
  • 48:05 - 48:09
    right well we will have office hours on
  • 48:07 - 48:12
    the ninth floor of the gates building of
  • 48:09 - 48:16
    building 32 in like five minutes
  • 48:12 - 48:16
    sweet see you there
Title:
Lecture 1: Course Overview + The Shell (2020)
Description:

more » « less
Video Language:
English
Duration:
48:17

English subtitles

Revisions