Organizing files on Linux starts with creating separate directories to store related data. If you’re wondering how to create subdirectory in linux, you’ve come to the right place. This guide will walk you through every method, from simple commands to advanced tricks, so you can keep your system tidy and efficient.
Directories are like folders on your computer. Subdirectories are simply folders inside other folders. Learning to create them is a fundamental skill for any Linux user, whether you’re a beginner or a seasoned admin.
Let’s get started with the most common and powerful way to create subdirectories.
How To Create Subdirectory In Linux
The primary command for creating directories in Linux is mkdir, which stands for “make directory.” It’s simple, fast, and works on almost every Linux distribution. You’ll use it in the terminal, so open your command line interface now.
Basic Usage Of The Mkdir Command
To create a single subdirectory inside your current working directory, type:
mkdir new_folder_name
Replace new_folder_name with whatever you want to call your subdirectory. For example, to create a folder called “projects,” you’d type:
mkdir projects
That’s it. The subdirectory is created right where you are. You can verify it by typing ls to list the contents of your current directory.
Creating Multiple Subdirectories At Once
Need several subdirectories? You don’t have to run the command multiple times. Just list them all after mkdir, separated by spaces:
mkdir folder1 folder2 folder3
This creates three separate subdirectories in one go. It’s a huge time-saver when setting up project structures.
Creating Nested Subdirectories
Sometimes you need a subdirectory inside another subdirectory. For example, you might want projects/2025/reports. The -p flag (parents) lets you create the entire path at once:
mkdir -p projects/2025/reports
This command creates projects, then 2025 inside it, and finally reports inside 2025. Without -p, you’d get an error if any parent directory didn’t exist.
Creating Subdirectories With Spaces In Names
If your directory name has spaces, you need to quote it or escape the spaces. For example:
mkdir "my project files"
Or using a backslash:
mkdir my\ project\ files
Both methods work, but quoting is usually easier to read. Avoid spaces in names if possible, as they can cause issues in scripts.
Creating Subdirectories With Permissions
You can set specific permissions when creating a directory using the -m (mode) flag. For example, to create a directory with read, write, and execute for the owner only:
mkdir -m 700 private_folder
This sets permissions to rwx------, meaning only you can access it. The number 700 is an octal permission code. You can use any valid permission set.
Using Absolute And Relative Paths
When creating subdirectories, you can use either absolute or relative paths. Understanding the difference is crucial for efficient navigation.
Absolute Paths
An absolute path starts from the root directory (/). For example:
mkdir /home/username/Documents/work
This creates a subdirectory called work inside Documents in your home folder, no matter where you are in the file system.
Relative Paths
A relative path is based on your current location. If you’re in /home/username, you can type:
mkdir Documents/work
This does the same thing as the absolute path example, but only works if you’re in the right starting directory. Use . for the current directory and .. for the parent directory.
Creating Subdirectories With A GUI
Not everyone loves the terminal. Most Linux desktop environments let you create subdirectories graphically. Here’s how in common file managers.
In Nautilus (GNOME)
Open the file manager, navigate to where you want the subdirectory, right-click in the empty space, and select “New Folder.” Type the name and press Enter.
In Dolphin (KDE)
Right-click in the folder pane, choose “Create New” then “Folder.” Name it and confirm. You can also use the keyboard shortcut Ctrl+Shift+N.
In Thunar (XFCE)
Right-click and select “Create Folder” or use Ctrl+Shift+N. It’s just as simple.
GUI methods are great for quick tasks, but they’re slower for bulk operations. The terminal wins for efficiency.
Advanced Techniques For Creating Subdirectories
Once you’re comfortable with the basics, you can automate and customize directory creation.
Using Brace Expansion
Brace expansion lets you create multiple directories with similar names. For example:
mkdir project_{a,b,c}
This creates project_a, project_b, and project_c. You can combine it with paths:
mkdir -p docs/{2025,2026}/{reports,summaries}
This creates docs/2025/reports, docs/2025/summaries, docs/2026/reports, and docs/2026/summaries all at once. It’s incredibly powerful.
Creating Directories With A Script
For repeated tasks, write a simple bash script. Create a file called create_dirs.sh with this content:
#!/bin/bash
mkdir -p /path/to/base/{sub1,sub2,sub3}
echo "Directories created."
Make it executable with chmod +x create_dirs.sh and run it with ./create_dirs.sh. This saves time on complex structures.
Using Find And Mkdir Together
You can combine find with mkdir to create directories based on file patterns. For example, to create a subdirectory for each text file in a folder:
for file in *.txt; do mkdir -p "${file%.txt}_folder"; done
This loops through all .txt files and creates a folder named after each one (without the extension). It’s a bit advanced but very useful.
Common Mistakes And How To Avoid Them
Even experienced users make errors. Here are frequent pitfalls and solutions.
Permission Denied Errors
If you try to create a directory in a protected area (like /etc), you’ll get “Permission denied.” Use sudo to run the command with root privileges:
sudo mkdir /etc/new_folder
Be careful with sudo—only use it when necessary.
Directory Already Exists
Running mkdir on an existing directory gives an error. Use the -p flag to suppress this and continue:
mkdir -p existing_folder
This is safe and won’t overwrite anything.
Invalid Characters In Names
Avoid characters like /, \0 (null), and : in directory names. They can break commands. Stick to letters, numbers, underscores, and hyphens.
Forgetting The -P Flag For Nested Paths
If you try mkdir a/b/c and a doesn’t exist, you’ll get an error. Always use -p when creating nested directories.
Practical Examples For Everyday Use
Let’s apply what you’ve learned to real-world scenarios.
Setting Up A Project Structure
Imagine you’re starting a web development project. You might need:
project/project/html/project/css/project/js/project/images/
Run this command from your home directory:
mkdir -p project/{html,css,js,images}
All subdirectories are created instantly.
Organizing Photos By Year And Month
For a photo collection, create a structure like photos/2025/January. Use:
mkdir -p photos/{2024,2025}/{January,February,March}
This creates 6 folders inside two year folders. You can expand it for all months.
Creating Backup Directories
To back up files daily, create dated folders:
mkdir -p backups/$(date +%Y-%m-%d)
This creates a folder like backups/2025-03-15. Run it in a cron job for automated backups.
Checking If A Directory Exists Before Creating
In scripts, you often want to avoid errors by checking first. Use this pattern:
if [ ! -d "my_folder" ]; then
mkdir my_folder
fi
This creates the folder only if it doesn’t already exist. It’s clean and safe.
Creating Directories With Timestamps
For logs or temporary files, add a timestamp to the name:
mkdir "logs_$(date +%s)"
The %s format gives Unix epoch time, ensuring unique names. You can use other date formats too.
Using Mkdir With Other Commands
Combine mkdir with cd to create and enter a directory in one step. For example:
mkdir new_project && cd new_project
The && ensures the second command runs only if the first succeeds. This is a common workflow.
You can also use mkdir inside a cp or mv command to create destination directories:
cp file.txt /path/to/new/dir/ || mkdir -p /path/to/new/dir/ && cp file.txt /path/to/new/dir/
This is a bit messy, so scripts are better for complex tasks.
Understanding Directory Permissions And Ownership
When you create a subdirectory, it inherits the permissions of its parent by default. But you can change that.
Setting Default Permissions With Umask
The umask command sets default permissions for new files and directories. For example, umask 022 gives 755 permissions (rwxr-xr-x) to directories. Check your current umask with umask.
Changing Ownership After Creation
Use chown to change the owner of a directory:
sudo chown username:groupname new_folder
This is useful when creating directories for other users.
Creating Directories On Remote Servers
If you’re working on a remote Linux server via SSH, the same commands apply. Just SSH into the server first:
ssh user@server_ip
mkdir -p /home/user/new_project
You can also create directories remotely without an interactive session using SSH commands:
ssh user@server_ip "mkdir -p /home/user/new_project"
This is handy for automation.
Creating Directories With Special Characters
Sometimes you need names with special characters like $ or !. Escape them with a backslash or use single quotes:
mkdir 'my$pecialFolder'
Single quotes prevent shell expansion. Double quotes allow some expansion, so be careful.
Creating A Directory Structure From A File
You can create directories based on a list in a text file. Suppose dirs.txt contains:
folder1
folder2/subfolder
folder3
Run:
while read line; do mkdir -p "$line"; done < dirs.txt
This reads each line and creates the path. It's great for bulk setups.
Using Mkdir In Cron Jobs
To automate directory creation, add a cron job. Edit your crontab with crontab -e and add:
0 0 * * * mkdir -p /home/user/backups/$(date +\%Y-\%m-\%d)
This creates a dated backup folder every midnight. The percent signs need escaping in cron.
Creating Directories With Extended Attributes
Linux supports extended attributes for directories. Use setfattr after creation:
mkdir my_folder
setfattr -n user.comment -v "Important data" my_folder
This adds metadata without affecting the directory's contents.
Common Questions About Creating Subdirectories
Here are answers to frequent queries.
Can I Create A Subdirectory With A Dot At The Beginning?
Yes, directories starting with a dot (like .hidden) are hidden in Linux. Use mkdir .hidden_folder. They won't show up in ls without the -a flag.
What's The Difference Between Mkdir And Mkdir -P?
mkdir creates a single directory and fails if parents are missing. mkdir -p creates the entire path, including missing parent directories, and doesn't error if the directory already exists.
How Do I Create A Subdirectory In A Different Partition?
You can create directories anywhere you have write permissions. Use the full path, like mkdir /mnt/usb_drive/my_folder. Make sure the partition is mounted first.
Can I Undo A Mkdir Command?
No, mkdir cannot be undone. Use rmdir to remove empty directories or rm -r to remove non-empty ones. Be careful with rm -r—it's permanent.
How Do I Create A Directory With A Specific Date In The Name?
Use command substitution: mkdir "backup_$(date +%F)". This gives a name like backup_2025-03-15. The %F format is YYYY-MM-DD.
Final Tips For Mastering Directory Creation
Practice makes perfect. Start with simple commands and gradually use flags like -p and -m. Learn brace expansion for efficiency. Always double-check your paths to avoid accidental creation in wrong locations.
Remember that the terminal is your friend. While GUI tools are fine, the command line gives you speed and precision. Bookmark this guide for quick reference.
Now you have a solid understanding of how to create subdirectory in linux. Go ahead and organize your files like a pro. Your future self will thank you for the clean structure.