Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The mkdir Commands You Will Use Most

mkdir backups
mkdir -p /var/www/example.com/public
mkdir logs cache tmp
mkdir -m 750 private

mkdir creates a directory in the current folder, or at the path you give. -p creates any missing parent folders on the way, several names create several folders at once, and -m sets the permissions directly. Check the result with ls -ld name; the ls command guide explains the columns.

Create Parent Directories with -p

mkdir /srv/app/releases/v2
mkdir -p /srv/app/releases/v2

The first command fails if /srv/app/releases does not exist yet. -p creates each missing folder in the path. It also lets mkdir succeed quietly when the directory already exists, which is why scripts use mkdir -p: running the script twice does not stop it with an error. Add -v to print each folder as it is created.

Several Directories at Once

mkdir -p project/{src,tests,docs}
mkdir -p /var/backups/2026-{01..12}
mkdir "client files"

The braces are a bash feature: the shell turns project/{src,tests,docs} into three paths before mkdir runs. Do not put spaces inside the braces. {01..12} expands to twelve numbered names. A name with a space needs quotes; without them, mkdir creates two folders called client and files. On a server, names without spaces save trouble in scripts later.

Permissions of a New Directory

A new folder takes its permissions from your umask. With the common umask 022, it becomes 755: the owner can write, and everyone else can list it and enter it. Run umask to see yours. -m sets the mode of the new folder whatever the umask is:

mkdir -m 700 ~/secrets
sudo mkdir -m 2775 /srv/shared

With -p, the -m mode applies only to the last folder in the path; the parents get the normal permissions. The 2 in 2775 sets the setgid bit, so files created inside get the folder's group, which suits a folder that several users share. Never use 777; chmod recursive explains the safe values.

Owner of a New Directory

sudo mkdir /var/www/example.com
sudo chown deploy:www-data /var/www/example.com
sudo install -d -o deploy -g www-data -m 750 /var/www/example.com

A folder belongs to the user who creates it, so sudo mkdir makes a folder owned by root. Change the owner afterward with chown, or create the folder in one step with install -d, which sets the owner, group, and mode together. More on owners is in chown recursive.

Create a Directory and Enter It

mkdir -p ~/build/app && cd ~/build/app

&& runs cd only if mkdir succeeded. The cd command guide covers moving between folders.

Fix Common Errors

  • File exists: something with that name is already there. ls -ld name shows whether it is a folder or a file. -p accepts an existing folder but still fails when the name belongs to a file.
  • Permission denied: creating a folder needs write permission on the parent folder. Check it with ls -ld on the parent, and use sudo for system paths such as /srv or /opt.
  • No such file or directory: a parent folder is missing. Add -p, or check the path for typos; see No such file or directory.
  • No space left on device: the disk is full, or it has run out of inodes, the entries every file and folder needs. df -h and df -i show both; see how to check disk space in Linux.
  • Read-only file system: the disk is mounted read-only, often after file system errors. Check findmnt and the kernel log before you write anything.

To remove a folder again, see how to remove a directory in Linux; to rename one, see how to rename a directory.

Was this answer helpful?
Loading...