Linux mkdir command

Linux mkdir command

Linux mkdir Command – Create Directories

The mkdir (make directory) command in Linux is used to create new directories within the filesystem. It is a fundamental tool for organizing files and structuring the filesystem.

Syntax of mkdir

mkdir [OPTIONS] DIRECTORY_NAME
  • OPTIONS → Flags that modify the behavior of mkdir.
  • DIRECTORY_NAME → The name of the directory you want to create.

Common Usage of mkdir

1. Create a Single Directory

To create a single directory, simply type:

mkdir directory_name

Example:

mkdir Documents

This creates a directory named Documents in the current working directory.

2. Create Multiple Directories

You can create multiple directories at once by specifying their names:

mkdir directory1 directory2 directory3

Example:

mkdir folder1 folder2 folder3

This creates folder1, folder2, and folder3 in the current directory.

3. Create Parent Directories (-p)

If you want to create a directory and its parent directories (if they don't exist), use the -p option. This allows you to create an entire path of directories in one go:

mkdir -p /path/to/parent/directory

Example:

mkdir -p /home/user/projects/2025

If /home/user/projects doesn't exist, it will be created along with the 2025 directory.

4. Set Directory Permissions (-m)

You can specify the directory's permissions while creating it using the -m option (for mode):

mkdir -m 755 directory_name

Example:

mkdir -m 700 secure_folder

This creates the directory secure_folder with read, write, and execute permissions for the owner only (700).

5. Create a Directory with Verbose Output (-v)

To see the details of what mkdir is doing, you can use the -v option for verbose output:

mkdir -v directory_name

Example:

mkdir -v Documents

Output:

mkdir: created directory 'Documents'

This will print a message confirming the creation of the directory.

6. Create a Directory with a Specific Parent Directory (--parents)

This is similar to the -p option and can be used when creating a directory structure, ensuring that the parent directories are created if needed.

mkdir --parents /path/to/new/directory

Example:

mkdir --parents /home/user/projects/2025/January

If /home/user/projects/2025 doesn't exist, it will be created.

Examples

1. Create a Single Directory

mkdir my_folder

This creates a directory named my_folder in the current working directory.

2. Create Multiple Directories

mkdir dir1 dir2 dir3

This creates dir1, dir2, and dir3 in the current directory.

3. Create Parent Directories

mkdir -p /home/user/backup/2025

If the directories /home/user/backup don't exist, they will be created along with 2025.

4. Create Directory with Specific Permissions

mkdir -m 755 public_folder

This creates the public_folder directory with read, write, and execute permissions for the owner and read and execute permissions for others.

5. Create Directory with Verbose Output

mkdir -v Downloads

Output:

mkdir: created directory 'Downloads'

Error Handling with mkdir

  • Directory Already Exists: If the directory already exists, you'll see an error:

    mkdir: cannot create directory 'directory_name': File exists

    To avoid this, use the -p option, which will silently skip existing directories.

  • Permission Denied: If you do not have sufficient permissions to create the directory, you will see a permission denied error:

    mkdir: cannot create directory 'directory_name': Permission denied

    You may need to use sudo to create directories in restricted locations.

Conclusion

The mkdir command is a simple yet powerful tool for creating directories in Linux. Whether you're creating a single directory, a nested structure, or setting specific permissions, mkdir provides several options for customizing the creation of directories.

Would you like additional details or SEO optimization? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close