All right. 1
So in this lesson, I want to introduce you to directory creation, file creation, and directory manipulation. 2
And we're going to be doing all of this inside the command line just with our keyboards. 3
S,o again, right now I'm starting in my root directory over him and I am going to create a new folder 4
right here. So, obviously, I can go through the right click. 5
New Folder all of that. 6
But we are pro developers now, 7
so we're going to get used to using the command line. 8
So the command is mkdir or make directory, 9
and then we're going to give our directory a name. 10
So let's just call it Angela, 11
right? 12
And you can see, there you go, pops up a new folder called Angela. 13
Now, let's cd into this new directory. And you can see that if I click on here or if I use "ls," there's 14
absolutely nothing in there, it's a empty folder. 15
So what if I wanted to create a file? 16
So in our graphical user interface, I could say open up an application like TextEdit, and I could make 17
a file and write something in it, and then save it inside my new directories. 18
So I would have to do a little bit of navigating in order to get there, and then I have to give my file 19
a name. 20
Let's call it Text and hit Save. 21
So, now we have to exit the application. 22
And after all of that work, I've got a text file. And if we do "ls," now, you can see that Text.rtf exists. 23
But if you want to do the same thing using the command line, then it's as simple as simply typing "touch" 24
to create a new file, 25
then we're going to specify the name of the file, let's call it Text2, and then we give an extension .txt. 26
And you can open in either by double clicking on it, which is what we're used to, 27
or you can simply write open Text2, and it will open it using the default application which is TextEdit 28
in a Mac. Now, you can specify which application you want to open this file in. 29
And all you have to do is, say, the "open," and you're going to use the -a flag, 30
then you're going to specify the application that you want to use. 31
So let's say that we want to open it inside Atom, so I can write Atom, and then I can specify the 32
file that I want to open, so Text2.txt, and hit enter. 33
And it will open up the Atom application that I've got, and then open up my files for me to be able 34
to edit. 35
So what if you wanted to delete files inside the command line? So currently, I'm inside the Angela folder 36
and there are two files, and I want to delete 37
my Text.rtf. 38
So then all you need to do is just use "rm"command which removes files. 39
If you type the command as is, so just "rm," 40
and then we're going to specify Text.rtf to remove. 41
And once I hit enter, 42
you'll see it disappear also in the Finder as well. 43
Now, what if I want to remove all the files inside a folder? 44
So let's create some more text files. 45
So let's call it Text.txt, and then let's create Text 3.txt. 46
So now I've got Text, Text2, Text3. A little bit of a tongue twister there. 47
And I want to remove all of them. 48
So to do that, all you have to do is just hit-- 49
So to do that, you have to be inside the directory that you want to remove all the files from. 50
And now I want to remove all of them in one go without having to type remove Text.txt, et cetera. 51
So, again, we're using the remove command and then we're simply going to use the asterisk as a wildcard which 52
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 know, trying to remove all the files inside your root directory, 60
then it's 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 here, now if you're inside your root directory and you tried to do remove--and you tried 63
to do remove star, nothing bad is actually going to happen, because there aren't any files that are inside 64
this directory, 65
there's only folders. And to remove folders or remove directories, 66
there's a different command. 67
So, now I want to remove this Angela folder that I've created and I'm inside the right place to do it, 68
I'm inside my root directory which is at this level. And, again, always check where you are 69
by using the "ls" command. You can see that I am at this level. 70
So, now I want to remove this Angela directory and if I just simply write remove Angela, and I hit enter, 71
you can see it says that Angela is a directory. 72
So this remove command by itself is not going to work. 73
So to do it, we actually have to add a flag which is the -r flag. 74
So this allows you to remove a directory. 75
So, now let's try that again. 76
Remove -r, 77
and then we're going to use the Angela directory. 78
And if we watch over here and I hit enter, you can see that that directory is now gone including all 79
the files that it held, all the folders that are inside the Angela folder, and it removes all of its child 80
components. 81
So, again, these are destructive commands. 82
Be really careful when you're using them. 83
And in fact, there's a pretty hilarious bit of internet history which is where somebody submitted a question 84
to StackExchange serverfault which is similar to StackOverflow, 85
but it's more for assistant admin and network admins. 86
So what he said was "Monday morning mistake," 87
he used the sudo command which stands for super user do, which uses your admin privileges to basically 88
allow you to do things that are, probably, a little bit dangerous, and use the remove command, and then the 89
"-rf" flag. 90
So we've already seen the "-r" which allows you to remove entire directories and all the folders and 91
all the child folders it contained. 92
He also used the "f" flag which stands for force. So this doesn't ask you for a confirmation. 93
And then the very last thing which is something that you should probably never ever write and there's 94
very few cases where this is actually useful which is the "no-preserve-root." 95
And what that means is that along with everything else inside this command, it basically wipes your hard 96
disk to the point where you cannot recover it. 97
This guy said, "Monday morning mistake." 98
"I basically just wiped my entire server," which obviously contains data from other people who are using 99
his server. 100
"And what do I do?" 101
So as it turns out, this is actually a hoax where this guy is trying to advertise his server company. 102
And I mean it's clever or is it not clever? 103
I'm not sure that it's positive PR, but it got a lot of attention on the internets. 104
So if you're interested, you can have a read of this and a link to it in this lesson. 105
But basically, if you have a look at the answers, you can see that this is something really bad to never 106
do. So with great power comes great responsibility. 107
And in this module, I've introduced you to some of the most common commands that you're going to be finding 108
yourself using, 109
and also some of the most common shortcuts. In the last lesson, I posted a link to a cheat sheet of some 110
other shortcuts that you can have a look at. 111
And if you want to learn more about the command line, then this is a website that I highly recommend. 112
I have nothing to do with them. 113
It's just something that I found to be written quite well. 114
So it's completely free and it's called the Command Line Tutorial, Learn Enough Command Line to Be Dangerous. 115
Again, the title is repeating what we just said before, but it goes into more depth like a grepping kerl, 116
all those things that you might have come across, and you will certainly come across more of it in your 117
journey as a developer. 118
So have a look at that if you're interested. 119
And just before we head off, I want to show you something quite cool. Let's call it a bonus. 120
And if you go into a Terminal and you type emacs -batch -l, and it's dunnet with two "Ns," 121
and hit enter, 122
you will get taken into a text-based to your own adventure game that dates back a long time, and it's 123
included in emacs which is a text editor that comes bundled with all macOS. 124
So you might have heard of Vim and Emacs, a highly divisive issue, and it's a really cool game that you can 125
play within Terminal. 126
And I myself have probably spent more than, you know, a good few hours on this before. 127
So, again, play at your own peril. 128
So it tells you a bit of a story and you can write text commands to play the game. 129
So, for example, at this point, you're at a dead end of a dirt road and you find a shovel. 130
So let's say something like, "Use a shovel." 131
"I don't understand that." 132
Okay, let's try something else. 133
What about "pick up the shovel"? 134
"I don't understand that." 135
What about "take shovel"? 136
Okay, great. 137
So it recognizes that. "Taken." 138
And what can we do with the shovel? Let's try "dig." "Digging here reveals nothing." 139
And this is my personal favorite. 140
What if let's just "eat shovel"? 141
So as you can see, there's some pretty interesting end points. And have a play with it if you're interested, 142
but otherwise, I will see you on the next module.