Creating a new directory on a Linux system is a fundamental skill that requires just one simple command. If you’re wondering how to create a folder on Linux, you’ve come to the right place. This guide will walk you through every step, from the basic command to advanced options, so you can manage your files like a pro.
Linux uses the term “directory” instead of “folder,” but they mean the same thing. Whether you’re a beginner or need a refresher, this article covers everything. Let’s start with the basics and build up your knowledge.
How To Create A Folder On Linux
The primary command for creating a directory in Linux is mkdir, which stands for “make directory.” It’s short, powerful, and works on almost every Linux distribution. You’ll use it in the terminal, which is the command-line interface.
To create a single folder, open your terminal and type:
mkdir foldername
Replace “foldername” with whatever you want to call your directory. For example, mkdir myproject creates a folder named “myproject” in your current location. The terminal won’t show a success message, but the folder is there. You can verify it with ls to list files.
That’s the core of it. But there’s more to learn for efficient folder creation.
Creating Multiple Folders At Once
You can create several directories with one command. Just list their names separated by spaces:
mkdir folder1 folder2 folder3
This creates three folders in your current directory. It’s a huge time-saver when you’re setting up a project structure.
If you need nested folders, like a parent directory with child directories inside, use the -p flag. For example:
mkdir -p parent/child/grandchild
This creates “parent,” then “child” inside it, and “grandchild” inside that. Without -p, the command would fail if “parent” didn’t already exist.
Setting Permissions While Creating
Sometimes you want to control who can read, write, or execute the folder. Use the -m flag to set permissions immediately:
mkdir -m 755 myfolder
This creates “myfolder” with permissions that allow the owner full access, while others can only read and execute. Permissions are written in octal format (like 755, 644, etc.). It’s handy for shared directories.
You can combine flags too. For instance, mkdir -p -m 700 secret/data creates a nested structure with restricted permissions.
Common Mistakes And How To Avoid Them
Even simple commands can trip you up. Here are frequent errors beginners make when creating folders on Linux.
Spaces In Folder Names
If your folder name has spaces, like “my project files,” you need to escape the spaces. Otherwise, the command treats each word as a separate folder. Use quotes or a backslash:
mkdir "my project files"
Or:
mkdir my\ project\ files
Both work. The backslash tells Linux to treat the space as part of the name. Without it, you’d create three folders: “my,” “project,” and “files.”
Permission Denied Errors
If you’re trying to create a folder in a protected area, like the root directory, you’ll get a “Permission denied” message. Use sudo to run the command as an administrator:
sudo mkdir /root/newfolder
Be careful with sudo—it gives you full power, so double-check your command. Only use it when necessary.
Folder Already Exists
Trying to create a folder that already exists gives an error. To suppress this, use the -p flag, which won’t complain if the folder is already there:
mkdir -p existingfolder
This is especially useful in scripts where you’re not sure if the directory exists.
Advanced Folder Creation Techniques
Once you’re comfortable with basics, explore these advanced methods. They’ll make you more efficient.
Creating Folders With Dates Or Timestamps
You can automate folder names with the current date. For example, to create a backup folder with today’s date:
mkdir backup-$(date +%Y-%m-%d)
This creates something like “backup-2023-10-05.” The $(...) runs the date command and inserts the result. It’s perfect for logs or daily backups.
Using Brace Expansion
Brace expansion lets you create multiple folders with patterns. For instance:
mkdir project-{a,b,c}
This creates “project-a,” “project-b,” and “project-c.” You can also combine with numbers: mkdir chapter-{1..5} creates five folders. It’s a quick way to generate series.
Creating Folders From A File List
If you have a text file with folder names (one per line), you can create them all at once. Use xargs:
xargs mkdir < folderlist.txt
This reads each line from "folderlist.txt" and creates a directory. It's great for batch operations.
Verifying Your Created Folders
After creating folders, you'll want to confirm they exist. The ls command lists directory contents:
ls
To see detailed info, use ls -l which shows permissions, owner, size, and date. For hidden folders (those starting with a dot), add -a:
ls -la
You can also use tree to see the folder structure visually. Install it with sudo apt install tree (on Debian/Ubuntu) or sudo yum install tree (on RHEL/CentOS). Then run:
tree
It displays a hierarchical view of your directories.
Practical Examples For Everyday Use
Let's apply what you've learned with real-world scenarios.
Setting Up A Project Structure
Suppose you're starting a web project. You might create:
mkdir mywebsitemkdir mywebsite/css mywebsite/js mywebsite/imgmkdir -p mywebsite/docs/archive
This gives you a clean structure. The -p flag ensures "docs" and "archive" are created even if "mywebsite" already exists.
Organizing Downloads
To sort files by type, create folders for each category:
mkdir Documents Pictures Videos Music
Then move files with mv *.pdf Documents/. It keeps your home directory tidy.
Creating Temporary Folders
For quick tests, use mktemp to create a temporary directory with a unique name:
mktemp -d
This creates something like "/tmp/tmp.XXXXXX" and prints the path. It's automatically cleaned on reboot.
Using Graphical File Managers
Not everyone loves the terminal. Most Linux desktops have a file manager (like Nautilus on GNOME, Dolphin on KDE). To create a folder there:
- Open the file manager.
- Navigate to where you want the folder.
- Right-click and select "New Folder" or "Create Folder."
- Type the name and press Enter.
It's the same as on Windows or macOS. The terminal method is faster for bulk operations, but the GUI is fine for one-off tasks.
Scripting Folder Creation
If you create folders often, write a script. Here's a simple bash script that creates a project structure:
#!/bin/bash
mkdir -p "$1"/{src,docs,tests}
echo "Project $1 created"
Save it as "create_project.sh," make it executable with chmod +x create_project.sh, and run it: ./create_project.sh myapp. It creates "myapp" with three subfolders.
You can expand this with more logic, like checking if the folder already exists.
Troubleshooting Tips
Sometimes things go wrong. Here's how to fix common issues.
Command Not Found
If mkdir isn't recognized, your PATH might be broken. Usually, it's in /bin or /usr/bin. Try running /bin/mkdir. If that fails, reinstall coreutils: sudo apt install --reinstall coreutils.
Read-Only File System
You can't create folders on a read-only filesystem. Check with mount | grep " / " to see if your root is read-only. Remount it with write permissions: sudo mount -o remount,rw /.
Disk Full
If the disk is full, you'll get an error. Use df -h to check available space. Delete unnecessary files or move to another drive.
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 myfolder creates a directory called "myfolder" in the current location.
Can I create a folder with spaces in the name?
Yes, use quotes around the name, like mkdir "my folder", or escape spaces with a backslash: mkdir my\ folder.
How do I create a folder in a specific directory?
Provide the full path, such as mkdir /home/user/Documents/newfolder. Or navigate there first with cd and then run mkdir.
What does the -p flag do in mkdir?
The -p flag creates parent directories as needed and doesn't throw an error if the folder already exists. It's useful for nested structures.
How can I create a folder with specific permissions?
Use the -m flag followed by the permission code, like mkdir -m 755 myfolder. This sets read, write, and execute permissions at creation time.
Final Thoughts
Creating folders on Linux is straightforward once you know the mkdir command. Start with simple names, then experiment with flags and scripts. The more you practice, the faster you'll become.
Remember to check your work with ls or tree. And don't be afraid to make mistakes—they're part of learning. Now go ahead and create your first folder on Linux. You've got the tools.
If you run into issues, revisit the troubleshooting section. Linux is powerful, and mastering directory creation is a key step in your journey. Happy organizing!