mkdir - Linux
Overview
The mkdir
command in Linux is used for creating directories. It stands for “make directory”. This command allows users to create one or more directories at once and can set permissions as needed during creation. It is particularly useful in scripting and organizing files on a system.
Syntax
The basic syntax of the mkdir
command is:
mkdir [OPTIONS] DIRECTORY...
DIRECTORY...
: One or more directories to create.
Options/Flags
mkdir
has several options that modify its behavior:
-m
,--mode=MODE
: Set the file mode (permissions) of the new directory. Mode can be specified in symbolic or numeric format (e.g.,755
).-p
,--parents
: Allows the creation of nested directories specified in the path. Makes parent directories as needed.-v
,--verbose
: Prints a message for each created directory, providing transparent feedback on what is being done.--help
: Displays help information and exits.--version
: Outputs version information and exits.
Examples
-
Create a Single Directory:
mkdir new_directory
This command creates a new directory named
new_directory
in the current path. -
Create Multiple Directories:
mkdir dir1 dir2 dir3
Creates three new directories (
dir1
,dir2
,dir3
) at once. -
Create Nested Directories:
mkdir -p project/src/images
The
-p
flag ensures all non-existent parent directories are created automatically. -
Set Permissions While Creating Directory:
mkdir -m 750 secure_dir
Creates a directory named
secure_dir
with access permissions of 750.
Common Issues
-
Permission Denied: Users may encounter this if they attempt to create directories in areas where they lack write permissions. To solve, either switch to a user with adequate permissions or modify the permissions of the target location.
-
Directory Exists: Trying to create a directory that already exists without using
-p
will yield an error. If the intention is to create subdirectories, ensure the use of-p
.
Integration
mkdir
can be effectively combined with other commands for more complex tasks:
Creating a directory and changing into it:
mkdir new_folder && cd new_folder
This command chain is common in scripting when a new directory is created and then immediately used as the working directory.
Creating directories from a list in a file:
xargs mkdir < dir_list.txt
Here, xargs
reads names from dir_list.txt
and passes them to mkdir
, which creates them.
Related Commands
rmdir
: Command to delete empty directories.rm
: Command used to delete directories containing files.ls
: List directory contents, often used to verify directory creation.
For more detailed information, consult the mkdir
man page:
man mkdir
or visit the GNU Coreutils official mkdir documentation.