WEBVTT 0 00:00.840 --> 00:01.070 All right. 1 00:01.080 --> 00:06.450 So in this lesson I want to introduce you to directory creation, 2 00:06.450 --> 00:12.060 file creation and directory manipulation. And we're going to be doing all of this inside the command line, 3 00:12.210 --> 00:13.470 just with our keyboards. 4 00:13.470 --> 00:21.690 So again right now I'm starting in my root directory over here and I am going to create a new folder 5 00:22.020 --> 00:22.950 right here. 6 00:23.130 --> 00:30.570 So obviously I can go through the right click, New Folder, all of that, but we are pro developers now, 7 00:30.600 --> 00:37.410 so we're going to get used to using the command line. So the command is mkdir or make directory, and 8 00:37.470 --> 00:39.560 then we're going to give our directory a name, 9 00:39.570 --> 00:42.720 so let's just call it Angela, right? 10 00:42.850 --> 00:47.520 You can see, there you go, pops up a new folder called Angela. 11 00:47.580 --> 00:58.600 Now let's cd into this new directory, and you can see that if I click on here, or if I use ls, 12 00:58.750 --> 01:00.560 that there's absolutely nothing in there. 13 01:00.570 --> 01:03.090 It's a empty folder. 14 01:03.090 --> 01:05.540 So what if I wanted to create a file? 15 01:05.730 --> 01:13.650 So in our graphical user interface I could, say, open up an application, like TextEdit, and I could make 16 01:13.650 --> 01:19.860 a file and write something in it and then save it inside my new directory. 17 01:19.890 --> 01:24.080 So I would have to do a little bit of navigating in order to get there, 18 01:24.090 --> 01:28.830 and then I have to give my file a name, let's call it Text and hit save. 19 01:28.830 --> 01:36.600 So now we have to exit the application and after all of that work I've got a text file and if we do 20 01:36.700 --> 01:36.990 ls 21 01:36.990 --> 01:43.440 now, you can see that text.rtf exists. But if you want to do the same thing using the command line 22 01:43.500 --> 01:48.460 then it's as simple as simply typing touch to create a new file. 23 01:48.660 --> 01:53.630 Then we're going to specify the name of the file, let's call it Text2, and then we give it an extension, 24 01:53.640 --> 02:00.780 .txt. And you can open it either by double clicking on it, which is what we're used to, or you can 25 02:00.780 --> 02:11.010 simply write open Text2, and it will open it using the default application, which is TextEdit in a 26 02:11.010 --> 02:19.020 Mac. Now you can specify which application you want to open this file in, and all you have to do is say 27 02:19.050 --> 02:25.800 the open, and you're going to use a -a flag, then you're going to specify the application that you 28 02:25.800 --> 02:26.510 want to use. 29 02:26.520 --> 02:34.340 So let's say that we want to open it inside Atom. So I can write Atom, and then I can specify that the 30 02:34.350 --> 02:41.580 file that I want to open, so Text2.txt, and hit enter, and it will open up the Atom application 31 02:41.580 --> 02:45.150 that I've got and then open up my file for me to be able to edit. 32 02:45.300 --> 02:52.560 So what if you wanted to delete files inside the command line? So currently I'm inside the Angela folder 33 02:52.800 --> 02:58.240 and there are two files, and I want to delete my Text.rtf. 34 02:58.440 --> 03:04.320 So then all you need to do is just use the rm command, which removes files 35 03:04.680 --> 03:13.110 if you type the command as is, so just rm, and then we're going to specify Text.rtf to remove, 36 03:13.230 --> 03:16.150 and once I hit enter you'll see it disappear 37 03:16.230 --> 03:17.820 also in the Finder as well. 38 03:18.150 --> 03:21.540 Now what if I want to remove all the files inside a folder? 39 03:21.750 --> 03:34.850 So let's create some more text files. So let's call it Text.txt, and then let's create 40 03:34.890 --> 03:35.520 Text3.txt. 41 03:35.520 --> 03:39.330 So now I've got Text, Text2, Text3. 42 03:39.380 --> 03:40.590 Little bit of a tongue twister there. 43 03:41.530 --> 03:43.700 And I want to remove all of them. 44 03:43.740 --> 03:48.960 So to do that you have to be inside the directory that you want to remove all the files from, and if 45 03:48.960 --> 03:51.750 you want to know which directory you're currently in, 46 03:51.750 --> 03:55.440 you can either look at the part that's before your user name and prompt, 47 03:55.470 --> 04:02.610 so in this case I'm inside a folder called Angela on Angela's MacBook Pro, but you can also use the 48 04:02.610 --> 04:09.120 pwd, or print working directory command, and then when you hit enter it tells you your entire path and 49 04:09.120 --> 04:16.770 your current folder location. And now I want to remove all of them in one go without having to type remove 50 04:16.800 --> 04:18.580 Text.txt, etc.. 51 04:18.690 --> 04:25.200 So again we're using the remove command and then we're simply going to use the * as a wildcard 52 04:25.530 --> 04:28.950 which matches everything that has every single name. 53 04:28.980 --> 04:35.190 And now if I hit enter you can see that all my three files inside my current directory are removed. 54 04:35.490 --> 04:40.170 So do you remember in the beginning of this module we mentioned that using the command line gives you 55 04:40.170 --> 04:42.390 more power, more control? 56 04:42.390 --> 04:49.350 And, as we all know, with great power comes great responsibility, and that means that you have to be careful 57 04:49.590 --> 04:56.070 when using certain commands because the graphical user interface isn't there to protect you. 58 04:56.070 --> 05:03.030 So for example if you were not inside the directory that you wanted to delete, say if you were inside 59 05:03.120 --> 05:10.710 your root directory and you, you know, tried to remove all the files inside your root directory, then it's 60 05:10.710 --> 05:12.910 probably not such a great idea. 61 05:12.930 --> 05:19.590 So always check which location you're in before you're doing some of these destructive commands. 62 05:19.590 --> 05:23.970 Now if you're inside your root directory and you tried to do remove *, 63 05:23.970 --> 05:29.550 nothing bad is actually going to happen, because there aren't any files that are inside this directory. 64 05:29.550 --> 05:35.680 There's only folders and to remove folders or remove directories there's a different command. 65 05:35.700 --> 05:42.500 So now I want to remove this Angela folder that I've created and I'm inside the right place to do it. 66 05:42.510 --> 05:46.100 I'm inside my root directory which is at this level. 67 05:46.230 --> 05:50.640 And again always check where you are by using the ls command. 68 05:50.640 --> 05:52.890 You can see that I am at this level. 69 05:52.890 --> 06:01.240 So now I want to remove this Angela directory and if I just simply write remove Angela and I hit enter, 70 06:01.260 --> 06:04.240 you can see it says that Angela is a directory. 71 06:04.290 --> 06:08.140 So this remove command by itself is not going to work. 72 06:08.160 --> 06:13.170 So to do it we actually have to add a flag which is the -r flag. 73 06:13.440 --> 06:16.530 So this allows you to remove a directory. 74 06:16.530 --> 06:22.140 So now let's try that again remove -r, and then we're going to use the Angela directory, and if 75 06:22.140 --> 06:28.710 we watch over here and I hit enter you can see that that directory is now gone, including all the files 76 06:28.710 --> 06:36.450 that it held, all the folders that are inside the Angela folder, and it removes all of its child components. 77 06:36.450 --> 06:39.510 So again these are destructive commands. 78 06:39.510 --> 06:41.640 Be really careful when you're using them. 79 06:41.640 --> 06:49.950 And in fact there's a pretty hilarious bit of Internet history which is where somebody submitted a question 80 06:50.280 --> 06:54.210 to Stack Exchange Server Fault, which is similar to Stack Overflow, 81 06:54.240 --> 06:56.960 but it's more for sysadmin and network admins. 82 06:56.970 --> 07:00.580 So what he said was Monday morning mistake. 83 07:00.990 --> 07:07.350 He used the sudo command, which stands for super user do, which uses your admin privileges to basically 84 07:07.350 --> 07:13.950 allow you to do things are probably a little bit dangerous, and he used the remove command, and then the 85 07:14.040 --> 07:14.880 -rf flag. 86 07:14.880 --> 07:20.340 So we've already seen the -r, which allows you to remove entire directories and all the folders 87 07:20.520 --> 07:22.970 and all the child folders it contains. 88 07:22.980 --> 07:26.250 He also used the f flag which stands for force, 89 07:26.280 --> 07:28.650 so this doesn't ask you for a confirmation. 90 07:28.830 --> 07:34.290 And then the very last thing which is something that you should probably never ever write and there's 91 07:34.290 --> 07:39.150 very few cases where this is actually useful which is the no-preserve-root. 92 07:39.150 --> 07:47.970 And what that means is that, along with everything else inside this command, it basically wipes your hard 93 07:47.970 --> 07:51.120 disk to the point where you cannot recover it. 94 07:51.180 --> 07:53.900 This guy said Monday morning mistake, 95 07:54.000 --> 08:00.180 I basically just wiped my entire server, which obviously contains data from other people who are using 96 08:00.180 --> 08:00.920 his server. 97 08:01.030 --> 08:01.950 And what do I do? 98 08:01.950 --> 08:08.960 So as it turns out this is actually a hoax where this guy is trying to advertise his server company. 99 08:09.120 --> 08:12.000 And I mean it's clever or is it not clever. 100 08:12.000 --> 08:18.440 I'm not sure that it's positive PR but it got a lot of attention on the internets. 101 08:18.450 --> 08:22.980 So if you're interested you can have a read of this, and I’ll link to it in this lesson, 102 08:22.980 --> 08:28.830 but basically if you have a look at the answers, you can see that this is something really bad to never 103 08:28.830 --> 08:29.150 do. 104 08:29.220 --> 08:31.830 So with great power comes great responsibility. 105 08:31.890 --> 08:36.600 And in this module I've introduced you to some of the most common commands that you're going to be finding 106 08:36.600 --> 08:40.020 yourself using and also some of the most common shortcuts. 107 08:40.170 --> 08:45.720 And if you want to learn more about the command line then this is a web site that I highly recommend. 108 08:45.720 --> 08:49.550 I have nothing to do with them, it’s just something that I found to be written quite well. 109 08:49.560 --> 08:51.630 So it's completely free, 110 08:51.630 --> 08:56.130 and it's called the Command Line Tutorial, Learn Enough Command Line to Be Dangerous. 111 08:56.130 --> 09:03.210 Again the title is repeating what we just said before, but it goes into more depth like grepping, curl, 112 09:03.240 --> 09:07.950 all of those things that you might have come across and you will certainly come across more of it in 113 09:07.950 --> 09:09.570 your journey as a developer.