Table of Contents#
- Basic Syntax of
mkdir - Creating a Single Directory
- Creating Multiple Directories at Once
- Creating Parent Directories Recursively
- Setting Directory Permissions While Creating
- Common Practices
- Best Practices
- Troubleshooting
- Conclusion
- References
1. Basic Syntax of mkdir#
The basic syntax of the mkdir command is straightforward:
mkdir [options] directory_nameHere, options are the command - line flags that modify the behavior of the mkdir command, and directory_name is the name of the directory you want to create.
2. Creating a Single Directory#
To create a single directory, you simply need to provide the name of the directory after the mkdir command. For example, to create a directory named new_folder, you would run the following command:
mkdir new_folderYou can verify that the directory has been created by using the ls command:
lsThis will list all the files and directories in the current working directory, and you should see new_folder in the list.
3. Creating Multiple Directories at Once#
If you need to create multiple directories at once, you can list all the directory names separated by a space. For example:
mkdir folder1 folder2 folder3Running the ls command after this will show that folder1, folder2, and folder3 have been created in the current working directory.
4. Creating Parent Directories Recursively#
Sometimes, you may want to create a directory inside another directory that doesn't exist yet. By default, the mkdir command will give an error in such cases. However, you can use the -p or --parents option to create the parent directories recursively.
For example, let's say you want to create a directory structure like parent_dir/child_dir. If parent_dir doesn't exist, you can use the following command:
mkdir -p parent_dir/child_dirThis command will first create parent_dir if it doesn't exist, and then create child_dir inside it.
5. Setting Directory Permissions While Creating#
You can use the -m or --mode option to set the permissions of the directory while creating it. The permissions are specified in octal notation. For example, to create a directory named secure_folder with read, write, and execute permissions only for the owner, you can use the following command:
mkdir -m 700 secure_folderThe 700 octal value represents rwx (read, write, execute) for the owner and no permissions for the group and others.
6. Common Practices#
- Use Descriptive Names: When creating directories, use names that clearly describe their purpose. For example, if you are creating a directory to store all your project - related files, name it something like
project_name_files. - Organize Hierarchically: Create a hierarchical directory structure to make it easier to manage files. For example, you can have a top - level directory for all your work projects, and inside it, create separate directories for each project.
7. Best Practices#
- Check for Existing Directories: Before creating a directory, it's a good practice to check if it already exists. You can use the
test -dcommand to check if a directory exists. For example:
if [ ! -d "new_dir" ]; then
mkdir new_dir
fi- Use Environment Variables: If you are creating directories in a script, use environment variables to make the script more flexible. For example, you can use the
$HOMEenvironment variable to create a directory in the user's home directory:
mkdir $HOME/my_project_folder8. Troubleshooting#
- Permission Denied: If you get a "Permission denied" error when trying to create a directory, it means you don't have the necessary permissions to create a directory in the specified location. You can try running the command with
sudoto gain administrative privileges. For example:
sudo mkdir /var/www/new_site- Directory Already Exists: If you try to create a directory that already exists, the
mkdircommand will give an error. You can use the-poption to avoid this error, as it will not raise an error if the directory already exists.
9. Conclusion#
The mkdir command in Linux is a simple yet powerful tool for creating directories. By understanding its basic syntax, advanced options, and following common and best practices, you can efficiently manage your directory structure in Linux. Whether you are a beginner or an experienced Linux user, mastering the mkdir command is essential for effective file and directory management.
10. References#
- Linux Documentation Project: https://tldp.org/
- The Linux Documentation Handbook: https://www.tutorialspoint.com/unix/unix-using - man - pages.htm
- Linux Command Manual Pages: You can access the manual pages for the
mkdircommand by runningman mkdirin the terminal.