Linux mkdir: Create Directories, Build Paths, and Fix Common Errors
Creating a directory from the Linux command line is simple; choosing the right path and understanding why a command fails are the parts that tend to trip people up. The basic command is: mkdir project That creates project inside your current working directory. To check where you are first, run pwd . Choose a path that matches the task You can pass mkdir a relative or absolute path. A relative…
Creating directories on the Linux command line is straightforward, but it can be frustrating when things go wrong. To create a single directory named "project" in the current working directory, simply use the command "mkdir project". To verify your current location, run "pwd" before executing the mkdir command.
You can specify either a relative or absolute path when using mkdir. A relative path is based on your current directory, while an absolute path begins with a forward slash and remains consistent regardless of your current directory. For example, "mkdir projects/demo" will create a directory named "demo" inside a folder called "projects", but only if "projects" already exists.
An absolute path, like "mkdir /home/alex/projects/demo", offers more predictability in scripts, as the current working directory may be unknown. If you need to create multiple directories simultaneously, separate their names with spaces, like "mkdir logs backups temp". These directories become siblings rather than nested within each other.
To create a directory tree that includes missing parent directories, use the "-p" option. For instance, "mkdir -p projects/app/logs" will generate "projects", "app", and "logs" if they don't exist already. It also works if the target directory is already there, which can be useful in scripts that might run multiple times.
When dealing with spaces in directory names, quote them with quotation marks to ensure they're treated as a single argument. For a hidden directory, such as ".local-data", run "ls -a" to reveal it with "ls", but note that the leading dot signifies a naming convention, not a special permission.
Permissions on new directories are typically determined by your shell's umask. A standard umask of 022 will yield permissions of 755, granting read, write, and execute access to the owner while providing read and execute access to group and other users. To create a directory with specific permissions, like 700 (owner-only access), use the "-m" option: "mkdir -m 700 private-data". The specified mode applies to the final directory in both -m and -p combinations.
If you encounter an error stating "file exists", check whether it's a directory or a file using "ls -ld path/to/name". If it's a file, you can't use mkdir again, but if it's a directory, you might reuse it. Permission denied errors arise when the shell lacks write or execute permission on the parent directory. Check the parent directory with "ls -ld path/to/parent" before attempting creation. Using "sudo mkdir" might create the directory under root ownership, limiting your ability to write within it.
Running "mkdir -p" on a missing parent directory will attempt to create it alongside the target directory. If you hit a "Read-only file system" error, the issue lies in the filesystem's write permissions, not a missing directory. Lastly, to verify that you've successfully created a directory, use "ls -ld project" to display its details, including permissions and owner. To automatically enter the newly created directory, chain the commands with "&&", like "mkdir project && cd project".
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.