In the Ubuntu system, we often need to create new folders (called “directories” in Linux) to organize files. The mkdir command is specifically designed for creating directories; it is simple to use and one of the essential basic commands for every Ubuntu user.
1. What is mkdir?¶
mkdir is an abbreviation for “make directory”. Its function is to create a new empty directory (folder) at the specified location.
2. Basic Usage: Create a Single Folder¶
The most common scenario is creating a new folder in the current directory.
Command Format: mkdir 文件夹名称
Example:
Suppose you are in the terminal and want to create a folder named projects on the desktop:
mkdir projects
After execution, a new folder named projects will appear in the current directory (here, the desktop directory).
3. Create a Folder at a Specified Path¶
If you want to create a folder in a different location (not the current directory), you need to specify the full path. The path can be a relative path (relative to the current directory) or an absolute path (starting from the root directory).
Relative Path Example:¶
If your current directory is ~/Documents (your “Documents” folder), and you want to create a notes folder under it:
mkdir ~/Documents/notes # ~ represents the home directory, ~/Documents is the Documents folder
Absolute Path Example:¶
Starting from the root directory /, directly specify the path (e.g., creating temp_files inside the /tmp folder at the root):
mkdir /tmp/temp_files
4. Key: Create Nested Folders (-p Option)¶
If you need to create a multi-level nested folder structure (e.g., a/b/c), using the basic mkdir will throw an error because the parent directories (e.g., a or a/b) do not exist. In this case, use the -p option (abbreviation for --parents), which automatically creates all non-existent parent directories.
Command Format: mkdir -p 多层路径
Example:
To create the three-level folder workspace/code/python:
mkdir -p workspace/code/python
After execution, it will automatically create workspace, workspace/code, and workspace/code/python without the need to create them one by one manually.
5. Common Issues and Solutions¶
Issue 1: Parent Directory in Path Does Not Exist¶
If you execute mkdir a/b/c and a or a/b does not exist, an error will occur:
mkdir: cannot create directory ‘a/b/c’: No such file or directory
Solution: Add the -p option to create all parent directories at once:
mkdir -p a/b/c
Issue 2: Permission Denied¶
If you try to create a folder in a location without permissions (e.g., the /root directory, which ordinary users typically lack permission to access):
mkdir /root/test
Error: mkdir: cannot create directory ‘/root/test’: Permission denied
Solution: Use sudo (administrator privileges) for system directories, but use it cautiously (to avoid accidentally modifying system files):
sudo mkdir /root/test # Enter the administrator password to execute
Tip: Use sudo only when necessary; avoid it for daily operations.
6. Summary¶
mkdiris the basic command for creating folders. Its core syntax is:mkdir [选项] 文件夹路径.- Basic usage:
mkdir 文件夹名(create in the current directory). - Multi-level folders: Always use the
-poption, e.g.,mkdir -p a/b/c. - Permission issues: Check the path for correctness or use
sudo(with caution).
With the above simple examples, you should quickly master the usage of mkdir. Practice different scenarios to become proficient in meeting daily folder creation needs!