Making a new directory in Linux requires the mkdir command followed by the name you want for the folder. If you are learning how to create directory in linux, this is the first command you need to master. It is simple, fast, and works on almost every Linux distribution.
Directories help you organize files, keep projects separate, and make your system cleaner. Whether you are a beginner or an experienced user, creating folders is a daily task. This guide covers everything from basic commands to advanced options.
Understanding The Mkdir Command
The mkdir command stands for “make directory.” It is built into Linux and requires no extra installation. You just type mkdir followed by the directory name, and the system creates it in your current location.
For example, to create a folder called “projects,” you run:
mkdir projects
That is it. The folder appears instantly. You can verify it by running ls to list the contents of your current directory.
Basic Syntax And Options
The full syntax for mkdir is:
mkdir [options] directory_name
Common options include:
-p: Create parent directories if they do not exist-v: Show a message for each directory created (verbose)-m: Set permissions for the new directory
These options make the command more flexible. For instance, -p is extremely useful when you need to create nested folders.
How To Create Directory In Linux
Now we get into the core of this guide. The process is straightforward, but there are several ways to do it depending on your needs. Below are the most common methods.
Creating A Single Directory
To create one directory, use:
mkdir myfolder
This creates a folder named “myfolder” in your current working directory. If you want it in a specific location, provide the full path:
mkdir /home/username/Documents/myfolder
Always check that the parent directory exists. If it does not, the command will fail with an error like “cannot create directory: No such file or directory.”
Creating Multiple Directories At Once
You can create several directories with one command by listing their names separated by spaces:
mkdir folder1 folder2 folder3
This creates three separate folders in the current directory. It saves time when you need multiple folders for a project.
Creating Nested Directories With -P
The -p option is a lifesaver. It creates parent directories automatically. For example:
mkdir -p projects/2025/reports
If “projects” or “2025” do not exist, mkdir creates them along with “reports.” Without -p, you would get an error. This is perfect for setting up deep folder structures.
Setting Permissions During Creation
Use the -m option to set file permissions right away. For example, to create a directory with read, write, and execute permissions for everyone:
mkdir -m 777 public_folder
This is handy for shared directories. You can also use symbolic permissions like u=rwx,g=rx,o=rx.
Verbose Output With -V
If you want confirmation that the directory was created, add -v:
mkdir -v newfolder
The output will say something like “mkdir: created directory ‘newfolder’.” This helps when you are running scripts or need to track actions.
Practical Examples For Everyday Use
Let us look at real-world scenarios where you will use these commands.
Setting Up A Project Structure
Imagine you are starting a new coding project. You need folders for source code, tests, and documentation. Run:
mkdir -p myproject/src myproject/tests myproject/docs
This creates the main project folder with three subfolders inside. You can also use a single command with -p to build the entire tree.
Creating Directories With Spaces In Names
Directory names with spaces require quotes or escape characters. For example:
mkdir "My Documents"
Or:
mkdir My\ Documents
Both work. Avoid spaces if possible, as they can cause issues in scripts. Use underscores or hyphens instead.
Creating Directories In Other Locations
To create a folder in a different directory without changing your current location, use the full path:
mkdir /tmp/backup
You can also combine this with options:
mkdir -p /var/log/myapp/2025
Common Errors And How To Fix Them
Even simple commands can fail. Here are the most frequent issues.
Directory Already Exists
If you try to create a directory that already exists, mkdir returns an error: “cannot create directory: File exists.” To avoid this, check first with ls or use -p, which suppresses the error for existing directories.
Permission Denied
You need write permission in the parent directory. If you get “Permission denied,” use sudo:
sudo mkdir /root/newfolder
Be careful with sudo. Only use it when necessary.
Invalid Characters In Name
Avoid characters like /, \0, or : in directory names. Stick to letters, numbers, underscores, and hyphens. Linux is case-sensitive, so “Folder” and “folder” are different.
Advanced Techniques And Tips
Once you master the basics, these advanced methods will boost your productivity.
Using Brace Expansion
Bash brace expansion lets you create multiple directories with patterns. For example:
mkdir {jan,feb,mar}_reports
This creates “jan_reports,” “feb_reports,” and “mar_reports.” You can also combine with numbers:
mkdir project{1..5}
This creates project1 through project5.
Creating Directories From A File List
If you have a list of directory names in a text file, use a loop:
while read dir; do mkdir "$dir"; done < directories.txt
Each line in the file becomes a new directory. This is great for bulk operations.
Using Mkdir With Find Or Xargs
Combine mkdir with other commands for automation. For instance, to create directories based on file names:
ls *.txt | sed 's/\.txt$//' | xargs mkdir
This creates a folder for each text file (without the extension).
Comparing Mkdir With Other Methods
While mkdir is the standard, there are other ways to create directories.
Using File Manager GUI
Most Linux desktops have a file manager like Nautilus or Dolphin. Right-click and select "New Folder." This is easier for beginners but slower for bulk tasks.
Using Install Command
The install command can create directories with specific permissions:
install -d -m 755 mydir
It works like mkdir -p but sets permissions automatically.
Using Touch And Rmdir
You cannot create a directory with touch (it creates files). Use rmdir to remove empty directories. For non-empty ones, use rm -r.
Best Practices For Directory Management
Follow these tips to keep your system organized.
- Use descriptive names: "project_2025" is better than "newfolder"
- Avoid spaces in names for scripting compatibility
- Set appropriate permissions with
-mfor shared folders - Use
-pwhen creating nested structures to avoid errors - Check your current directory with
pwdbefore running mkdir
Frequently Asked Questions
What Is The Command To Create A Directory In Linux?
The command is mkdir directory_name. For nested directories, add -p.
How Do I Create Multiple Directories At Once?
List them separated by spaces: mkdir dir1 dir2 dir3. Or use brace expansion like mkdir {a,b,c}.
Can I Create A Directory With Specific Permissions?
Yes, use mkdir -m 755 mydir to set permissions during creation.
What Does Mkdir -P Do?
It creates parent directories if they do not exist. For example, mkdir -p a/b/c creates a, b, and c.
Why Do I Get "Permission Denied" When Creating A Directory?
You lack write permission in the parent directory. Use sudo or change to a directory where you have write access.
Conclusion
Creating directories in Linux is a fundamental skill. The mkdir command is simple yet powerful, with options for nested folders, permissions, and bulk creation. Practice with the examples above, and you will master it quickly.
Remember to use -p for nested structures, -v for verbose output, and -m for permissions. Avoid common errors by checking your current directory and permissions. With these tools, you can organize your files efficiently.
Now go ahead and create some directories. The command line is your friend, and every folder you make brings order to your system.