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.
— Abhishek
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. You may find it a daunting task if you don't know or remember the correct command.
But fret not. You can create a new folder in the Ubuntu terminal as easily as running this command:
mkdir new_dir
The above command will create a new empty folder named new_dir
.
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
Unlike creating new files, there is only one command for creating new directories.
To create a new empty directory in the current location, you can use the command:
mkdir new_dir
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:
abhi@learnubuntu:~/new_dir$ mkdir dir1 dir2 dir3
abhi@learnubuntu:~/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.
abhi@learnubuntu:~/new_dir$ mkdir dir_{1..9}
abhi@learnubuntu:~/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 👇
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.
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.
abhi@learnubuntu:~$ ls
file2.txt my_dir new_dir
abhi@learnubuntu:~$ ls new_dir/
abhi@learnubuntu:~$ mkdir new_dir/another_new_dir
abhi@learnubuntu:~$ 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".
abhi@learnubuntu:~$ ls
file2.txt my_dir new_dir
abhi@learnubuntu:~$ 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.
abhi@learnubuntu:~$ ls
file2.txt my_dir new_dir
abhi@learnubuntu:~$ mkdir -p new_dir
abhi@learnubuntu:~$ 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.
abhi@learnubuntu:~$ ls
dir1 file2.txt my_dir
abhi@learnubuntu:~$ mkdir -p dir1/dir2/dir3
abhi@learnubuntu:~$ ls dir1/
dir2
abhi@learnubuntu:~$ 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.
Enjoy learning Ubuntu.