Creating a subdirectory in Linux uses the mkdir command followed by the directory name. If you are wondering how to make subdirectory in linux, this guide walks you through every step with clear examples and practical tips.
Directories are the backbone of file organization in Linux. Whether you are a beginner or a seasoned user, knowing how to create subdirectories efficiently saves time and keeps your system tidy.
How To Make Subdirectory In Linux
To create a subdirectory, open your terminal and type mkdir followed by the name you want. For example, mkdir projects makes a folder called projects in your current location.
But there is more to it than that. Let us break down the process step by step, covering everything from basic commands to advanced options.
Understanding The Mkdir Command
The mkdir command stands for “make directory.” It is one of the most fundamental tools in Linux. You use it to create new folders anywhere in your file system.
Here is the basic syntax:
mkdir [options] directory_name
You can create one directory or multiple directories at once. You can also set permissions or create nested folders with a single command.
Basic Usage: Creating A Single Subdirectory
Open your terminal. Navigate to where you want the new folder using the cd command. Then type:
mkdir myfolder
Press Enter. That is it. The folder appears instantly. Check it with ls to confirm.
If you want to create a subdirectory inside an existing folder, specify the full path:
mkdir /home/username/Documents/myfolder
This works even if you are not currently in that directory.
Creating Multiple Subdirectories At Once
You do not need to run mkdir multiple times. Just list all names separated by spaces:
mkdir folder1 folder2 folder3
This creates three separate subdirectories in your current location. It is a huge time saver when setting up project structures.
Creating Nested Subdirectories With -P
What if you need a folder inside a folder inside a folder? Use the -p flag. It creates parent directories automatically if they do not exist.
mkdir -p projects/2025/reports
This command creates projects, then 2025 inside it, and finally reports inside that. No errors if some already exist.
Without -p, you would get an error if projects or 2025 did not already exist. The -p flag is a lifesaver for complex folder trees.
Setting Permissions While Creating Subdirectories
Sometimes you want a subdirectory with specific access rights. Use the -m flag to set permissions right away.
mkdir -m 755 secure_folder
This creates a folder with read, write, and execute for the owner, and read and execute for everyone else. You can use any permission number here.
Check the permissions with ls -l to verify they are correct.
Creating Subdirectories With Spaces In Names
If your folder name has spaces, enclose it in quotes or use a backslash before each space.
mkdir "my project files"
Or:
mkdir my\ project\ files
Both work. The second method escapes the space character. Avoid spaces if you can, as they make command-line work harder.
Using Absolute Vs Relative Paths
You can create subdirectories using absolute paths (starting from root) or relative paths (based on your current location).
Absolute example:
mkdir /tmp/backup
Relative example (if you are in /home/user):
mkdir Documents/backup
Relative paths are shorter and easier when you are already in the right area.
Creating Subdirectories In The Home Directory
Use the tilde (~) to reference your home directory from anywhere.
mkdir ~/newfolder
This creates a folder in your home directory no matter where your terminal is pointing. Very handy for quick access.
Checking If A Subdirectory Already Exists
Before creating a folder, you might want to check if it exists. Use test -d or just run ls.
if [ ! -d "myfolder" ]; then mkdir myfolder; fi
This script creates the folder only if it does not already exist. It prevents accidental overwrites or errors.
Using Mkdir With Variables In Scripts
In shell scripts, you often use variables for folder names. Here is an example:
#!/bin/bash
BACKUP_DIR="backup_$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR/logs"
This creates a dated backup folder with a logs subdirectory. The -p flag ensures the parent folder is created too.
Common Mistakes And How To Avoid Them
- Forgetting the
-pflag when creating nested folders. You get an error if a parent does not exist. - Using spaces without quotes. The command treats each word as a separate folder name.
- Typing
mkdrior other typos. The command ismkdir. - Creating folders with invalid characters like slashes or null bytes.
Practical Examples For Everyday Use
Here are some real-world scenarios where you will use mkdir:
- Setting up a web project:
mkdir -p website/css website/js website/img - Organizing downloads:
mkdir -p ~/Downloads/{images,docs,music} - Creating a backup structure:
mkdir -p /mnt/backup/$(date +%F)/home
These patterns save you from typing many commands manually.
Using Brace Expansion For Multiple Folders
Bash supports brace expansion. It generates multiple names from a pattern.
mkdir {folder1,folder2,folder3}
This creates three folders. You can also nest them:
mkdir -p project/{src,docs,tests}/{2024,2025}
This creates a whole tree of directories with one command. It is powerful and efficient.
Creating Subdirectories With A Specific Owner
If you have superuser access, you can create folders owned by another user using sudo and chown.
sudo mkdir /opt/app
sudo chown username:groupname /opt/app
Or combine them with a single command using install:
sudo install -d -o username -g groupname /opt/app
The install command creates directories and sets ownership in one step.
Verbose Output With -V
Use the -v flag to see what mkdir is doing. It prints a message for each folder created.
mkdir -v newfolder
Output: mkdir: created directory 'newfolder'
This is helpful in scripts or when you want confirmation.
Creating Subdirectories On Different Filesystems
You can create folders on mounted drives, USB sticks, or network shares. Just use the correct path.
mkdir /mnt/usb/backup
Make sure the mount point exists and you have write permissions. Otherwise, you get a permission denied error.
Using Mkdir With Find And Other Commands
Combine mkdir with find to create directories for each file type. For example:
find . -type f -name "*.txt" -exec sh -c 'mkdir -p "${1%.*}"' _ {} \;
This creates a folder for each text file based on its name. It is an advanced technique but very useful.
Creating Subdirectories With Timestamps
Automate folder creation with timestamps using date.
mkdir "backup_$(date +%Y-%m-%d_%H-%M-%S)"
This creates a unique folder every time you run it. Perfect for logs or backups.
Handling Errors Gracefully
If a folder already exists, mkdir throws an error. Use the -p flag to suppress it, or redirect errors to /dev/null.
mkdir myfolder 2>/dev/null || echo "Folder exists"
This keeps your scripts clean and avoids unwanted messages.
Creating Subdirectories With Special Characters
For names with hyphens, underscores, or dots, just type them normally. Avoid starting with a hyphen, as it looks like an option.
mkdir my-folder
mkdir my_folder
mkdir my.folder
All work fine. If you must start with a hyphen, use -- to signal the end of options:
mkdir -- -myfolder
Using Mkdir In Cron Jobs
Automate folder creation with cron. Add a line like this to your crontab:
0 3 * * * mkdir -p /home/user/backup/$(date +\%Y\%m\%d)
This creates a dated backup folder every night at 3 AM. The % signs need escaping in cron.
Creating Subdirectories With Extended Attributes
Some filesystems support extended attributes. You can set them after creating the folder using setfattr.
mkdir myfolder
setfattr -n user.comment -v "Important data" myfolder
This adds metadata to the folder. Not commonly needed but good to know.
Recap: Key Takeaways
- Use
mkdirfor single folders. - Add
-pfor nested folders. - Use
-mto set permissions. - Use quotes for spaces.
- Combine with brace expansion for bulk creation.
Mastering these techniques makes you faster and more efficient on the command line.
Frequently Asked Questions
How do I create a subdirectory in Linux with a single command?
Use mkdir -p parent/child/grandchild to create nested folders all at once. The -p flag handles missing parent directories.
What is the difference between mkdir and mkdir -p?
mkdir creates only the last directory in the path and errors if parents are missing. mkdir -p creates all directories along the path without errors.
Can I create a subdirectory with spaces in its name?
Yes. Enclose the name in quotes: mkdir "my folder". Or escape spaces with backslashes: mkdir my\ folder.
How do I set permissions when creating a subdirectory?
Use the -m flag: mkdir -m 755 myfolder. This sets the permissions immediately upon creation.
What happens if I try to create a subdirectory that already exists?
Without -p, you get an error: “File exists.” With -p, the command succeeds silently and does nothing.
Now you know exactly how to make subdirectory in linux. Practice these commands in your terminal to build confidence. Start with simple folders, then try nested structures and permissions. The more you use mkdir, the more natural it becomes.