BLOG POSTS
    MangoHost Blog / mkdir – Command used to create directories in Linux
mkdir – Command used to create directories in Linux

mkdir – Command used to create directories in Linux

Linux mkdir Command

The mkdir command in Linux is used to create directories. It is a basic command that is available in all Linux distributions and is used for creating new directories or folders.

What is mkdir?

The mkdir command stands for “make directory”. It is used to create new directories in the file system. Directories are used to organize files and other directories in a hierarchical structure.

Where is mkdir used?

The mkdir command is used in the command line interface of Linux. It can be used in the terminal or in shell scripts to create directories.

Programming Languages Used

The mkdir command is not built using any specific programming language. It is a built-in command in the Linux operating system.

Official Page

You can find more information about the mkdir command on the official Linux man page: https://man7.org/linux/man-pages/man1/mkdir.1.html

Installation

The mkdir command is a part of the coreutils package, which is installed by default on most Linux distributions. Therefore, there is no need to install it separately.

Supported Operating Systems

The mkdir command is supported on all major Linux distributions, including:

  • Ubuntu
  • Debian
  • CentOS
  • Fedora
  • Red Hat Enterprise Linux
  • Arch Linux

Command Examples

Here are some examples of using the mkdir command:

Create a Directory

To create a new directory, simply use the mkdir command followed by the name of the directory you want to create:

mkdir mydirectory

This will create a new directory named “mydirectory” in the current working directory.

Create Nested Directories

You can also create nested directories by specifying the path of the directory you want to create:

mkdir -p mydirectory/subdirectory

This will create a new directory named “mydirectory” and a subdirectory named “subdirectory” inside it.

Create Multiple Directories

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

mkdir dir1 dir2 dir3

This will create three directories named “dir1”, “dir2”, and “dir3” in the current working directory.

Similar Commands

There are several other commands in Linux that serve a similar purpose to the mkdir command:

  • mkdir: Creates directories
  • rmdir: Removes empty directories
  • rm -r: Removes directories and their contents
  • cp -r: Copies directories and their contents
  • mv: Moves or renames directories

Scripts Examples

Here are three examples of using the mkdir command in automation:

Script 1: Create Backup Directories

#!/bin/bash

# Create backup directories
mkdir -p /backup/$(date +%Y-%m-%d)/files
mkdir -p /backup/$(date +%Y-%m-%d)/databases

This script creates backup directories with the current date as the directory name. It creates two subdirectories: “files” and “databases”.

Script 2: Create User Directories

#!/bin/bash

# Read usernames from a file
while read -r username; do
  # Create user directories
  mkdir -p /home/$username/documents
  mkdir -p /home/$username/pictures
done < usernames.txt

This script reads a list of usernames from a file and creates directories for each user in the “/home” directory. It creates two subdirectories: “documents” and “pictures”.

Script 3: Create Project Directories

#!/bin/bash

# Read project names from a file
while read -r project; do
  # Create project directories
  mkdir -p /projects/$project/src
  mkdir -p /projects/$project/docs
done < projects.txt

This script reads a list of project names from a file and creates directories for each project in the “/projects” directory. It creates two subdirectories: “src” and “docs”.

List of Functions and Constants

Function/Constant Description
mkdir Creates directories
rmdir Removes empty directories
rm -r Removes directories and their contents
cp -r Copies directories and their contents
mv Moves or renames directories

Conclusion

The mkdir command is a simple yet powerful tool for creating directories in Linux. It is widely used by system administrators, developers, and automation scripts to organize files and directories. The command is easy to use and provides various options for creating directories in different scenarios. Whether you need to create a single directory or multiple nested directories, the mkdir command is a reliable choice. It is an essential tool in the Linux command line toolkit and is used daily by Linux users around the world.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked