The mkdir command in Linux allows you to create a subdirectory within an existing folder using a simple terminal instruction. If you are wondering how to make a subdirectory in linux, this guide will walk you through every step with clear examples and practical tips.
Creating subdirectories is a fundamental skill for organizing files, managing projects, or setting up server environments. Whether you are a beginner or an experienced user, mastering this command saves time and keeps your system tidy.
What Is A Subdirectory In Linux?
A subdirectory is simply a folder inside another folder. In Linux, everything is treated as a file, and directories are special files that hold other files or directories. Think of it like a tree structure: the root directory is the trunk, and subdirectories are branches.
For example, if you have a folder called “Projects” and you want to organize it by year, you can create subdirectories like “2023” and “2024” inside it. This makes navigation and file management much easier.
How To Make A Subdirectory In Linux
Now let’s get into the core of this article. The primary command for creating subdirectories is mkdir, which stands for “make directory.” Here is the basic syntax:
mkdir [options] directory_name
To create a subdirectory inside an existing directory, you specify the path. For instance, if you are in your home folder and want to create a subdirectory called “Documents/Work,” you would run:
mkdir Documents/Work
But wait—if “Documents” does not exist yet, this command will fail. That is where the -p option comes in handy. Let’s explore all the essential variations.
Basic Subdirectory Creation
To create a single subdirectory inside your current location, just type:
mkdir new_folder
This creates “new_folder” in the directory you are currently in. To verify, use ls to list contents. You should see the new folder appear.
If you want to create a subdirectory inside a specific path, provide the full or relative path:
mkdir /home/username/Documents/backups
This assumes the parent directory “Documents” already exists. If not, you will get an error like “cannot create directory: No such file or directory.”
Creating Multiple Subdirectories At Once
You can create several subdirectories in one command by listing them separated by spaces:
mkdir folder1 folder2 folder3
This creates three subdirectories in the current location. You can also mix paths and names:
mkdir /tmp/test1 /tmp/test2 ./local_folder
This is efficient when setting up project structures or testing environments.
Using The -P Option For Nested Directories
The -p flag is a lifesaver when you need to create a chain of subdirectories. It creates parent directories if they do not exist. For example:
mkdir -p /home/user/Documents/2024/Reports/January
This command creates “Documents,” “2024,” “Reports,” and “January” if any of them are missing. Without -p, it would fail if “Documents” did not exist.
This is extremely useful for scripting or when you are unsure about the directory structure. It also suppresses errors if the directory already exists, making it idempotent.
Setting Permissions With -M Option
You can set specific permissions on the new subdirectory at creation time using the -m flag. For instance:
mkdir -m 755 secure_folder
This creates “secure_folder” with read, write, and execute permissions for the owner, and read and execute for others. The default permissions depend on your umask setting, but -m overrides that.
Common permission values include 755 (standard for directories) and 700 (private). You can also use symbolic modes like u=rwx,g=rx,o=rx.
Creating Subdirectories With Spaces In Names
If your directory name contains spaces, you need to escape them or use quotes. For example:
mkdir "My Project Files"
Or using backslash escaping:
mkdir My\ Project\ Files
Both methods work, but quotes are generally easier to read. Avoid spaces in names when possible to prevent confusion in scripts.
Practical Examples Of Subdirectory Creation
Let’s walk through real-world scenarios where you would use these commands.
Organizing A Web Project
Suppose you are building a website and need a standard structure. Run:
mkdir -p my_website/{css,js,images,fonts,backup}
This creates “my_website” with five subdirectories inside it in one line. The curly braces expand to multiple names, saving you from typing each one.
Setting Up A Backup System
To create dated backup folders, you can combine mkdir with date commands:
mkdir -p backups/$(date +%Y-%m-%d)
This creates a folder like “backups/2025-03-15” with the current date. You can schedule this in a cron job for automated backups.
Creating User Home Directories
System administrators often need to create user directories with specific permissions:
sudo mkdir -m 700 /home/newuser
Then set the owner with chown. This ensures the directory is private to the user.
Common Mistakes And How To Avoid Them
Even experienced users make errors. Here are the most frequent pitfalls.
Forgetting Parent Directories Exist
If you try to create a subdirectory inside a path that does not exist, you get an error. Always use -p when unsure, or verify the parent directory first with ls.
Using Incorrect Paths
Relative paths depend on your current working directory. Use pwd to check where you are. Absolute paths start with / and are always reliable.
Permission Denied Errors
If you try to create a subdirectory in a system folder like /etc without sudo, you will get “Permission denied.” Use sudo only when necessary, and be careful not to change system files accidentally.
Typing Mistakes In Names
Linux is case-sensitive. “MyFolder” and “myfolder” are different. Double-check your spelling, especially when using uppercase letters.
Advanced Techniques For Subdirectory Management
Once you master basic creation, explore these advanced methods.
Using Brace Expansion
Bash brace expansion allows you to create multiple subdirectories with patterns:
mkdir -p {2023,2024,2025}/{Q1,Q2,Q3,Q4}
This creates 12 folders (3 years × 4 quarters) in one command. It is incredibly efficient for project planning.
Creating Directories From A File List
If you have a text file with directory names, you can create them all at once:
xargs mkdir -p < directories.txt
Each line in the file should contain one directory path. This is useful for bulk setups.
Using Find With Mkdir
You can combine find and mkdir to create missing directories. For example, to create subdirectories for each file type:
for ext in jpg png gif; do mkdir -p "images/$ext"; done
This creates "images/jpg," "images/png," and "images/gif" in one loop.
Verifying Your Subdirectories
After creation, always check your work. Use these commands:
ls -lto list contents with detailsls -Rto recursively list all subdirectoriestree(if installed) for a visual tree viewfind . -type dto list all directories
For example, tree my_website shows the entire structure you created. Install tree with sudo apt install tree if needed.
Removing Subdirectories (Safety Note)
While not the focus, knowing how to remove directories is important. Use rmdir for empty directories, or rm -r for non-empty ones. Be extremely careful with rm -rf as it deletes without confirmation.
Always double-check your path before deleting. A misplaced space can cause disaster.
Frequently Asked Questions
What Is The Difference Between Mkdir And Mkdir -P?
mkdir creates a single directory and fails if the parent does not exist. mkdir -p creates all parent directories as needed and does not error if the directory already exists.
Can I Create A Subdirectory With A Dot In The Name?
Yes, Linux allows dots in directory names. For example, mkdir my.folder works fine. Hidden directories start with a dot, like .config.
How Do I Create A Subdirectory In A Different Drive Or Partition?
Use the full path to the mount point. For example, if your USB drive is mounted at /mnt/usb, run mkdir /mnt/usb/my_folder.
What If I Get "File Exists" Error?
This means the directory already exists. Use mkdir -p to suppress the error, or check with ls before creating.
Is There A GUI Way To Create Subdirectories In Linux?
Yes, most file managers like Nautilus or Dolphin allow you to right-click and select "Create Folder." However, the terminal is faster for bulk operations.
Best Practices For Directory Naming
Follow these tips to keep your filesystem organized:
- Use lowercase letters to avoid case-sensitivity issues
- Replace spaces with hyphens or underscores
- Keep names short but descriptive
- Avoid special characters like ! @ # $ %
- Use consistent naming conventions across projects
For example, "project-backup-2025" is better than "Project Backup 2025!" because it is easier to type in commands.
Troubleshooting Common Issues
If a command fails, check these things:
- Are you in the correct directory? Use
pwd. - Do you have write permission? Use
ls -ldon the parent. - Does the path contain typos? Use tab completion.
- Is the filesystem full? Check with
df -h. - Are there hidden characters? Use
cat -Ato inspect.
Most errors are due to simple mistakes. Reading the error message carefully usually points to the solution.
Automating Subdirectory Creation With Scripts
For repetitive tasks, write a bash script. Here is a simple example:
#!/bin/bash
# Create project structure
BASE_DIR="$HOME/projects/$1"
mkdir -p "$BASE_DIR"/{src,docs,tests,config}
echo "Project $1 created at $BASE_DIR"
Save this as create_project.sh, make it executable with chmod +x, and run it with ./create_project.sh myapp. This saves time and ensures consistency.
You can also use loops to create multiple projects at once:
for project in web app api; do
mkdir -p "$HOME/projects/$project"/{src,docs}
done
This creates three project folders each with src and docs subdirectories.
Understanding Directory Permissions
When you create a subdirectory, its permissions are determined by your umask. The default umask is usually 022, which gives 755 permissions (rwxr-xr-x). You can change this temporarily:
umask 077
mkdir private_folder
This creates a folder with 700 permissions (rwx------). Remember to reset umask after if needed.
For shared projects, consider using groups and setting the setgid bit with chmod g+s to maintain group ownership.
Conclusion
Now you know how to make a subdirectory in linux using the mkdir command. From basic single folders to complex nested structures, you have all the tools to organize your files efficiently. Practice with the examples provided, and soon you will create directories without thinking twice.
Remember to use -p for nested paths, -m for permissions, and brace expansion for bulk creation. These techniques will make you more productive on the command line.
If you encounter any issues, refer to the man page with man mkdir for complete documentation. Happy organizing!