The mkdir (make directory) command is pretty universal as far as command languages go. And, in fact, you will find it in UNIX and UNIX-like systems as well as used in scripting languages. You will even find it used in the MS-DOS vocabulary of command.
Normal usage of mkdir is pretty straightforward, and basic arguments can be stated by using the following syntax:
It is worth note, however, that unless the location of the directory is specified in the argument mkdir will create a new directory inside of the directory you call the command in. The mkdir command can even handle the creation of multiple directories, separated by a space in the argument.
Like many commands, mkdir LINUX returns no status if everything executed successfully and an error if things happened to go wrong. This command is extremely powerful, though it is rather singular in its purpose, and the -p (parents) switch will allow users to create a directory anywhere in the filesystem, even if the parent folder does not already exist.
?MKDIR LINUX Nuts and Bolts
So, that is the skinny on mkdir, but say you wanted to make a new directory in your home folder but did know the entire path. Well, by default, the Terminal or command-line is set up to use your home folder, and if you ever find yourself needing to refer to it quickly the shortcut cd ~ will get you there in the blink of an eye.
But let us assume we are already there and ready to start using mkdir. Just type the following mkdir LINUX command to make your first folder:
That’s it. But, say you wanted to see the permissions on that new folder you just created. Well, thanks to our friends at Lifewire, we have an answer. If you run the ls command with the -l and -t flags (long format sorted by timestamp), it will list the contents of your current directory (which your new folder should be in) and each folder’s read-write access.
That readout would look something like the following command return:
drwx------+ 4 jack staff 136 Jun 4 2016 Music
Now, setting permission gets to be a little tricky, and that is beyond the scope of this blog, but, basically, it is a three-digit number that can be understood by noting that 7 is equivalent to full permission and 0 equals no permission, with the understanding that the first number is for the owner, the second for the group, and the third for everyone else.
So, say you wanted to make sure everyone had full permissions to the folder because you were going to share it. Then you would use chmod (change mode) and the three-digit number we just talked about in an argument that looks like the following:
chmod 777 commonstuff
?LINUX MKDIR Works Well with Others
But what does all of that have to do with mkdir? Well, mkdir works fine on its own, and it is a very powerful tool, but, when you combine it with other commands, you, hopefully, begin to see the broad scope of its application.
Sure, you can spend all day practicing using mkdir and learn all of the flags and switches, until you become so familiar with it, you began to dream about little mkdir’s jumping the fence in your dreams, and that would be fine. But, it wouldn’t impart any practical knowledge.
However, since we are on the subject, let’s have a look at those switches. One you know about already, and that is the -p or parent switch, which creates a parent and directory if there is not one. And the other is -m or mode, which allows you to set permissions when you create the folder.
If you are awake and facing the right way, you will recall that we were just talking about chmod and ls, and how you could use them to set permissions and further define directories and folders. Well, as you may be able to guess, there is a slightly more efficient way, as you can set permissions when you create folders.
Before we move on to an example of that, it is worth noting that mkdir LINUX will create a new folder or directory inside the default folder of the command-line (most of the time, the user home folder) unless you tell is otherwise.
It should also be no surprise that the new folder you create will inherit the permissions of the home folder unless you tell it otherwise. So, let’s go ahead and do that now with the -m switch.
mkdir -m777 commonstuff
Of course, this LINUX blog also reminds us the switch -v (verbose) can also be used to squeeze a little more info on the command-line. The verbose option, in fact, is somewhat universal and can be used as a flag with many commands, so it is not what we would call linked to mkdir LINUX.
?MKDIR Examples and Tips and Tricks
So, we have shown you a few examples already, but let’s use the universal medium of music to help us cement why mkdir and so many other commands via the command-line are so powerful and simple to use once you get the hand of them.
This following command-line scenario is presented with help from our friends at Computer Hope. Say you wanted to create a music directory with folders for genres and artists for the download and storage of your favorite tunes. Say you had four artists you wanted to start with.
While you could spend a few minutes in the GUI setting a music folder, naming it, setting up the different genre folders and naming them rock, rap, and jazz, and setting up the four artists folders and naming them, why not let mkdir LINUX do the heavy lifting for you in one quick command line argument?
We will use the -p switch to create the parents, and we will use add multiple folders to our directory as well. Remember ~/ means home.
mkdir -p ~/music/rock/CCR ~/music/rock/nickleback ~/music/rap/rm ~/music/jazz/louisarmstrong
Poof. Like that, you have to start of a music directory. If you open up your file manager, you will find a music file with the files “rock,” “rap,” and “jazz” inside. Inside of the genre folder, you’ll find folders with the appropriate artist’s names.
Of course, experienced LINUX command-line hounds will probably say, “well that could have been written even more elegantly,” so something like that, but you get the point, to be sure. And, hey, this is a tutorial. There is no shame in learning as you go. Plus, writing in the command-line more than you have to is a great way to learn it more quickly.
?LINUX RM Wrap-up
So, we have learned that the quickest, most efficient ways to make files and directories is to use mkdir LINUX. Even if you are more comfortable using a GUI file manager, executing a few lines of code in Terminal is always faster.
We know we have barely touched on what mkdir can do, we hope we have given you a taste of the power of the command line. A word of caution when learning to use permission, though. Always set a folder to full permissions until you get a good handle on what you are doing.
There is nothing worse than having a eureka command-line moment with mkdir LINUX only to discover you can use the folder you have just created. Of course, the beauty of the command-line is that it is usually easy to correct your mistakes if you are a user with elevated privileges, and you can just use chmod to reset permissions should this happen.
One thing that we did not talk about at all in this piece is the partner to mkdir, rmdir, which can remove entire directories at a time. This is also a powerful command, and you should practice only with folders that you have created. Like the rm command, the rmdir command can be very destructive.
Remember that if you practice and start with the basic commands, the command-line won’t seem so intimidating, you will want to practice more, and you will be well on your way to using piped arguments in no time at all.
So, start practicing now, move some files around, copy them, link them, and rm the old ones. Once you get going, you’ll wonder why they made the GUI at all.