A single mkdir command with the right flags handles folder creation in any Linux environment. If you’re wondering how to create folder linux, the process is straightforward once you understand the core command and its options. This guide walks you through every method, from basic single folders to complex nested directory trees.
You don’t need to be a command-line expert to manage folders in Linux. The mkdir command is your primary tool, and it works on almost every distribution, including Ubuntu, Fedora, Debian, and CentOS. Let’s start with the simplest case and build up from there.
Understanding The Mkdir Command Basics
The mkdir command stands for “make directory.” It’s a built-in utility that creates folders in the current working directory or at a specified path. The basic syntax is simple: mkdir [options] directory_name.
To create a single folder, open your terminal and type:
mkdir my_new_folder
This creates a folder named “my_new_folder” in your current location. If you want to create it somewhere else, provide the full path:
mkdir /home/username/Documents/projects
One common mistake is forgetting that Linux is case-sensitive. “MyFolder” and “myfolder” are two different directories. Also, avoid spaces in folder names, or wrap the name in quotes if you must use them.
How To Create Folder Linux With Multiple Directories
Creating a single folder is easy, but what if you need a whole tree of directories? The -p flag (parents) is your best friend. It creates parent directories as needed, without throwing an error if they already exist.
For example, to create a nested structure like projects/2024/reports, you’d run:
mkdir -p projects/2024/reports
This single command creates all three folders at once. Without -p, you’d get an error because “projects” doesn’t exist yet. The -p flag also supresses errors if the folder already exists, making it safe to use in scripts.
Here’s a practical example. Say you’re setting up a website structure:
mkdir -p website/css website/js website/images website/fonts
This creates five separate folders under “website” in one line. You can list multiple paths separated by spaces. The -p flag ensures each path’s parent directories are created automatically.
Creating Folders With Specific Permissions
Sometimes you need a folder with specific access rights from the start. The -m flag (mode) sets permissions during creation. For instance, to create a shared folder readable and writable by everyone:
mkdir -m 777 public_shared
This sets full permissions (read, write, execute) for the owner, group, and others. Be careful with 777—it’s often too permissive. A more common use is creating a private folder for yourself:
mkdir -m 700 private_data
This gives only you full access. The mode works the same as the chmod command. You can combine flags too:
mkdir -p -m 755 secure/projects/client_data
This creates the nested structure and sets permissions on the final “client_data” folder. Note that permissions only apply to the last directory in the path when using -p.
Creating Folders With Spaces In Names
Folder names with spaces are tricky in Linux. The terminal interprets spaces as separators between arguments. To create a folder like “My Documents,” you have two options.
First, wrap the name in quotes:
mkdir "My Documents"
Second, use a backslash to escape the space:
mkdir My\ Documents
Both methods work, but quotes are easier to read. However, spaces in folder names cause headaches later when navigating or scripting. Consider using underscores or hyphens instead: My_Documents or My-Documents.
If you’re dealing with user input or variable names, always quote them to avoid unexpected behavior. For example, in a script:
folder_name="My Folder"
mkdir "$folder_name"
Without quotes, this would try to create two folders: “My” and “Folder”.
How To Create Folder Linux Using Absolute Vs Relative Paths
Understanding paths is crucial for efficient folder creation. An absolute path starts from the root directory (/), like /home/user/projects. A relative path starts from your current location, like projects or ../backup.
To create a folder using an absolute path:
mkdir /tmp/test_folder
This works regardless of where you are in the filesystem. Using a relative path:
mkdir test_folder
This creates the folder in your current directory. You can check your current location with pwd (print working directory).
The .. notation refers to the parent directory. To create a folder one level up:
mkdir ../backup
This is useful when you’re deep in a directory tree and want to create a sibling folder. Combine this with -p for complex relative paths:
mkdir -p ../../archive/2024
This goes up two levels, then creates “archive/2024” from there.
Creating Multiple Folders At Once
You can create several folders in a single command by listing them after mkdir. This is faster than running multiple commands. For example:
mkdir folder1 folder2 folder3
This creates three folders in your current directory. You can mix paths too:
mkdir project1 project2 /tmp/backup ../old_data
This creates folders in different locations simultaneously. The command processes each argument in order. If one fails (e.g., due to permissions), it continues with the next.
For creating folders with a pattern, like numbered folders, use brace expansion:
mkdir folder_{1..5}
This creates folder_1, folder_2, folder_3, folder_4, and folder_5. Brace expansion works in bash and most modern shells. You can also combine letters:
mkdir {jan,feb,mar}_reports
This creates jan_reports, feb_reports, and mar_reports.
Creating Folders With Dates Or Timestamps
Automating folder creation with dates is common for backups or logs. You can use the date command inside backticks or with $() to generate dynamic names.
For a folder named with today’s date:
mkdir backup_$(date +%Y-%m-%d)
This creates something like “backup_2024-11-15”. You can customize the format. For a timestamp with hours and minutes:
mkdir snapshot_$(date +%Y%m%d_%H%M)
This gives you “snapshot_20241115_1430”. The + symbol starts the format string. Common placeholders include:
%Y– four-digit year%m– two-digit month%d– two-digit day%H– hour (00-23)%M– minute (00-59)%S– second (00-59)
You can combine this with -p for nested dated structures:
mkdir -p logs/$(date +%Y)/$(date +%m)
This creates a “logs” folder with year and month subfolders, like “logs/2024/11”.
How To Create Folder Linux Using A Script
When you need to create many folders programmatically, a bash script is the way to go. Here’s a simple script that reads folder names from a file:
#!/bin/bash
while read folder; do
mkdir -p "$folder"
done < folder_list.txt
Save this as create_folders.sh, make it executable with chmod +x create_folders.sh, and run it. Each line in folder_list.txt becomes a folder path.
For creating folders based on a list of names in a variable:
#!/bin/bash
names=("alpha" "beta" "gamma" "delta")
for name in "${names[@]}"; do
mkdir -p "project/$name"
done
This creates "project/alpha", "project/beta", and so on. The -p flag ensures the "project" parent is created if it doesn't exist.
You can also use a for loop with numbers:
#!/bin/bash
for i in {1..10}; do
mkdir "chapter_$i"
done
This creates chapter_1 through chapter_10. Scripts are powerful for repetitive tasks, but always test them on a small set first.
Handling Errors When Creating Folders
Sometimes mkdir fails. Common reasons include permission issues, existing folders (without -p), or invalid characters in names. You can check the exit status of mkdir using $?:
mkdir myfolder
if [ $? -eq 0 ]; then
echo "Folder created successfully"
else
echo "Failed to create folder"
fi
Or use the || operator to run a command on failure:
mkdir myfolder || echo "Error: Could not create folder"
For scripts, use set -e to exit on any error. This stops the script if a mkdir command fails, preventing cascading issues.
To check if a folder exists before creating it, use -d:
if [ ! -d "myfolder" ]; then
mkdir myfolder
fi
This avoids overwriting or errors. The ! negates the condition, so the command runs only if the folder does not exist.
Creating Folders With Special Characters
Linux allows most characters in folder names, but some cause problems. Avoid / (slash) because it's the path separator. Also avoid null characters. Other characters like ?, *, and [ have special meanings in the shell and must be quoted or escaped.
To create a folder with a hyphen at the start, use -- to signal the end of options:
mkdir -- -temp_folder
Without --, mkdir interprets -temp_folder as an unknown option. Similarly, for a folder named with a dollar sign:
mkdir "\$money"
Or use single quotes to prevent variable expansion:
mkdir '$money'
Single quotes preserve every character literally. Double quotes still expand variables and some escape sequences.
For folders with newlines or tabs in names (not recommended), you'd need to use quoted strings with escape sequences. But seriously, avoid these—they break most tools and scripts.
How To Create Folder Linux In The GUI
Not everyone lives in the terminal. Most Linux desktop environments offer graphical ways to create folders. In file managers like Nautilus (GNOME), Dolphin (KDE), or Thunar (XFCE), you can right-click in a directory and select "New Folder" or "Create Folder."
Keyboard shortcuts vary. In Nautilus, press Ctrl+Shift+N to create a new folder. In Dolphin, it's F10 then select "Create New" > "Folder." These methods are intuitive but slower for bulk creation.
For remote servers or headless systems, the command line is your only option. But on a desktop, mixing GUI and terminal can be efficient. Use the terminal for batch operations and the GUI for occasional single folders.
Creating Folders With File Manager Actions
Some file managers support custom scripts or actions. For example, in Nautilus, you can add a script to create a folder with a timestamp. Place a script in ~/.local/share/nautilus/scripts/ and it appears in the right-click menu.
A simple timestamp script:
#!/bin/bash
mkdir "$(date +%Y-%m-%d_%H-%M-%S)"
Make it executable and it creates a folder named with the current timestamp wherever you right-click. This is handy for organizing screenshots or downloads.
In Dolphin, you can create service menus with .desktop files. These offer similar functionality but are more integrated into the file manager.
Best Practices For Folder Creation
Consistent naming conventions save time and prevent confusion. Use lowercase letters and hyphens or underscores instead of spaces. For example, project-backup or project_backup rather than "Project Backup."
Plan your directory structure before creating folders. A shallow hierarchy is easier to navigate. Avoid nesting more than three or four levels deep unless necessary.
Use meaningful names. "temp" or "stuff" are unhelpful a month later. Include dates or version numbers for project folders. For example, website_v2_2024 is clear and sortable.
Set appropriate permissions from the start. Use -m to avoid later chmod commands. For shared projects, consider 775 (owner and group can write, others read-only). For personal data, 700 is secure.
Document your structure. A simple text file or README in the root folder explains the layout to others (and future you).
Common Mistakes And How To Avoid Them
One frequent error is forgetting the -p flag when creating nested folders. Without it, mkdir fails if any parent directory is missing. Always use -p unless you're certain the parent exists.
Another mistake is using absolute paths when you meant relative. Double-check your current directory with pwd. A typo like /home/user instead of ./home/user can create folders in unexpected places.
Running mkdir without sufficient permissions produces "Permission denied" errors. Use sudo only when necessary, and prefer creating folders in your home directory where you have full control.
Creating folders with the same name as existing files causes an error. Use -p to supress this, but be aware that it won't overwrite anything—it just ignores the duplicate.
Finally, avoid creating folders with names that start with a dot (like .hidden) unless you intend them to be hidden. Hidden folders are ignored by default in file managers and ls without the -a flag.
Frequently Asked Questions
What is the command to create a folder in Linux?
The command is mkdir followed by the folder name. For example, mkdir newfolder creates a folder called "newfolder" in the current directory. Use mkdir -p to create nested folders.
How do I create multiple folders at once in Linux?
List multiple folder names after mkdir, separated by spaces. For example, mkdir folder1 folder2 folder3. Use brace expansion for patterns: mkdir {a,b,c}_data.