How to Create Directories in Linux Using the mkdir Command?
The directory is one of the most meaningful units of a file system because the organization and management of files depend on it. Making a directory is a very simple process; that is one of the most frequent tasks performed on Linux. The most adhered command for doing so is the mkdir command (make directory). The system administrator, developer, or a beginner Linux user can perform the task of directory creation expertly and thereby make themselves efficient in the course of work.
The following article presents a complete note on the working of the command mkdir on Linux, explaining command syntax and options along with examples and FAQs.
Reasons to Create Directories in Linux
Creating directories in Linux is necessary for:
- Organization of files
- Management of project and application files
- Efficiently managed backup and log files
- Setting security through directory-based permissions and
- Workarounds in command-line scripts that deal with file manipulations.
What is mkdir Command?
The mkdir command works in a Linux operating system to make directories inside its file system. It is a simple command that comes with various options like the discussion of permission, creation of parent directories, and so on.
Basic Syntax of mkdir
mkdir [options] directory_name
mkdir: The command to create directories, mainly in Linux like systems.
[options]: Some optional flags for modifying the behavior of mkdir.
directory_name: The name of the directory to be created.
Options Frequently Used With mkdir Command
- Create a Directory
mkdir my_directory
This command will make a directory called my_directory in the current working directory.
- Make Multiple Directories
mkdir dir1 dir2 dir3
This single command will create three directories called dir1, dir2, and dir3 all at once.
- Make Parent Directories with -p (-Parents)
mkdir -p parent/child/grandchild
The -p option will make an automatic directory as soon as the ancestor directories do not exist.
- Assign Permissions While Making a Directory
mkdir -m 755 my_secure_directory
The -m option allows you to set permissions; 755 gives read, write, and execute for the owner and read and execute for others.
- Display Messages with -v (-Verbose)
mkdir -v new_directory
This option makes it print each time the mkdir creates a directory and confirms the creation.
- Check for an Existing Directory Before Creation
mkdir my_directory || echo "Directory already exists"
The scope of the command is that it creates a directory only when it does not exist.
Examples of Using the mkdir Command
Example 1: Creating a Directory in a Specific Location
mkdir /home/user/Documents/new_folder
Creates new_folder in the /home/user/Documents directory.
Example 2: Creating Nested Directories
mkdir -p /var/www/html/project/{css,js,images}
Creates a project directory in /var/www/html, with subdirectories css, js, and images.
Example 3: Creating a Directory with Different Permissions
mkdir -m 700 private_folder
Creates a private_folder, with full access given to the owner but no access to anyone else.
Conclusion
The mkdir command is one of the very basic yet important utilities used in Linux to create directories neatly. Understanding the syntax and options allows the user to build the filesystem in an organized manner. With options like -p, -m, and -v, users get flexibility and power while making a directory. The command can be beneficial in the case of either handling a personal system or a production one.
Frequently Asked Questions
What if I try creating a directory that exists already?
If you try to create a directory that already exists, Linux will return an error unless you use the -p option to suppress this error.
Can I create many directories at once?
Yes, you give multiple directory names with one mkdir command, and thus, you create a number of directories at the same time.
How can I check that a directory exists before creation?
The command below can be applied to verify that a directory exists before creation.
[ -d my_directory ] || mkdir my_directory
It creates a directory only if it doesn’t already exist.
How can I modify permissions after creating directories?
You can run a command to modify permissions after creating directories.
chmod 755 my_directory
It will give permissions to the created directory.
Can I use mkdir in a shell script?
Yes, you can use mkdir from inside the shell scripts for creating directories automatically.