When using the Bash shell in a Linux operating system, you may need to create directories to organize your files and projects. In this blog post, we will explore different methods to create directories using Bash commands.
Method 1: Using the mkdir
command
The mkdir
command is the most common way to create directories in Bash. Here’s how you can use it:
mkdir directory_name
Replace directory_name
with the desired name for your directory. This command will create a new directory in your current location.
Example:
To create a directory named “docs” in your current location, use the following command:
mkdir docs
Method 2: Creating nested directories
If you need to create a directory within another directory, you can use the -p
option with the mkdir
command. This option will create any parent directories that don’t exist.
mkdir -p parent_directory/child_directory
Replace parent_directory
with the name of the parent directory and child_directory
with the desired name for the nested directory.
Example:
To create a directory structure with a parent directory named “project” and a nested directory named “src”, use the following command:
mkdir -p project/src
Method 3: Creating multiple directories
In some cases, you may need to create multiple directories simultaneously. To achieve this, you can provide multiple directory names as arguments to the mkdir
command:
mkdir directory1 directory2 directory3
Simply add additional directory names separated by spaces.
Example:
To create three directories named “images”, “videos”, and “documents” in your current location, use the following command:
mkdir images videos documents
Method 4: Creating directories with specific permissions
By default, when you create a directory using the mkdir
command, it inherits the permissions of its parent directory. However, you can specify specific permissions using the -m
option, followed by the octal value representing the desired permissions.
mkdir -m 755 directory_name
Replace directory_name
with the name of the directory you want to create.
Example:
To create a directory named “public_html” with permissions set to 755
, use the following command:
mkdir -m 755 public_html
Conclusion
Creating directories in Bash is a simple and essential task when organizing your files and projects. By using the mkdir
command with various options, you can easily create directories with specific names, nested structures, and desired permissions. Start organizing your files efficiently with these Bash commands!