How to Create a Folder in Ubuntu Command Line

Creating a folder in the Ubuntu command line is quite simple. If you are absolutely new to the terminal, this article will help you.

When you are new to the Linux command line, even the most basic things may look complicated.

Take the example of creating folders in the Ubuntu command line. If you don't know or remember the correct command, you may find it a daunting task.

But fret not. You can create a new folder in the Ubuntu terminal as easy as running this command:

mkdir new_dir

The above command will create a new empty folder named new_dir.

🗒️
Notice the use of the word dir here. In Windows, you are used to calling it a folder but in Linux, folders are called directories. The command mkdir implies "make directory".

Let's take a deeper look into creating new directories in Ubuntu via the terminal.

Create a new directory

To create a new empty directory in the current location, you can use the command:

mkdir new_dir
⚠️
You cannot create a new directory with the same name as another file or directory in the same location. Yes. It applies to file names also, not just folder names.

Create multiple folders

If you want to create more than one folder in Ubuntu, you can do that in a single command like this:

mkdir dir1 dir2 dir3

Here's a sample output:

[email protected]:~/new_dir$ mkdir dir1 dir2 dir3
[email protected]:~/new_dir$ ls
dir1  dir2  dir3

I know you are not prepared for it at this moment but you can use the curly braces expansion to make multiple folders with similar names.

[email protected]:~/new_dir$ mkdir dir_{1..9}
[email protected]:~/new_dir$ ls
dir_1  dir_2  dir_3  dir_4  dir_5  dir_6  dir_7  dir_8  dir_9

It's okay if you are not familiar with this concept. You can learn more about it later.

🎓🐧Recommended for learning Linux 👇

Learn Linux Quickly - Linux Commands for Beginners
Learn Linux Quickly doesn’t assume any prior Linux knowledge, which makes it a perfect fit for beginners. Nevertheless, intermediate and advanced Linux users will still find this book very useful as it goes through a wide range of topics. Learn Linux Quickly will teach you the following topics:Insta…

Create a folder in another location

The above two examples create new directories in the same location as your current one.

If you want to create the folder in some other folder location, you'll have to provide the absolute or relative path to the location. Here's a quick recall.

Aboslute and relative path in Ubuntu

Here's an example. I have a new_dir folder in my current location. I'll create a new folder under this new_dir folder from the same position.

[email protected]:~$ ls
file2.txt  my_dir  new_dir
[email protected]:~$ ls new_dir/
[email protected]:~$ mkdir new_dir/another_new_dir
[email protected]:~$ ls new_dir/
another_new_dir

Created directory only when it doesn't exist already

This is a common problem when you are writing scripts.

If you try to create a folder that already exists, you'll get an error "mkdir: cannot create directory ‘new_dir’: File exists".

[email protected]:~$ ls
file2.txt  my_dir  new_dir
[email protected]:~$ mkdir new_dir
mkdir: cannot create directory ‘new_dir’: File exists

Imagine you are writing a script that will be used on other systems and by other users. And in the script, you have to create a certain folder. If a folder by that name already exists, your script will fail because of the error it shows.

This is where the -p option of mkdir comes into play. With this, you won't see any errors if there already exists a directory of the same name. This way, your script won't fail because the mkdir command doesn't return any errors.

[email protected]:~$ ls
file2.txt  my_dir  new_dir
[email protected]:~$ mkdir -p new_dir
[email protected]:~$ ls
file2.txt  my_dir  new_dir

Create nested directories

Imagine that you have to create a certain directory structure that has a directory inside it.

The -p option of the mkdir command can be used here as well. It creates the parent directory if it doesn't exist already.

mkdir -p dir1/dir2/dir3

Let me share an example.

[email protected]:~$ ls
dir1  file2.txt  my_dir
[email protected]:~$ mkdir -p dir1/dir2/dir3
[email protected]:~$ ls dir1/
dir2
[email protected]:~$ ls dir1/dir2
dir3

Bonus Tip: Create directories with specific permissions

This is a bit advanced topic for you at this stage. I hope you are familiar with the concept of file permissions in Linux.

You can force mkdir to create folders with specific permissions:

mkdir -m 755 new_dir

Conclusion

That's all you need to know about making a folder in the Ubuntu command line. You probably want to learn about deleting folders as well. These are some of the basic stuff you need to know when you are just starting with the command line.

How to Delete (Directories) Folders in Ubuntu Command Line
New to Ubuntu? Learn how to remove folders in Ubuntu via the command line in this beginner’s tutorial.
How to Delete Files in Ubuntu Command Line
New to Ubuntu? Learn how to remove files in Ubuntu via the command line in this beginner’s tutorial.

Enjoy learning Ubuntu.