All right. 1
So in this lesson I want to introduce you to directory creation, 2
file creation and directory manipulation. And we're going to be doing all of this inside the command line, 3
just with our keyboards. 4
So again right now I'm starting in my root directory over here and I am going to create a new folder 5
right here. 6
So obviously I can go through the right click, New Folder, all of that, but we are pro developers now, 7
so we're going to get used to using the command line. So the command is mkdir or make directory, and 8
then we're going to give our directory a name, 9
so let's just call it Angela, right? 10
You can see, there you go, pops up a new folder called Angela. 11
Now let's cd into this new directory, and you can see that if I click on here, or if I use ls, 12
that there's absolutely nothing in there. 13
It's a empty folder. 14
So what if I wanted to create a file? 15
So in our graphical user interface I could, say, open up an application, like TextEdit, and I could make 16
a file and write something in it and then save it inside my new directory. 17
So I would have to do a little bit of navigating in order to get there, 18
and then I have to give my file a name, let's call it Text and hit save. 19
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
ls 21
now, you can see that text.rtf exists. But if you want to do the same thing using the command line 22
then it's as simple as simply typing touch to create a new file. 23
Then we're going to specify the name of the file, let's call it Text2, and then we give it an extension, 24
.txt. And you can open it either by double clicking on it, which is what we're used to, or you can 25
simply write open Text2, and it will open it using the default application, which is TextEdit in a 26
Mac. Now you can specify which application you want to open this file in, and all you have to do is say 27
the open, and you're going to use a -a flag, then you're going to specify the application that you 28
want to use. 29
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
file that I want to open, so Text2.txt, and hit enter, and it will open up the Atom application 31
that I've got and then open up my file for me to be able to edit. 32
So what if you wanted to delete files inside the command line? So currently I'm inside the Angela folder 33
and there are two files, and I want to delete my Text.rtf. 34
So then all you need to do is just use the rm command, which removes files 35
if you type the command as is, so just rm, and then we're going to specify Text.rtf to remove, 36
and once I hit enter you'll see it disappear 37
also in the Finder as well. 38
Now what if I want to remove all the files inside a folder? 39
So let's create some more text files. So let's call it Text.txt, and then let's create 40
Text3.txt. 41
So now I've got Text, Text2, Text3. 42
Little bit of a tongue twister there. 43
And I want to remove all of them. 44
So to do that you have to be inside the directory that you want to remove all the files from, and if 45
you want to know which directory you're currently in, 46
you can either look at the part that's before your user name and prompt, 47
so in this case I'm inside a folder called Angela on Angela's MacBook Pro, but you can also use the 48
pwd, or print working directory command, and then when you hit enter it tells you your entire path and 49
your current folder location. And now I want to remove all of them in one go without having to type remove 50
Text.txt, etc.. 51
So again we're using the remove command and then we're simply going to use the * as a wildcard 52
which matches everything that has every single name. 53
And now if I hit enter you can see that all my three files inside my current directory are removed. 54
So do you remember in the beginning of this module we mentioned that using the command line gives you 55
more power, more control? 56
And, as we all know, with great power comes great responsibility, and that means that you have to be careful 57
when using certain commands because the graphical user interface isn't there to protect you. 58
So for example if you were not inside the directory that you wanted to delete, say if you were inside 59
your root directory and you, you know, tried to remove all the files inside your root directory, then it's 60
probably not such a great idea. 61
So always check which location you're in before you're doing some of these destructive commands. 62
Now if you're inside your root directory and you tried to do remove *, 63
nothing bad is actually going to happen, because there aren't any files that are inside this directory. 64
There's only folders and to remove folders or remove directories there's a different command. 65
So now I want to remove this Angela folder that I've created and I'm inside the right place to do it. 66
I'm inside my root directory which is at this level. 67
And again always check where you are by using the ls command. 68
You can see that I am at this level. 69
So now I want to remove this Angela directory and if I just simply write remove Angela and I hit enter, 70
you can see it says that Angela is a directory. 71
So this remove command by itself is not going to work. 72
So to do it we actually have to add a flag which is the -r flag. 73
So this allows you to remove a directory. 74
So now let's try that again remove -r, and then we're going to use the Angela directory, and if 75
we watch over here and I hit enter you can see that that directory is now gone, including all the files 76
that it held, all the folders that are inside the Angela folder, and it removes all of its child components. 77
So again these are destructive commands. 78
Be really careful when you're using them. 79
And in fact there's a pretty hilarious bit of Internet history which is where somebody submitted a question 80
to Stack Exchange Server Fault, which is similar to Stack Overflow, 81
but it's more for sysadmin and network admins. 82
So what he said was Monday morning mistake. 83
He used the sudo command, which stands for super user do, which uses your admin privileges to basically 84
allow you to do things are probably a little bit dangerous, and he used the remove command, and then the 85
-rf flag. 86
So we've already seen the -r, which allows you to remove entire directories and all the folders 87
and all the child folders it contains. 88
He also used the f flag which stands for force, 89
so this doesn't ask you for a confirmation. 90
And then the very last thing which is something that you should probably never ever write and there's 91
very few cases where this is actually useful which is the no-preserve-root. 92
And what that means is that, along with everything else inside this command, it basically wipes your hard 93
disk to the point where you cannot recover it. 94
This guy said Monday morning mistake, 95
I basically just wiped my entire server, which obviously contains data from other people who are using 96
his server. 97
And what do I do? 98
So as it turns out this is actually a hoax where this guy is trying to advertise his server company. 99
And I mean it's clever or is it not clever. 100
I'm not sure that it's positive PR but it got a lot of attention on the internets. 101
So if you're interested you can have a read of this, and I’ll link to it in this lesson, 102
but basically if you have a look at the answers, you can see that this is something really bad to never 103
do. 104
So with great power comes great responsibility. 105
And in this module I've introduced you to some of the most common commands that you're going to be finding 106
yourself using and also some of the most common shortcuts. 107
And if you want to learn more about the command line then this is a web site that I highly recommend. 108
I have nothing to do with them, it’s just something that I found to be written quite well. 109
So it's completely free, 110
and it's called the Command Line Tutorial, Learn Enough Command Line to Be Dangerous. 111
Again the title is repeating what we just said before, but it goes into more depth like grepping, curl, 112
all of those things that you might have come across and you will certainly come across more of it in 113
your journey as a developer.