Practical mkdir Command Examples in Linux

Practical mkdir Command Examples in Linux

The mkdir command in Linux is one of the most basic and widely used commands for file management. It stands for “make directory,” and as the name suggests, it is used to create new directories or folders within the filesystem. Understanding how to effectively use the mkdir command is essential for anyone working with Linux, whether you are a system administrator, developer, or just a casual user. This article will explore practical examples and variations of the mkdir command, helping you make the most of this powerful tool.

Basic Usage of mkdir Command

The most straightforward use of the mkdir command is to create a single directory. The syntax is as follows:

mkdir directory_name 

Example 1: Creating a Single Directory

Let’s create a directory called project:

mkdir project 

After running this command, a new directory named project will be created in the current working directory. You can verify its creation by listing the contents of the directory:

ls 

You should see project listed among other files and directories.

Example 2: Creating Multiple Directories at Once

You can also create multiple directories in a single command. The syntax is:

mkdir directory_name1 directory_name2 directory_name3 

For example, to create three directories named dir1, dir2, and dir3, you would use:

mkdir dir1 dir2 dir3 

This command creates all three directories simultaneously, saving time and effort.

Example 3: Creating a Directory with a Specific Path

If you want to create a directory in a specific location, provide the full path:

mkdir /path/to/directory_name 

For instance, to create a directory called data in /home/user/documents, the command is:

mkdir /home/user/documents/data 

This command creates the data directory within the documents directory.

Using the -p Option to Create Parent Directories

Sometimes, you may need to create a directory and its parent directories simultaneously. The -p option is handy in such cases. Without the -p option, mkdir will return an error if the parent directory does not exist. However, with -p, mkdir creates all necessary parent directories.

Example 4: Creating Nested Directories with the -p Option

Suppose you need to create a directory structure like /home/user/documents/reports/2024. If documents or reports does not exist, you can still create this structure with one command:

mkdir -p /home/user/documents/reports/2024 

The -p option ensures that documents and reports are created if they do not already exist.

Example 5: Creating Multiple Nested Directories

You can also use the -p option to create multiple nested directories in one command. For example, to create dirA/dirB/dirC and dirX/dirY/dirZ, you can use:

mkdir -p dirA/dirB/dirC dirX/dirY/dirZ 

This command creates all specified directories in a single execution.

Handling Errors and Permissions

When using the mkdir command, you might encounter errors related to permissions or existing directories. Understanding how to handle these errors is crucial for smooth operation.

Example 6: Handling Existing Directories

If you try to create a directory that already exists, mkdir will return an error:

mkdir project 

If project already exists, the command will display:

arduinomkdir: cannot create directory ‘project’: File exists 

To avoid this error, check for the directory’s existence before creating it. Alternatively, you can ignore the error using the -p option:

mkdir -p project 

With -p, mkdir will not return an error if the directory already exists.

Example 7: Handling Permissions Issues

If you try to create a directory in a location where you do not have write permissions, mkdir will return a permissions error:

mkdir /root/newdir 

For non-root users, this command will fail because they typically do not have write permissions in the /root directory. The error will be:

mkdir: cannot create directory ‘/root/newdir’: Permission denied 

To create a directory in a restricted area, use sudo:

sudo mkdir /root/newdir 

This command elevates your privileges, allowing you to create the directory.

Advanced mkdir Command Examples

Now that we’ve covered the basics, let’s look at some advanced use cases for the mkdir command.

Example 8: Creating Directories with Custom Permissions

By default, directories are created with the system’s default permissions, usually 775 or 755. You can set custom permissions at the time of creation using the -m option.

mkdir -m 700 secure_dir 

This command creates secure_dir with 700 permissions, meaning only the owner has read, write, and execute permissions.

Example 9: Creating Directories Based on Variables

You can use variables within your mkdir commands to automate directory creation based on dynamic input. Suppose you want to create a directory with the current date as its name:

mkdir $(date +%Y-%m-%d) 

This command creates a directory with the current date, like 2024-08-24. It’s handy for organizing files by date.

Example 10: Using mkdir in Scripts

The mkdir command is often used in shell scripts to automate tasks. Here’s a simple example of a script that creates a series of directories for a new project:

#!/bin/ # Script to create project directories mkdir -p ~/projects/new_project/{docs,src,bin,tests} 

This script creates a new_project directory with subdirectories docs, src, bin, and tests. The {} braces expand to create multiple directories efficiently.

Example 11: Creating a Directory and Changing Into It

Sometimes, you may want to create a directory and immediately move into it. While this requires two commands, you can streamline it:

mkdir project && cd project 

The && operator ensures you only change into the directory if mkdir is successful.

Example 12: Combining mkdir with Other Commands

You can combine mkdir with other Linux commands using pipes and logical operators. For instance, you might want to create a directory and list its contents immediately:

mkdir newdir && ls -l newdir 

This command creates newdir and then lists its contents in long format. Since the directory is new, the list will be empty, but it confirms the directory’s creation.

Example 13: Creating Directories from a List

If you have a list of directory names stored in a file, you can create all of them in one go. Assume you have a file dir_list.txt with the following contents:

dirA dirB dirC 

Use the following command to create each directory listed in the file:

xargs mkdir < dir_list.txt 

The xargs command reads each line of the file and passes it to mkdir, creating each directory.

Conclusion

The mkdir command is an essential tool in Linux for creating directories. It is straightforward yet powerful, with options that allow you to perform complex directory creation tasks efficiently. From basic directory creation to advanced usage in scripts and automation, mastering the mkdir command will make your Linux experience smoother and more productive.

Whether you’re organizing files, setting up a new project, or automating tasks, mkdir is the go-to command for managing directories. With the examples provided in this article, you should be well-equipped to use mkdir in various practical scenarios.

Now that you have a solid understanding of the mkdir command, try applying these examples to your own projects and see how they can enhance your workflow.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: