From the course: Unix Essential Training

Creating directories - Unix Tutorial

From the course: Unix Essential Training

Start my 1-month free trial

Creating directories

- [Instructor] Now that we know how to create files in Unix, let's learn how to create directories. The first thing you need to know is that the directories that we get to from the Unix shell are the same as what we access from the graphical user interface. So for example, if we do LS, and we see a list of the different files and directories that are in the home directory and then I go to the graphical user interface we see those same files and directories are here. That means that I could easily create a new directory using new folder or whatever your operating system uses to create a new folder or directory in the graphical user interface and it will then show up here in the listing inside Unix. They're exactly the same. But we can also create directories from inside Unix. And we can do that with the mkdir command. That's short for make directory. Mkdir space and then the path to the directory we want to create. By default, it's going to be in the current working directory. So if I use my_project, that will create a directory inside the current directory called my_project. Let's use LS and then space dash L to get a listing. And you'll see that it created my project and you'll see that it is a directory. It has a D in front of it. Let's try making that same directory again, underscore my project and you'll see that it already exists. So it won't let us create a directory if one already exists. This creates a directory in the current working directory, but we can also provide a path somewhere else. For example, we could do mkdir and then my_project and then junk, and that'll make a directory inside the my project directory called junk. We do LS and my_project after it, it'll list the contents of the my project directory and you see the directory there called junk. Let's go up and just add the dash L option. And we can see that as a directory. Now I want to share with you a useful trick. Let's say that we want to create two nested directories. For example, let's say we wanted to make dir and then use the my project directory, but inside there we know there's only one directory right now called junk. Let's create a new directory called test one and inside there, another directory called test two. If we hit return, it's going to say, sorry, I can't do that because test one doesn't exist. That's probably what you would expect to happen, right? We need to create test one and then we could create test two inside of it. But a nice trick is that you can use the dash P option and you can create them all at once. Now, if I do LS dash L for my project, you can see it has test one. And if I then look inside, test one, it has test two. It doesn't complain anymore. It just creates all of the parent directories that it needs on the way to creating that child directory. So that's how you create directories in Unix. You use the mkdir command. You provide a path to the directory. The directory can't already exist. And if some directories in the path of that directory don't exist, you can add the dash P option to create those parent directories at the same time.

Contents