Skip to content

How to Create Directory in Linux or Ubuntu: A Complete Guide

Published: at 09:30 AMSuggest Changes

Are you looking to create directories in Linux or Ubuntu? This comprehensive guide will walk you through everything you need to know about making directories using the mkdir command. Whether you’re a beginner or an experienced user, you’ll learn how to efficiently manage directory structures in Linux.

Table of Contents

Open Table of Contents

Basic Directory Creation

The fundamental command for creating a directory in Linux is mkdir (make directory). Here’s how to use it:

mkdir my_directory

Output:

$ ls
my_directory

This creates a single directory in your current location. To verify the directory was created, you can use the ls command.

Creating Multiple Directories

You can create multiple directories simultaneously by listing them after the mkdir command:

mkdir documents photos videos

Output:

$ ls
documents  photos  videos

Creating Nested Directories

To create a directory structure with multiple levels, use the -p (parents) flag:

mkdir -p project/src/components

Output:

$ tree project
project
└── src
    └── components

This command creates the entire directory path even if the parent directories don’t exist.

Setting Directory Permissions

You can create a directory with specific permissions using the -m flag:

mkdir -m 755 secure_folder

Output:

$ ls -l
total 4
drwxr-xr-x 2 user user 4096 Nov 2 10:00 secure_folder

Common Errors and Solutions

Error: Permission Denied

If you see this error:

mkdir: cannot create directory 'test': Permission denied

Solution: Use sudo for administrative privileges:

sudo mkdir test

Error: Directory Exists

When trying to create a directory that already exists:

mkdir: cannot create directory 'documents': File exists

Solution: Use the -p flag to suppress the error:

mkdir -p documents

Useful Tips and Tricks

  1. Create Directory and Change Into It
mkdir new_project && cd new_project
  1. Create Directory with Date
mkdir backup_$(date +%Y%m%d)

Output:

$ ls
backup_20241102
  1. Create Directory with Specific User and Group
sudo mkdir -m 755 shared_folder && sudo chown user:group shared_folder

Best Practices

  1. Use meaningful directory names
  2. Avoid spaces in directory names (use underscores or hyphens)
  3. Always verify directory creation using ls or tree
  4. Use the -p flag when creating nested directories
  5. Consider permissions when creating directories in system locations

Conclusion

Creating directories in Linux is a fundamental skill for any user. The mkdir command provides flexibility and power to create simple to complex directory structures. Remember to use the appropriate flags based on your needs, and always consider permissions when working with system directories.

For more information about directory management in Linux, check out the manual page by running man mkdir in your terminal.



Previous Post
How to Remove Files and Directories in Linux: Master the rm Command
Next Post
How to Navigate the Linux Command Line for Beginners