How to create a directory in your home directory in Linux

Share your love

Whether you’re using Linux on a desktop machine or as a hosting server, you must create directories and subdirectories under the home directory at any time.

Creating directories inside the home directory is very easy as you will have to use the mkdir command on the Terminal. The mkdir command makes it easy to create directories without taking too many unnecessary graphical steps.

You type a few lines, and you have a new directory.

But, before we start creating directories, let’s review why one would be driven towards creating new directories inside the home folder.

Why would you create a directory in your home folder

You will need to create new directories in your home folder when deploying various web applications on a Linux instance or shared hosting account.

Moreover, you will need to create subdirectories in your home folder for easier directory and file organization.

Deploying apps

When deploying your web applications on a Linux instance or using CPanel, you may consider keeping each web application’s assets in a separate directory.

For that reason, web application 1 will have a separate directory that is different from the directory used by web application 2.

So, in your home directory, you will have two subdirectories, webapp1_directory and webapp2_directory.

A subdirectory is a folder that is within another directory. Another name for a subdirectory is a subfolder.

As an example, if my_dir directory lives inside the Downloads folder, the my_dir folder is a subdirectory.

Better file organization

You would also create new subdirectories inside the home directory to achieve better directory and file organization.

One major reason to achieve good file organization is that it is easier to find directories associated with each web application on the Linux file system.

Moreover, you may want to keep highly associative or similar files within a directory.

So, instead of having /home/username/Downloads/Videos, you would want to keep all your video files in their directory, /home/username/Videos.

The advantage is that all video files can live inside the /home/username/Videos/ and nowhere else within your Linux file system.

Directories containing the Movies, Tv Shows, Documentaries, normal videos, e.t.c, files would have their directory inside the Videos directory.

Your files will have a better organization, making finding a resource faster and easier.

Now that you understand the reasons behind creating new directories inside the home folder let’s see how we can create new directories.

How to make a directory in Linux using the Terminal

Making a directory in Linux using the Terminal is very easy. You need to open the Terminal, navigate to the location you want to create the directory, and use the mkdir command to create the directory.

Here’s how to use mkdir to create a directory in Linux.

Open a new Linux Terminal window by pressing CTRL + ALT + T or pressing the superkey (the key on the keyboard with the Windows logo) and typing ‘terminal.’ Open the app that appears in the search results.

You should see a similar window like the one below:

Linux Terminal window

Type pwd and press Enter to confirm that you are inside the home directory.

You should see a text displaying the username of the user currently logged in.

Using pwd on Linux to display the current directory

To create a new directory called my_dir, type the following and hit Enter:

mkdir my_dir

You should not see anything if the command runs successfully. That means the mkdir command has successfully created a new directory called my_dir.

If you list the contents of the directory you are navigated into, you should see the directory you created.

ls

How to create a directory within your home directory in Linux

To create a new directory, you have to navigate into the home folder or specify a full path to the home directory when creating the directory using the mkdir command.

The home directory is a directory on its own.

Thus, you must create a subdirectory that will reside within the /home/username/ directory.

Here are the two ways of creating a directory using the mkdir command.

Navigating into the home directory and creating the directory

With this method, you must navigate into the home directory and create the directory inside the current working directory.

The current working directory is the directory that you’re currently navigated into in the Terminal. cd command is used for navigating between various directories in the Linux file system.

So, there are two commands you may use to navigate into the Linux home directory.

Command 1:

cd ~/

Command 2:

cd /home/username/

After navigating into the home directory, create a new one using the mkdir command. Let’s say you want to have a Courses directory within the home folder; then, you would type:

mkdir Courses

The syntax is mkdir directory_name.

Specifying the full path to the home directory to the mkdir command

The second approach to creating a new directory in the home directory is to specify the full path and the directory name you want to create in the mkdir command.

Here’s how to create a new directory in the home directory of your Linux system by using a full path with the mkdir command:

Approach 1:

mkdir ~/my_new_directory

Approach 2:

mkdir /home/username/my_new_directory

And that is how you create a subdirectory in the home directory of a Linux file system.

Let’s say you make a mistake naming your new directory in the home folder; how do you remove it?

How to remove a directory in Linux

Use rmdir to remove a directory created in a Linux file system. Alternatively, you may use the rm -d to remove an empty directory and rm -r to remove directories with files and subdirectories within them.

Use the following command to remove an empty directory using the Terminal

rmdir ~/my_dir

OR

rm -d my_dir

Use the following command to remove a non-empty directory using the Terminal.

rm -r my_dir

The -r option used with the rm command is used to delete each file and directory recursively.

Common flags used with the mkdir command

When using the mkdir, there are some flags you may use to create a directory with additional information or behaviors.

mkdir flags define various properties of the directories created with the mkdir command.

The most common flags are -m used to specify permissions of a directory and -p for forcing the creation of non-existent directories.

mkdir -m (used to specify permissions)

Users have read, write, and execute permissions to a directory.

Therefore, one may use these permissions to allow or restrict a user from reading the files inside the directory, creating new files or folders, or executing binary files within the folder.

Here is an example of a command to create a directory and give the read, write, and execute permissions.

mkdir -m 777 ~/new_dir

mkdir -p (used to create intermediate path name directories)

Another flag used with the mkdir command is -p, which is used to force the creation of a directory if it does not exist.

For example, if you create a new directory on a parent directory that does not exist, the mkdir error will through a “directory does not exist” error.

Try running this code:

mkdir /home/ghghgjgkkgg/new_dir

mkdir will throw an error indicating that you cannot create the directory because the parent directory is not yet created.

To bypass the error, you may use the -p flag to force the creation of a directory that does not exist.

Try running the following code, which should run successfully despite not creating the directories.

mkdir -p ~/Desktop/new_dir_that_does_not_exist/another_new_dir

Common errors you may face when creating directories using the mkdir command

mkdir: cannot create directory ‘’: No such file or directory

The “cannot create directory: No such file or directory” occurs when you have not yet created the parent directory that the subdirectory will exist.

Solution:

The solution to the mkdir: cannot create directory ‘’: No such file or directory is to use the -p flag to force the creation of the parent directory if it does not exist.

Another approach to solving the mkdir: cannot create directory ‘’: No such file or directory error is to create the parent directory first and then create the subdirectory.

mkdir: cannot create directory ‘’: Permission denied

The mkdir: cannot create directory ‘’: Permission denied error occurs when you do not have the right permissions to create a new directory within the directory you are currently navigated into.

Solution:

A solution to the mkdir: cannot create directory ‘’: Permission denied error is to use the sudo command before the mkdir command to elevate the permissions.

A sudo account has all the administrative rights and should create the directory when the permissions to create a directory are not allowed for the regular user.

Here is an example of a solution using the superuser privileges:

sudo mkdir ~/new_dir

Using mkdir on a Linux hosting server example

Most web hosting services, such as Amazon Web Services, will have the default home directory under the username. So, if your username is ubuntu, the home directory lives under the /home/ubuntu/ directory.

On AWS, the default home directory you will have after creating a new Ubuntu Linux instance will be /home/ubuntu.

On /home/ubuntu, you may create various directories for applications hosted using the Ubuntu server.

Let’s say you have three different Django web applications that will use the same Ubuntu server; then, you would create three different directories for each web application.

To do that,

Access the ubuntu server using SSH or access a secure connection using the Terminal found on cPanel and start typing the following commands:

pwd

pwd command is used to print the current directory you’re navigated in. pwd means print working directory.

We’re using the pwd command to verify that we’re currently logged in the home directory of our Ubuntu server.

You should see the following output on the Ubuntu Terminal instance:

/home/ubuntu

That confirms that we’re in the home directory.

Create three new directories for each web application you will host on the Ubuntu server instance.

For example, I may have a blog application, portfolio application, and maybe a client/friend application.

So, I will have these directories in my home directory of the Ubuntu server instance: DjangoBlog, myPortfolio, and JohnDoePersonalSite.

To create these directories, you should type the following:

mkdir DjangoBlog

mkdir myPortfolio

mkdir JohnDoePersonalSite

The commands above create three new directories inside the home folder of our Linux server.

The same commands and steps work similarly in a desktop environment.

That’s it for this tutorial.

I hope you are now comfortable creating directories and subdirectories using the Terminal for your web hosting server instances and Linux desktop environments.

Happy make-daaa-ing!

Share your love
Badi
Badi

Badi here, creator of ngangasn.com— A website dedicated to providing helpful information and how-to's of web development and hosting. Inspired by a fascination to write the most efficient code to make a computer laugh, & humans, Steve has a passion for organizing characters to create code and informative content.
What makes me happy?
Well, nothing like the feeling of finally figuring out that one pesky bug that's been driving me crazy.

Leave a Reply

Your email address will not be published. Required fields are marked *