Create directories/folders in ubuntu linux. In this tutorial, you will learn how to create directories or folders in ubuntu linux using mkdir
command with command line or terminal.
This tutorial guide will help you to create directories in ubuntu linux system using mkdir command and you can also set permissions, create multiple directories (folders) at once, and much more.
Prerequisites
- Linux or UNIX-like system.
- Access to a terminal/command line.
- A user with permissions to create and change directory settings.
How to Create a Folder in Linux Ubuntu 20.04|22.04 Terminal
- Syntax of mkdir Command in Ubuntu Linux
- Create a New Folder/Directory in Ubuntu Linux
- How to Create Multiple Folders/Directories in ubuntu Linux using mkdir command
- How to Make Parent Directories
- Set Permissions When Making a Directory
- How to Verify Directories
Syntax of mkdir Command in Ubuntu Linux
First of all, you need to know the basic command for creating directories in Linux consists of the mkdir
command and the name of the directory. as shown below:
mkdir [option] dir_name
Note that, The mkdir command allows users to create or make new directories in ubuntu linux system.
Create a New Folder/Directory in Ubuntu Linux
To create a directory using the mkdir command in linux with terminal.
See the following example of how to create directory in linux:
mkdir Linux
If the operation is successful, the terminal returns an empty line.
To verify, use ls
.
Note: To create a hidden directory, follow our guide on how to show and create hidden files in Linux.
How to Create Multiple Folders/Directories in ubuntu Linux using mkdir command
If you want to create multiple directory at once command. So, you can use a single mkdir command to create multiple directories at once.
To do so, use the curly brackets {} with mkdir and state the directory names, separated by a comma.
mkdir {test1,test2,test3}
Note that, Do not add any spaces in the curly brackets for the directory names.
How to Make Parent Directories
Building a structure with multiple subdirectories using mkdir
requires adding the -p
option. This makes sure that mkdir
adds any missing parent directories in the process.
For example, if you want to create “mydirtest2” in “mydirtest1” inside the Linux directory (i.e., Linux/mydirtest1/mydirtest2), run the command:
mkdir –p Linux/mydirtest1/mydirtest2
Note that, you can use ls -R
to display the recursive directory tree in linux. And Without the -p
option, the terminal returns an error if one of the directories in the string does not exist.
Set Permissions When Making a Directory
The mkdir
command by default gives rwx permissions for the current user only.
To add read, write, and execute permission for all users, add the -m
option with the user 777 when creating a directory.
To create a directory DirM with rwx permissions:
mkdir –m777 DirM
To list all directories and show the permissions sets: -l
The directory with rwx permissions for all users is highlighted. As you can see on the image above, two other directories by default have rwx permission for the owner, xr for the group and x for other users.
How to Verify Directories
When executing mkdir
commands for creating a directory in linux, there is no feedback for successful operations. To see the details of the mkdir process, append the -v
option to the terminal command.
Let’s create a Details directory inside Dir1 and print the operation status:
mkdir Command Options and Syntax Summary
Option / Syntax | Description |
---|---|
mkdir directory_name | Creates a directory in the current location |
mkdir {dir1,dir2,dir3,dir4} | Creates multiple directories in the current location. Do not use spaces inside {} |
mkdir –p directory/path/newdir | Creates a directory structure with the missing parent directories (if any) |
mkdir –m777 directory_name | Creates a directory and sets full read, write, execute permissions for all users |
mkdir –v directory_name(s) | Creates a directory in the current location |
Conclusion
This guide covered all commands you have learned how to create directories/folders in ubuntu Linux and how to set permission for the directory.
Hi
Good and very useful information about mkdir command. Every option is explained well and easy to understand.