The mkdir command in Linux is used to create directories. It is a fundamental command that every Linux user should be familiar with, as it plays a crucial role in file and directory management. The basic syntax of the mkdir command allows you to create one or more directories at a time, set permissions, and even create parent directories if they don’t already exist. Understanding the various options and syntax for mkdir can significantly improve your efficiency when managing files and directories in the command line.
This guide will explain the syntax of the mkdir command, explore its options, and provide practical examples of how to use mkdir in different scenarios on a Linux system.
Syntax of the mkdir Command
In its simplest form, the mkdir command follows this syntax:
mkdir [OPTION]... DIRECTORY...
- mkdir is the command to instruct the system to make a new directory.
- [OPTION] is an optional argument modifying the behavior of the command.
- DIRECTORY is the name of the directory or directories you want to create.
The Options Available with mkdir Command
There are several options you can use with the mkdir command to customize its functionality:
- -m, –mode=MODE: Sets the file mode (permissions) for the new directory, often represented as a numeric string, like ‘755’ or ‘644’.
- -p, –parents: If the specified directories already exist, this option ensures no error is reported. It also allows the creation of parent directories as required.
- -v, –verbose: Provides verbose output, explaining what the mkdir command is doing.
- –help: Displays a help message and exits.
- –version: Outputs version information and exits.
mkdir Command Examples
Creating a Simple Directory with mkdir
The most simple and common use of the mkdir command is to create a new directory:
mkdir new_directory
In this command, new_directory is the name of the directory that you’re creating. After executing this command, a new directory named new_directory will be created in your current location.
Making Multiple Directories with mkdir
The mkdir command can also create multiple directories simultaneously. Here’s how you do it:
mkdir dir1 dir2 dir3
In this command, dir1, dir2, and dir3 are the names of the directories to be created. This command will create these three directories in your current location.
Creating a Directory with Specific Permissions with mkdir
Using the -m or –mode option, you can specify the permissions of the directory at the time of its creation:
mkdir -m 755 new_directory
Here, new_directory is created with permissions set to ‘755’, meaning the owner can read, write, and execute, while the group and others can only read and execute.
Creating Nested Directories with mkdir
The -p or –parents option allows the creation of parent directories as needed. For instance:
mkdir -p dir1/dir2/dir3
This command checks for the existence of dir1 and dir2, and if they do not exist, it creates them. Then, it creates dir3 inside dir2, which is inside dir1.
Using Verbose Output with mkdir
The -v or –verbose option makes mkdir print a message for each directory it creates, which can be useful for troubleshooting or confirmation:
mkdir -v new_directory
This command will create new_directory and then print a message indicating its creation.
Creating a Directory with a Space in the Name
Sometimes, we want to create a directory with a space in its name. We can achieve this by enclosing the directory name in quotes:
mkdir "new directory"
With this command, a new directory named new directory (with a space between ‘new’ and ‘directory’) is created.
Checking Version of mkdir
You can check the version of the mkdir command currently installed on your system. This might be helpful in case of compatibility issues or while troubleshooting. To check the version, you can use:
mkdir --version
This command will display the version information of the mkdir command currently in use on your system.
Displaying Help for mkdir
If you ever need help using the mkdir command or want to quickly check its options, use the –help option:
mkdir --help
This command will output a help message showing the usage and available options of the mkdir command.
Creating Directories from a Text File
Imagine you have a text file with a list of directory names that you want to create. You can do this using the xargs command along with mkdir:
xargs mkdir < dir_list.txt
In this command, dir_list.txt is a text file containing the list of directory names. The command will read this file and create directories with these names.
Creating a Directory with a Full Path
You can specify the full path where you want to create the directory. This is especially helpful when you want to create directories in a location other than the current working directory:
mkdir /path/to/new_directory
Here, new_directory is created in the specified path /path/to/.
Creating a Hidden Directory
In Linux, any file or directory name that starts with a dot (.) is considered hidden. To create a hidden directory, use:
mkdir .hidden_directory
This command will create a hidden directory named .hidden_directory. You can view this directory using ls -a.
Prevent Overwriting of Existing Directories
The -p or –parents option can also be used to prevent the mkdir command from overwriting existing directories:
mkdir -p existing_directory
With this command, if existing_directory already exists, it will not be overwritten, preventing any potential data loss.
Creating a Directory and Setting Group ID
The g
option allows you to set the group ID for the new directory:
mkdir -m g=group_name new_directory
This command creates new_directory and sets its group ID to group_name.
Creating a Directory and Making it a Temporary Directory
The t
option allows you to make a directory a temporary directory, which Linux treats slightly differently from a standard directory:
mkdir -m t new_directory
This command will create new_directory as a temporary directory. Files created in a temporary directory are usually deleted when they’re not accessed for a certain amount of time.
Making Multiple Levels of Nested Directories
The -p
option can be used to create multiple levels of nested directories with a single command:
mkdir -p Level1/Level2/Level3/Level4
This command will create four levels of directories. Level1 is the top-level directory, Level4 is the bottom-level directory, and Level2 and Level3 are intermediary directories.
Creating a Directory with Different Permissions for User, Group, and Others
The -m option allows you to specify different permissions for user, group, and others:
mkdir -m u=rwx,g=rx,o= new_directory
Here, new_directory is created with read, write, and execute permissions for the user (u), read and execute permissions for the group (g), and no permissions for others (o).
Creating a Directory in Verbose Mode and Ignoring Errors
The -v option can be combined with -p to ignore errors and provide verbose output:
mkdir -pv existing_directory
In this command, if existing_directory already exists, the mkdir command will not return an error and will provide a verbose output instead.
Making a Directory Without Verbose Mode
If you want to create a directory silently, without any verbose output, use the mkdir command without the -v option:
mkdir quiet_directory
This command creates a directory named quiet_directory without displaying any message.
Using Brace Expansion with mkdir Command
Brace expansion is a powerful feature in Linux that can be used with the mkdir command to create a sequence of directories:
mkdir dir{1..5}
This command will create five directories named dir1, dir2, dir3, dir4, and dir5.
Creating Directories and Setting Sticky Bit
The sticky bit is a permission bit that protects the files within a directory. If it’s set, the files can be deleted or renamed only by the owner of the files or root user. Here’s how you can set the sticky bit when creating a directory:
mkdir -m +t new_directory
With this command, new_directory is created with the sticky bit set.
Creating a Directory and Setting the Setuid and Setgid Bits
The setuid and setgid bits can be set when creating a directory, which influences the ownership of files and directories created within. The -m option allows you to set these bits:
mkdir -m u+s,g+s new_directory
This command will create new_directory with the setuid and setgid bits set.
Creating Directories with Different Permissions Using Octal Mode
While we’ve already discussed the symbolic mode for setting permissions, the mkdir command also supports the octal mode:
mkdir -m 700 private_directory
Here, private_directory is created with permissions set to ‘700’ (read, write, and execute for the user, and no permissions for group and others).
Creating a Directory and Making it Immutable
Making a directory immutable prevents it from being deleted, even by the root user. First, we create a directory and then make it immutable using the chattr
command:
mkdir new_directory
sudo chattr +i new_directory
This will create a new_directory and then make it immutable.
Creating a Directory and Setting Default ACLs
If your file system supports Access Control Lists (ACLs), you can set default ACLs when creating a directory:
mkdir new_directory
setfacl -d -m g::rwx new_directory
In these commands, new_directory is first created, and then the setfacl command sets the default ACL for the directory to rwx for the group.
Creating a Directory and Setting an SELinux Context
If you’re working on a system with SELinux enabled, you can set the SELinux context when creating a directory:
mkdir new_directory
chcon -t httpd_sys_content_t new_directory
Here, new_directory is created first. Then, the chcon command changes the SELinux context of new_directory to httpd_sys_content_t.
Conclusion
Mastering the mkdir command and its various options can greatly enhance your ability to manage directories on a Linux system. By understanding its syntax and experimenting with the examples provided, you can streamline your workflow and improve your command-line efficiency. Whether you’re creating single directories, multiple directories, or setting specific permissions, mkdir is an essential tool for any Linux user.