How do I create a directory and nested folders on a Linux server?
mkdir creates directories at the path you specify. Add -p when missing parent directories should also be created. Choose a location owned by your account for personal work; application directories should follow the application’s deployment ownership model.
Before you start
- An authorised SSH session on the Linux server.
Instructions
1. Inspect the parent location
Confirm that the destination belongs to the intended account. This example creates a new personal notes directory and does not touch a website root.
ls -ld -- "$HOME"Check the owner and permissions before creating the child directory.
2. Create only the required tree
Keep the quoted path intact. Re-running -p is acceptable when the directories already exist, but it will not repair their ownership or change an existing directory’s mode.
mkdir -p -- "$HOME/server-notes/commands"
ls -ld -- "$HOME/server-notes" "$HOME/server-notes/commands"The two directories are created if absent. The listing confirms both paths and their inherited access mode.
Check the result
- Both directory paths exist, have the expected owner and are outside any unintended public document root.
Troubleshooting
- A component already exists as a file
- Inspect that exact path. Rename or choose a different directory deliberately; do not remove the existing object blindly.
- New permissions are unexpected
- The process umask and any parent default ACL affect creation. Review them before changing access.
Official references
Use the documentation for your installed version when a flag, service name or platform feature differs.
