How To Create Directories In Linux With The "mkdir" Command

Create Folders Using Linux Command Line
Create Folders Using Linux Command Line.

Introduction

This guide will show you how to create new folders or directories within Linux using the command line.

The command that you use for creating directories is mkdir. This article shows you the basic way to create directories in Linux as well as covering all the available switches.

How To Create A New Directory

The simplest way to create a new directory is as follows:

mkdir <foldername>

For example, if you want to create a directory under your home folder called test, open a terminal window and make sure you are in your home folder (use the cd ~ command).

 

mkdir test

Changing The Permissions Of The New Directory

After creating a new folder you might wish to set the permissions so that only a certain user can access the folder or so that some people can edit files in the folder but others have read only.

In the last section, I showed you how to create a directory called test. Running the ls command will show you the permissions for that directory:

ls -lt

The chances are you will have something along these lines:

drwxr-xr-x 2 owner group 4096 Mar 9 19:34 test

The bits we are interested in are drwxr-xr-x owner and group

The d tells us that test is a directory.

The first three characters after the d are the owner permissions for the directory specified by the owner's name.

  • r stands for read
  • w stands for write
  • x stands for execute (which in this case means you can access the folder)

The next three characters are the group permissions for the file specified by the group name.

Again the options are r,w and x. The - means that there is a permission missing. In the example above anybody belonging to the group can access the folder and read the files but can't write to the folder.

The final three characters are the permissions that all users have and as you can see in the example above these are the same as the group permissions.

To change the permissions for a file or folder you can use the chmod command. The chmod command lets you specify 3 numbers which set the permissions.

  • read permissions = 4
  • write permissions = 2
  • execute permissions = 1

To get a mixture of permissions you add the numbers together. For example to get read and execute permissions the number you need is 5, to get read and write permissions the number is 6 and to get write and execute permissions the number is 3.

Remember you need to specify 3 numbers as part of the chmod command. The first number is for the owner permissions, the second number is for the group permissions and the last number is for everyone else.

For example to get full permissions on the owner, read and execute permissions on the group and no permissions for anyone else type the following:

chmod 750 test

If you wish to change the group name that owns a folder use the chgrp command.

For example, imagine you want to create a directory that all the accountants in your company can access.

First of all, create the group accounts by typing the following:

groupadd accounts

If you don't have the correct permission to create a group you may need to use sudo to gain extra privileges or switch to an account with valid permissions using the su command.

Now you can change the group for a folder by typing the following:

chgrp accounts <foldername>

For example:

chgrp accounts test

To give anybody in the accounts group read, write and execute access as well as the owner but read only to everybody else you can use the following command:

chmod 770 test

To add a user into the accounts group you will probably want to use the following command:

usermod -a -G accounts <username>

The above command appends the accounts group to the list of secondary groups the user has access to.

How To Create A Directory And Set Permissions At The Same Time

You can create a directory and set the permissions for that directory at the same time using the following command:

mkdir -m777 <foldername>

The above command will create a folder which everybody has access to. It is very rare that you would want to create anything with this kind of permissions.

Create A Folder And Any Parents That Are Required

Imagine you want to create a directory structure but you don't want to create each individual folder along with the way and work your way down a tree.

For example, you might be creating folders for your music as follows:

  • /home/music/rock/alicecooper
  • /home/music/rock/queen
  • /home/music/rap/drdre
  • /home/music/jazz/louisjordan

It would be annoying to have to create the rock folder, then the alice cooper and queen folder and then create the rap folder and the dr dre folder and then the jazz folder and then the louisjordan folder.

By specifying the following switch you can create all the parent folders on the fly if they don't already exist.

mkdir -p <foldername>

For example, to create one of the folders listed above try the following command:

mkdir -p ~/music/rock/alicecooper

Getting Confirmation That A Directory Was Created

By default, the mkdir command doesn't tell you if the directory you are creating was successfully created. If no errors appear then you can assume it has.

If you want to get more verbose output so that you know what has been created use the following switch.

mkdir -v <foldername>

The output will be along the lines of mkdir: created directory /path/to/directoryname.

Using "mkdir" In A Shell Script

Sometimes you will want to use the "mkdir" command as part of a shell script. For example, let's look at a script that accepts a path. When the script is executed it will create the folder and add a single text file called "hello".

#!/bin/bash

mkdir $@

cd $@

touch hello

The first line should be included in every script that you write and is used to show that this is indeed a BASH script.

The "mkdir" command is used to create a folder. The "$@" (also known as input parameters) at the end of the 2nd and 3rd line is replaced with the value you specify when running the script.

The "cd" command changes into the directory you specify and finally the touch command creates an empty file called "hello".

You can try the script out for yourself. To do so follow these instructions:

  1. Open a terminal window (press Alt and T should do it)
  2. Enter nano createhellodirectory.sh
  3. Type in the commands above into the editor
  4. Save the file by pressing CTRL and O at the same time
  5. Exit the file by pressing CTRL and X at the same time
  6. Change the permissions by typing chmod +x createhellodirectory.sh
  7. Run the script by typing ./createhellodirectory.sh test

When you run the script a directory called "test" will be created and if you change to that directory (cd test) and run a directory listing (ls), you will see a single file called "hello".

So far so good but now try running step 7 again.

An error will appear stating that the folder already exists.

There are various things we can do to improve the script. For instance, if the folder already exists we don't particularly care as long as it exists.

#!/bin/bash

mkdir -p $@

cd $@

touch hello

If you specify the -p as part of the mkdir command then it will not error if the folder already exists but if it doesn't exist it will create it.

As is happens the touch command will create a file if it doesn't exist but if it does exist it simply amends the last accessed date and time.

Imagine the touch statement was replaced with an echo statement which writes text to a file as follows:

#!/bin/bash

mkdir -p $@

cd $@

echo "hello" >> hello

If you run the command "./createhellodirectory.sh test" again and again the effect will be that the file called "hello" in the test directory will grow bigger and bigger with more and more lines with the word "hello" in it.

Now, this may or may not be as intended but let's say for now that this isn't the desired action. You can write a test to make sure the directory doesn't exist before you run the echo command as follows.

#!/bin/bash

mkdir $@ 2>/dev/null;

if [ $? -eq 0 ] ; then

cd $@

echo "hello" >> hello

exit

fi

The above script is my preferred method for handling the creation of folders. The mkdir command creates the folder which is passed as an input parameter but any error output is sent to /dev/null (which essentially means nowhere).

The third line checks the output status of the previous command which is the "mkdir" statement and if it succeeded it will perform the statements until the "fi" statement is reached.

This means you can create the folder and perform all the things you want if the command is successful. If you want to do something else if the command wasn't successful then you can simply enter an else statement as follows:

#!/bin/bash

mkdir $@ 2>/dev/null;

if [ $? -eq 0 ] ; then
cd $@
echo "hello" >> hello
exit
else
cd $@
echo "hello" > hello
exit
fi

In the above script if the mkdir statement works then the echo statement sends the word "hello" to the end of the file called "hello" whereas if it doesn't exist a new file will be created called "hello" with the word "hello" in it.

This example isn't particularly practical because you could achieve the same results simply by always running the echo "hello" > hello line. The point of the example is to show that you can run the "mkdir" command, hide the error output, check the status of the command to see if it was successful or not and then perform one set of commands if the "mkdir" command was successful and another set of commands if it wasn't.