1 00:00:00,000 --> 00:00:05,060 >> [MUSIC PLAYING] 2 00:00:05,060 --> 00:00:05,890 >> DOUG LLOYD: OK. 3 00:00:05,890 --> 00:00:08,680 So let's talk about how to use the Linux command line. 4 00:00:08,680 --> 00:00:11,860 Now, the CS50 IDE, or in fact, even a CS50 appliance, 5 00:00:11,860 --> 00:00:15,210 if you're familiar with that, or you're taking an older version of CS50, 6 00:00:15,210 --> 00:00:17,420 is a cloud-based machine which runs Ubuntu, 7 00:00:17,420 --> 00:00:20,259 which is one of the many flavors of the Linux operating system. 8 00:00:20,259 --> 00:00:22,300 Linux operating system is favored by programmers, 9 00:00:22,300 --> 00:00:25,690 because it's just cooler, right? 10 00:00:25,690 --> 00:00:29,170 >> Many modern Linux distributions have graphical user interfaces, 11 00:00:29,170 --> 00:00:33,710 which we also call GUIs, G-U-I, to allow easy mouse-based navigation, 12 00:00:33,710 --> 00:00:36,920 which you're probably familiar with, if you're a Windows or Mac user, 13 00:00:36,920 --> 00:00:39,961 moving around your mouse, double-clicking on icons, and so on. 14 00:00:39,961 --> 00:00:42,210 Still though, as a programmer, and even though the IDE 15 00:00:42,210 --> 00:00:44,800 contains the ability to do some graphical user 16 00:00:44,800 --> 00:00:47,400 stuff, clicking, and dragging, and all that, 17 00:00:47,400 --> 00:00:50,197 you'll still be using your terminal window pretty frequently. 18 00:00:50,197 --> 00:00:52,280 And you can do many of the same tasks that you can 19 00:00:52,280 --> 00:00:53,905 do with a mouse with keyboard commands. 20 00:00:53,905 --> 00:00:56,821 And we're going to talk a little bit about what some of those commands 21 00:00:56,821 --> 00:00:58,050 are right now. 22 00:00:58,050 --> 00:01:02,490 >> Now, these commands can be used on any Unix-based operating 23 00:01:02,490 --> 00:01:06,790 system, which includes Linux, but also includes Mac OS. 24 00:01:06,790 --> 00:01:12,930 If you open up Terminal on your Mac, you can use these exact commands. 25 00:01:12,930 --> 00:01:15,705 Windows also has Command Prompt, but some of the commands 26 00:01:15,705 --> 00:01:17,871 are slightly different, so it doesn't actually work, 27 00:01:17,871 --> 00:01:20,080 because Windows is not a Unix-based system. 28 00:01:20,080 --> 00:01:22,620 >> So let's take a look at some of these Linux commands. 29 00:01:22,620 --> 00:01:26,100 The first one that you'll probably use quite a lot is ls. 30 00:01:26,100 --> 00:01:30,410 That's a lowercase l, followed by a lowercase s, which is short for list. 31 00:01:30,410 --> 00:01:33,720 And what the list command does is it gives you a readout of all the files 32 00:01:33,720 --> 00:01:35,670 and folders in your current directory. 33 00:01:35,670 --> 00:01:39,430 So you can see everything you can get to from where you currently are. 34 00:01:39,430 --> 00:01:41,545 >> So I've opened up here the CS50 IDE. 35 00:01:41,545 --> 00:01:44,540 And I'm going to zoom-in in a second to give you a closer look, 36 00:01:44,540 --> 00:01:47,240 but here's the broad picture of what the IDE looks like. 37 00:01:47,240 --> 00:01:49,580 On the left, you can see we have a file tree, which you're probably 38 00:01:49,580 --> 00:01:52,663 familiar with, double-clicking, and files and folders, and all that stuff. 39 00:01:52,663 --> 00:01:54,694 So that's still there in the CS50 appliance. 40 00:01:54,694 --> 00:01:57,860 At the center in the top is where you're going to be writing your code, once 41 00:01:57,860 --> 00:01:58,960 you click on a file. 42 00:01:58,960 --> 00:02:00,900 And at the bottom, we have a terminal window, 43 00:02:00,900 --> 00:02:04,040 which is where we can execute these terminal commands. 44 00:02:04,040 --> 00:02:07,460 >> I'm going to zoom-in and head over here, just 45 00:02:07,460 --> 00:02:12,360 to show you that, in fact, I can click on these files and folders. 46 00:02:12,360 --> 00:02:17,010 So clearly, where I currently am, I have two folders, called pset0 and pset1, 47 00:02:17,010 --> 00:02:20,520 and three files, one called hello, one called hello.c, 48 00:02:20,520 --> 00:02:22,870 and one called hello.txt. 49 00:02:22,870 --> 00:02:27,100 >> So let's move down to the terminal window and get a closer look. 50 00:02:27,100 --> 00:02:31,010 So we just talked, again, about the fact that we have three files and two 51 00:02:31,010 --> 00:02:32,970 folders in the current directory. 52 00:02:32,970 --> 00:02:37,080 If I type ls, which again is the command to list 53 00:02:37,080 --> 00:02:40,870 the contents of the current directory, and then I hit Enter, 54 00:02:40,870 --> 00:02:47,220 look what I see, hello, hello.c, hello.txt, pset0 and pset1. 55 00:02:47,220 --> 00:02:49,620 >> pset0 and pset1 are colored blue, to indicate 56 00:02:49,620 --> 00:02:52,420 to you that those are directories that we could navigate into. 57 00:02:52,420 --> 00:02:54,503 And we'll learn a little bit about how to navigate 58 00:02:54,503 --> 00:02:56,290 into directories in a minute. 59 00:02:56,290 --> 00:03:00,920 And every other thing is colored black, if it's a text file or a source code 60 00:03:00,920 --> 00:03:03,590 file, and green, if it's an executable file. 61 00:03:03,590 --> 00:03:06,367 So clearly, that means that I could run a program called, hello. 62 00:03:06,367 --> 00:03:07,950 That's what the green one there means. 63 00:03:07,950 --> 00:03:10,080 But basically, typing the ls command has allowed 64 00:03:10,080 --> 00:03:13,470 me to look at everything that exists in my current directory, which 65 00:03:13,470 --> 00:03:17,940 matches what we see here, in the graphical display of the same. 66 00:03:17,940 --> 00:03:19,940 The next command you'll probably use quite a bit 67 00:03:19,940 --> 00:03:24,792 is cd, lowercase c, lowercase d, which is short for change directory. 68 00:03:24,792 --> 00:03:27,500 This allows us to do what I was talking about a second ago, which 69 00:03:27,500 --> 00:03:31,150 is to navigate between directories at the command 70 00:03:31,150 --> 00:03:33,700 line, as opposed to double-clicking on folders. 71 00:03:33,700 --> 00:03:37,250 So if we type cd and then the name of a directory, 72 00:03:37,250 --> 00:03:40,890 we can get into that directory. 73 00:03:40,890 --> 00:03:44,680 >> As an aside, know that the name of the current directory is always 74 00:03:44,680 --> 00:03:48,670 dot, and the name of the directory one level above where we are now, 75 00:03:48,670 --> 00:03:54,820 that is the name of the folder in which our folder is, dot, dot-- 76 00:03:54,820 --> 00:03:56,851 or in which our folder is, is dot, dot. 77 00:03:56,851 --> 00:03:59,350 And if you're ever curious about the name of your directory, 78 00:03:59,350 --> 00:04:03,430 you can type pwd, which stands for present working directory. 79 00:04:03,430 --> 00:04:06,827 We'll take a look at all of these now, by heading back to the CS50 IDE. 80 00:04:06,827 --> 00:04:08,160 So I'm back in my workspace now. 81 00:04:08,160 --> 00:04:11,930 And I'll zoom-in again on the terminal, so we can take a look 82 00:04:11,930 --> 00:04:14,209 at moving around within the IDE. 83 00:04:14,209 --> 00:04:16,250 So I'm going to list the contents of my directory 84 00:04:16,250 --> 00:04:19,220 again, just to reground us in where we are. 85 00:04:19,220 --> 00:04:21,740 So if I type ls, which is for list again, 86 00:04:21,740 --> 00:04:24,160 I see that I can get to pset0 and pset1. 87 00:04:24,160 --> 00:04:26,420 Those are the directories I can get to from here. 88 00:04:26,420 --> 00:04:30,400 I know that, because the IDE gives me a clue by coloring them blue. 89 00:04:30,400 --> 00:04:33,295 >> Let's say that I want to get into my pset1 directory, 90 00:04:33,295 --> 00:04:35,310 because I'm working on problem set 1. 91 00:04:35,310 --> 00:04:41,380 I can type cd-- again, short for change directory-- space, pset1. 92 00:04:41,380 --> 00:04:43,334 And if I hit Enter, notice what happens. 93 00:04:43,334 --> 00:04:45,000 It doesn't look like a lot has happened. 94 00:04:45,000 --> 00:04:50,510 But if you look at the prompt, it now tells me that I'm in ~/workspace/pset1. 95 00:04:50,510 --> 00:04:55,510 I've navigated into the pset1 folder that was within my workspace. 96 00:04:55,510 --> 00:05:00,560 >> And if I type ls, I see some different stuff here, right? 97 00:05:00,560 --> 00:05:03,810 This isn't the same list that I saw before. 98 00:05:03,810 --> 00:05:05,350 I've navigated into pset1. 99 00:05:05,350 --> 00:05:08,560 And so now, when I type ls, I'm getting the context of what can I 100 00:05:08,560 --> 00:05:12,600 see from within the pset1 folder. 101 00:05:12,600 --> 00:05:16,056 >> Now, I'm going to type control l, which just clears the screen. 102 00:05:16,056 --> 00:05:18,180 And I'm going to list the contents of the directory 103 00:05:18,180 --> 00:05:18,880 again, just so you can see. 104 00:05:18,880 --> 00:05:20,730 I just wanted to do that to clear out some of the stuff 105 00:05:20,730 --> 00:05:22,521 that you saw down below and to prevent this 106 00:05:22,521 --> 00:05:24,660 from going too far down out of range. 107 00:05:24,660 --> 00:05:28,440 >> Now, I said earlier that, if I want to navigate to the current directory, 108 00:05:28,440 --> 00:05:31,580 I can type cd space dot. 109 00:05:31,580 --> 00:05:32,080 Hit Enter. 110 00:05:32,080 --> 00:05:33,330 It doesn't do anything, right? 111 00:05:33,330 --> 00:05:35,480 I'm changing directories to the current directory. 112 00:05:35,480 --> 00:05:37,813 You're not always going to find a need for a single dot, 113 00:05:37,813 --> 00:05:39,540 but you will occasionally. 114 00:05:39,540 --> 00:05:41,950 >> Let's say that I want to move up one level. 115 00:05:41,950 --> 00:05:44,450 I want to get back to my workspace directory. 116 00:05:44,450 --> 00:05:49,800 I can't type cd workspace, there's no such file or directory. 117 00:05:49,800 --> 00:05:52,780 And the reason for that-- if I type ls one more time-- 118 00:05:52,780 --> 00:05:58,060 is that there is no directory called workspace inside of my pset1 directory. 119 00:05:58,060 --> 00:06:00,275 I'm going to clear my screen again with control l. 120 00:06:00,275 --> 00:06:02,150 Remember what I said earlier, though, that we 121 00:06:02,150 --> 00:06:04,010 can navigate back with dot, dot. 122 00:06:04,010 --> 00:06:05,880 That's the name of the parent directory. 123 00:06:05,880 --> 00:06:10,727 So if I type cd, space, dot, dot, and then hit Enter, 124 00:06:10,727 --> 00:06:11,810 now look at what happened. 125 00:06:11,810 --> 00:06:15,250 My command prompt tells me that I'm back in my ~/workspace directory. 126 00:06:15,250 --> 00:06:19,360 I moved up one level, thanks to dot, dot. 127 00:06:19,360 --> 00:06:23,400 >> Now, let's say that I'm using an operating system that is Linux-based, 128 00:06:23,400 --> 00:06:25,440 but doesn't necessarily tell me where I am. 129 00:06:25,440 --> 00:06:28,370 This one happens to tell us that I'm in ~/workspace right now, 130 00:06:28,370 --> 00:06:29,280 right at the prompt. 131 00:06:29,280 --> 00:06:31,940 But I could be completely lost in a mess of folders, 132 00:06:31,940 --> 00:06:34,946 and I have no idea where I am and no idea where I want to get back to. 133 00:06:34,946 --> 00:06:36,320 There's two things that I can do. 134 00:06:36,320 --> 00:06:40,920 >> First of all, I can figure out where I am, by typing pwd. 135 00:06:40,920 --> 00:06:42,720 That's my present working directory. 136 00:06:42,720 --> 00:06:45,830 And if I hit Enter, it tells me exactly where I am. 137 00:06:45,830 --> 00:06:49,400 Now /home/ubuntu is the long way of saying, tilde, 138 00:06:49,400 --> 00:06:51,290 which is your home directory. 139 00:06:51,290 --> 00:06:56,540 But it tells me that I'm in home/ubuntu/workspace, or ~/workspace. 140 00:06:56,540 --> 00:07:00,700 >> I'm going to navigate to my pset1 directory again, 141 00:07:00,700 --> 00:07:02,229 and I'm going to list the contents. 142 00:07:02,229 --> 00:07:04,770 And I see that I have another directory there, called extras. 143 00:07:04,770 --> 00:07:09,020 So I'm going to cd into extras, and then I'm going to clear my screen. 144 00:07:09,020 --> 00:07:11,540 >> So now, I'm pretty far in, right? 145 00:07:11,540 --> 00:07:13,680 What if I want to back to workspace immediately? 146 00:07:13,680 --> 00:07:15,138 There's a couple things I could do. 147 00:07:15,138 --> 00:07:18,750 I could type cd, dot, dot, slash, dot, dot, to move up one level and then 148 00:07:18,750 --> 00:07:19,640 another level. 149 00:07:19,640 --> 00:07:21,090 But that's kind of annoying. 150 00:07:21,090 --> 00:07:24,990 >> So if I ever want to get back to just my home directory, tilde, 151 00:07:24,990 --> 00:07:27,690 I can type cd with nothing after it. 152 00:07:27,690 --> 00:07:29,224 Cd, Enter. 153 00:07:29,224 --> 00:07:30,140 And now, I'm in tilde. 154 00:07:30,140 --> 00:07:34,310 And if I want to get to workspace, I can just type cd workspace. 155 00:07:34,310 --> 00:07:36,770 And that's how you work changing directories 156 00:07:36,770 --> 00:07:42,080 within the CS50 IDE or any Linux operating system at the command line. 157 00:07:42,080 --> 00:07:42,580 All right. 158 00:07:42,580 --> 00:07:45,610 The next one that might come in handy is mkdir, 159 00:07:45,610 --> 00:07:48,709 which is short for make a directory, if I need to create a new folder. 160 00:07:48,709 --> 00:07:50,750 If you're familiar with the GUI operating system, 161 00:07:50,750 --> 00:07:51,820 you might right-click. 162 00:07:51,820 --> 00:07:54,500 And then, when the context menu pops up, choose New Folder. 163 00:07:54,500 --> 00:07:56,250 That's probably how you've done it before. 164 00:07:56,250 --> 00:08:00,979 But we can also create directories at the command line. 165 00:08:00,979 --> 00:08:02,020 So we're back in the IDE. 166 00:08:02,020 --> 00:08:08,020 I'll zoom-in on the terminal and list the contents of my directory 167 00:08:08,020 --> 00:08:11,882 again, just to give us a frame of reference. 168 00:08:11,882 --> 00:08:14,340 Let's say that, now, I've finished working on problem set 0 169 00:08:14,340 --> 00:08:15,580 and problem set 1. 170 00:08:15,580 --> 00:08:19,390 So I want to create a new directory to work on for problem set 2. 171 00:08:19,390 --> 00:08:20,420 How do I do that? 172 00:08:20,420 --> 00:08:24,132 >> Well, again, I could right-click in the left side there and choose New Folder 173 00:08:24,132 --> 00:08:25,340 and create a pset2 directory. 174 00:08:25,340 --> 00:08:26,240 That would work too. 175 00:08:26,240 --> 00:08:28,615 But then I also do it at the command line pretty quickly, 176 00:08:28,615 --> 00:08:35,049 by typing mkdir, space-- I typed in n, but-- space pset2. 177 00:08:35,049 --> 00:08:38,659 If I hit Enter and then I list the contents of my directory again, 178 00:08:38,659 --> 00:08:40,710 I see that, look, now I have a pset2 folder. 179 00:08:40,710 --> 00:08:43,110 And I can navigate into that using cd and do 180 00:08:43,110 --> 00:08:45,614 all the work I need to do for pset2. 181 00:08:45,614 --> 00:08:48,530 Incidentally, I'll just pop over here really quickly to the file tree. 182 00:08:48,530 --> 00:08:53,494 And you can see that, also, in the graphical File Explorer, 183 00:08:53,494 --> 00:08:56,160 we can see that the pset2 directory has also been created there. 184 00:08:56,160 --> 00:08:58,537 And I can navigate to it, using the GUI as well. 185 00:08:58,537 --> 00:09:00,870 The next time and that's probably going to come in handy 186 00:09:00,870 --> 00:09:03,650 is cp, which is short for copy. 187 00:09:03,650 --> 00:09:06,960 Copy, unlike all the other commands we've seen before, 188 00:09:06,960 --> 00:09:11,800 takes two arguments, a source, the name of the file that you want to copy, 189 00:09:11,800 --> 00:09:15,810 and a destination, where you want to copy the file to. 190 00:09:15,810 --> 00:09:19,490 It's pretty easy to copy a file, so let's do that. 191 00:09:19,490 --> 00:09:20,640 >> So we're back in the IDE. 192 00:09:20,640 --> 00:09:24,540 I'm going to list the contents of my current directory with ls. 193 00:09:24,540 --> 00:09:28,310 Now, let's say that I want to make a copy of hello.txt. 194 00:09:28,310 --> 00:09:32,120 Again, from the file tree on the left, the graphical interface, 195 00:09:32,120 --> 00:09:35,969 I could right-click on hello.txt, make a copy, paste the copy. 196 00:09:35,969 --> 00:09:38,260 But I can do it pretty quickly at the command line too. 197 00:09:38,260 --> 00:09:42,520 >> Let's say I want to copy hello.txt to hi.txt. 198 00:09:42,520 --> 00:09:45,850 I can cp, space, hello.txt. 199 00:09:45,850 --> 00:09:48,850 That's my source file, so that's why I'm going to choose that one first. 200 00:09:48,850 --> 00:09:51,058 And then I need to name the destination file, hi.txt. 201 00:09:51,058 --> 00:09:53,690 202 00:09:53,690 --> 00:09:54,620 I hit Enter. 203 00:09:54,620 --> 00:09:57,930 And if I list the contents of my directory again, there's hi.txt. 204 00:09:57,930 --> 00:09:59,220 I made a copy of it. 205 00:09:59,220 --> 00:10:01,090 And in fact, if I went into hi.txt, I could 206 00:10:01,090 --> 00:10:04,030 see that it would be an exact duplicate of everything that 207 00:10:04,030 --> 00:10:08,330 existed in my hello.txt file. 208 00:10:08,330 --> 00:10:09,790 >> So that's how you copy a file. 209 00:10:09,790 --> 00:10:12,680 But what if you want to copy an entire directory? 210 00:10:12,680 --> 00:10:16,510 So for a second, let's take a look at what's in my pset0 directory. 211 00:10:16,510 --> 00:10:23,950 If I cd pset0 and list the contents, I have a directory called, sample, 212 00:10:23,950 --> 00:10:26,252 and a scratch file, scratch.sb2. 213 00:10:26,252 --> 00:10:27,210 So that's good to know. 214 00:10:27,210 --> 00:10:29,800 So let's clear the screen, and I'm going to go back 215 00:10:29,800 --> 00:10:32,870 to my workspace directory for a second. 216 00:10:32,870 --> 00:10:36,630 >> Let's say that, now, I want to make a copy of my pset0 directory. 217 00:10:36,630 --> 00:10:42,940 I can't just say cp pset0 pset3, for instance. 218 00:10:42,940 --> 00:10:46,220 You get this weird message, omitting directory pset0. 219 00:10:46,220 --> 00:10:47,439 Why do you get that message? 220 00:10:47,439 --> 00:10:49,730 Well, it turns out that, when you have a directory that 221 00:10:49,730 --> 00:10:53,150 has other stuff inside of it, the cp command doesn't really 222 00:10:53,150 --> 00:10:55,590 necessarily know what to do with it. 223 00:10:55,590 --> 00:10:59,670 >> We need to explicitly tell Linux, the terminal, 224 00:10:59,670 --> 00:11:03,720 I want you to copy the pset0 directory and copy 225 00:11:03,720 --> 00:11:07,530 every folder that exists inside of it and every file that 226 00:11:07,530 --> 00:11:08,750 exists inside of it. 227 00:11:08,750 --> 00:11:13,270 In other words, I need you to recursively dive down into pset0 228 00:11:13,270 --> 00:11:15,660 and make a copy of everything in there. 229 00:11:15,660 --> 00:11:25,280 >> If I want to do that, what I can do is cp-r, for recursive, pset0 pset3. 230 00:11:25,280 --> 00:11:26,850 Hit Enter. 231 00:11:26,850 --> 00:11:29,290 Now, if I list the contents of my directory, 232 00:11:29,290 --> 00:11:32,180 I see there's the pset3 directory that I can work with. 233 00:11:32,180 --> 00:11:36,990 And if I cd into pset3 now and then list the contents, look, 234 00:11:36,990 --> 00:11:39,594 there's sample and scratch.sb2 again. 235 00:11:39,594 --> 00:11:40,510 So that's pretty cool. 236 00:11:40,510 --> 00:11:43,819 So that's how you can copy an entire directory, and not just a single file. 237 00:11:43,819 --> 00:11:45,860 So if you want to copy a directory, just remember 238 00:11:45,860 --> 00:11:49,830 to use the -r flag when you're working with the cp command. 239 00:11:49,830 --> 00:11:50,330 All right. 240 00:11:50,330 --> 00:11:52,320 So I've copied a file, but I've done it by mistake. 241 00:11:52,320 --> 00:11:53,695 And now, I want to get rid of it. 242 00:11:53,695 --> 00:11:54,850 How do I do that? 243 00:11:54,850 --> 00:11:56,830 Again, if you're familiar with a GUI interface, 244 00:11:56,830 --> 00:11:58,310 you can right-click and just choose Delete. 245 00:11:58,310 --> 00:12:00,630 And it'll send it to the trash or the Recycle Bin. 246 00:12:00,630 --> 00:12:03,320 But at the command line, we can just type rm, for remove, 247 00:12:03,320 --> 00:12:05,580 and then the name of the file we want to get rid of. 248 00:12:05,580 --> 00:12:08,614 >> Now, rm is very careful. 249 00:12:08,614 --> 00:12:11,280 It does a lot of double-checking, to make sure that you actually 250 00:12:11,280 --> 00:12:12,377 want to delete the file. 251 00:12:12,377 --> 00:12:15,460 It doesn't want to make any mistakes, because there's no Recycle Bin here. 252 00:12:15,460 --> 00:12:17,010 Once we delete a file, it's gone. 253 00:12:17,010 --> 00:12:18,710 And there's really no way to recover it. 254 00:12:18,710 --> 00:12:20,730 So we need to be pretty careful, and so rm 255 00:12:20,730 --> 00:12:23,110 is going to be really careful for us. 256 00:12:23,110 --> 00:12:27,120 >> So let's go back to the IDE and get rid of that hi.txt file 257 00:12:27,120 --> 00:12:29,640 that I created a minute ago with copy. 258 00:12:29,640 --> 00:12:30,560 So we're in the IDE. 259 00:12:30,560 --> 00:12:33,180 And I list the contents of my directory again, just to give us some context. 260 00:12:33,180 --> 00:12:36,500 I mean, you can see that there's hi.txt, which I created a little while ago. 261 00:12:36,500 --> 00:12:38,196 Now, I want to get rid of it. 262 00:12:38,196 --> 00:12:38,820 How do I do it? 263 00:12:38,820 --> 00:12:39,810 >> Again, just rm. 264 00:12:39,810 --> 00:12:46,220 So I can type rm hi.txt and hit Enter. 265 00:12:46,220 --> 00:12:47,970 And there's rm being really careful for us 266 00:12:47,970 --> 00:12:50,178 and making sure we actually want to delete this file. 267 00:12:50,178 --> 00:12:52,784 Do you really want to remove regular empty file hi.txt? 268 00:12:52,784 --> 00:12:55,200 That's just like an operating system, like Windows or Mac, 269 00:12:55,200 --> 00:12:56,310 that you might be familiar with popping up 270 00:12:56,310 --> 00:12:58,770 that box says are you really sure you want to do this. 271 00:12:58,770 --> 00:13:01,228 >> I'm pretty sure I want to this, so I'm going to type, yes-- 272 00:13:01,228 --> 00:13:04,390 or I could also just type y-- and hit Enter. 273 00:13:04,390 --> 00:13:08,620 And if I list the contents of my directory again, hi.txt is gone. 274 00:13:08,620 --> 00:13:10,000 Not bad, right? 275 00:13:10,000 --> 00:13:12,410 Let's press Control L, just to get rid of all this stuff 276 00:13:12,410 --> 00:13:14,900 and get us back at the top of the screen. 277 00:13:14,900 --> 00:13:18,520 >> Now, there is a way to short circuit rm, so 278 00:13:18,520 --> 00:13:20,090 that it doesn't ask us that question. 279 00:13:20,090 --> 00:13:22,060 We really know we want to get rid of that file, 280 00:13:22,060 --> 00:13:23,490 and we don't even want to be asked the question. 281 00:13:23,490 --> 00:13:24,720 How do we do that? 282 00:13:24,720 --> 00:13:27,180 Well, we can specify an extra flag, just like we 283 00:13:27,180 --> 00:13:30,400 did with cp where we could add the -r flag, 284 00:13:30,400 --> 00:13:32,800 to recursively copy into a directory. 285 00:13:32,800 --> 00:13:35,630 There's another flag for rm, which is -f, 286 00:13:35,630 --> 00:13:39,470 which is to force rm to do exactly what we're telling it to do. 287 00:13:39,470 --> 00:13:44,610 >> So let's say that, now, I want to get rid of my hello.txt file. 288 00:13:44,610 --> 00:13:46,490 I don't want that one either. 289 00:13:46,490 --> 00:13:47,370 What can I do? 290 00:13:47,370 --> 00:13:54,710 Well, I can rm -f hello.txt. 291 00:13:54,710 --> 00:13:57,530 What do you think is going to happen here? 292 00:13:57,530 --> 00:13:58,030 It's gone. 293 00:13:58,030 --> 00:13:59,363 Didn't even ask me the question. 294 00:13:59,363 --> 00:14:03,600 And in fact, if I list the contents of my directory again, it's gone. 295 00:14:03,600 --> 00:14:05,750 There's no undo here. 296 00:14:05,750 --> 00:14:09,554 hello.txt is gone, and I wasn't even asked if I wanted to delete it. 297 00:14:09,554 --> 00:14:11,595 What if I want to get rid of an entire directory? 298 00:14:11,595 --> 00:14:14,180 299 00:14:14,180 --> 00:14:17,039 The way you do that is very similar to copy. 300 00:14:17,039 --> 00:14:19,080 If you want to delete a directory, you don't just 301 00:14:19,080 --> 00:14:22,121 want to delete the directory, you want to delete everything inside of it. 302 00:14:22,121 --> 00:14:26,250 And so you use -r, to recursively delete a directory. 303 00:14:26,250 --> 00:14:28,600 >> So let's rm -r pset2. 304 00:14:28,600 --> 00:14:31,870 I'm not actually working on pset2, so I can get rid of that directory. 305 00:14:31,870 --> 00:14:33,040 I hit Enter. 306 00:14:33,040 --> 00:14:35,050 Do I want to remove the directory, pset2? 307 00:14:35,050 --> 00:14:37,160 Yes, I do. 308 00:14:37,160 --> 00:14:41,150 And if I list the contents of my directory again, it's gone. 309 00:14:41,150 --> 00:14:41,650 All right? 310 00:14:41,650 --> 00:14:43,739 >> One more variation on this theme. 311 00:14:43,739 --> 00:14:46,280 So I'll clear the screen again, to put everything at the top. 312 00:14:46,280 --> 00:14:47,420 Hit ls. 313 00:14:47,420 --> 00:14:49,890 Now, I want to get rid of my pset3 directory, 314 00:14:49,890 --> 00:14:52,810 and I really know I want to get rid of my pset3 directory. 315 00:14:52,810 --> 00:15:00,120 I can recursively and forcibly remove my pset3 directory. 316 00:15:00,120 --> 00:15:03,900 >> Now, you're going to be really careful when you're using rm -rf. 317 00:15:03,900 --> 00:15:06,784 As you program more, it'll be something you just do really quickly. 318 00:15:06,784 --> 00:15:08,200 And in fact, I do it all the time. 319 00:15:08,200 --> 00:15:10,380 But it can lead to some disastrous consequences, 320 00:15:10,380 --> 00:15:12,340 if you accidentally delete the wrong thing. 321 00:15:12,340 --> 00:15:15,089 So I want you to be aware that this is an option available to you, 322 00:15:15,089 --> 00:15:17,220 but use it sparingly, and use it with caution. 323 00:15:17,220 --> 00:15:20,910 >> I know for a fact I'm not working on my problem set 3 anymore, so I'm 324 00:15:20,910 --> 00:15:22,550 going to get rid of all of those files. 325 00:15:22,550 --> 00:15:26,420 And I know I don't want to be asked every single time if I want to delete 326 00:15:26,420 --> 00:15:30,640 a file, so I'm going to rm -rf pset3. 327 00:15:30,640 --> 00:15:32,640 Didn't even ask me if I wanted to get rid of it. 328 00:15:32,640 --> 00:15:33,840 I hit ls. 329 00:15:33,840 --> 00:15:35,680 pset3 is gone. 330 00:15:35,680 --> 00:15:39,730 So that's all the variations that you can use to get rid of files with rm. 331 00:15:39,730 --> 00:15:43,620 >> The last command line command that's going to pretty useful for you 332 00:15:43,620 --> 00:15:46,391 is mv, which is short for move. 333 00:15:46,391 --> 00:15:48,960 Move Is basically equivalent to rename. 334 00:15:48,960 --> 00:15:53,870 It moves a file from one location to another, the source to the destination. 335 00:15:53,870 --> 00:15:59,400 Let's see where using a move might be handy at the terminal window. 336 00:15:59,400 --> 00:16:02,070 >> So I'm in my pset1 directory, and I've noticed 337 00:16:02,070 --> 00:16:03,860 something has gone a little wrong. 338 00:16:03,860 --> 00:16:08,620 I was working on my greedy problem, but I accidentally named it greddy. 339 00:16:08,620 --> 00:16:11,380 So when I try and run it through check50, it doesn't really work. 340 00:16:11,380 --> 00:16:12,630 I do have a couple of options. 341 00:16:12,630 --> 00:16:16,220 We could do what we've done before, which is to make a copy of the file. 342 00:16:16,220 --> 00:16:23,960 I could copy greddy.c to greedy.c, right? 343 00:16:23,960 --> 00:16:25,580 Hit Enter. 344 00:16:25,580 --> 00:16:28,180 I see that both files are there. 345 00:16:28,180 --> 00:16:36,337 And then I could rm, to remove, greddy.c. 346 00:16:36,337 --> 00:16:37,170 That gets rid of it. 347 00:16:37,170 --> 00:16:40,920 So that would work, after I confirm that I want to get rid of it. 348 00:16:40,920 --> 00:16:44,770 And I effectively have renamed greddy.c to greedy.c. 349 00:16:44,770 --> 00:16:48,460 I copied it, and then removed the original. 350 00:16:48,460 --> 00:16:50,170 >> But that's a multi-step process. 351 00:16:50,170 --> 00:16:51,670 And certainly, there's a better way. 352 00:16:51,670 --> 00:16:52,420 In fact, there is. 353 00:16:52,420 --> 00:16:55,810 So let's hit Control L, just to bring this back up, 354 00:16:55,810 --> 00:16:58,680 and list the contents of my directory again. 355 00:16:58,680 --> 00:17:00,350 >> So let's say, oh, I made a mistake. 356 00:17:00,350 --> 00:17:03,470 I actually really did want to call that file greddy.c. 357 00:17:03,470 --> 00:17:11,369 In one fell swoop, I can type move greedy.c to greddy.c. 358 00:17:11,369 --> 00:17:12,470 Hit Enter. 359 00:17:12,470 --> 00:17:15,210 And now, I didn't have to copy and remove it, 360 00:17:15,210 --> 00:17:16,880 I just was able to rename it. 361 00:17:16,880 --> 00:17:18,710 One-stop shopping. 362 00:17:18,710 --> 00:17:21,160 >> Now, there are a lot of other basic command line utilities 363 00:17:21,160 --> 00:17:22,640 that you can use with the Linux command line. 364 00:17:22,640 --> 00:17:24,990 And we'll be discussing a lot of them in the future in CS50. 365 00:17:24,990 --> 00:17:27,948 But for now, just getting your feet wet with working with this terminal 366 00:17:27,948 --> 00:17:30,540 environment, these five commands should get you pretty far 367 00:17:30,540 --> 00:17:34,990 in navigating around and working with files in your IDE 368 00:17:34,990 --> 00:17:37,770 or in whatever Linux-based operating system you're using. 369 00:17:37,770 --> 00:17:40,580 >> If you're curious and you want to look ahead a little bit at some 370 00:17:40,580 --> 00:17:43,420 of the command line commands we'll be using in the future, 371 00:17:43,420 --> 00:17:46,930 here's a list of a couple of the more common ones. 372 00:17:46,930 --> 00:17:47,820 I'm Doug Lloyd. 373 00:17:47,820 --> 00:17:49,860 This is CS50. 374 00:17:49,860 --> 00:17:52,598