Creating a Directory with mkdir

The mkdir (make directory) command is the primary tool used to create directories in Linux. Its syntax is straightforward:

klingon$ mkdir directory_name

For example, to create a directory called projects, run:

klingon$ mkdir projects

This command will create a new folder named projects in your current working directory.

How umask affects mkdir

When you don’t pass -m, the final permissions are (requested_mode OR default_dir_perms) AND (NOT umask). On most systems the default dir perms are 0777, so:

umask # show current mask (e.g., 0022)
mkdir newdir # results in 0755 when umask is 0022

With -p, only newly created parents get the calculated mode; existing parents keep theirs.

Directory permission bits (r, w, x) in practice

  • r (read): list names inside the directory (ls).

  • w (write): create/delete/rename entries inside.

  • x (execute): traverse—enter with cd and access by path.

chmod 755 public_dir # owner rwx, group/others r-x (enter & list, can’t write)
chmod 700 private_dir # only you can enter/list/write

Verify exactly what was created

Use ls -ld (note the extra d to show the directory itself, not its contents):

ls -ld project code src
stat project # detailed metadata, including mode/owner and timestamps

Fast scaffolding with brace expansion

Create entire trees and sibling folders in one go:

mkdir -p app/{src/{lib,modules},bin,docs,tests/{unit,integration}}
mkdir -p site/{assets/{css,js,img},content/{posts,pages},_build}

Helpful flags you’ll actually use

mkdir -pv path/to/dir # -p: parents; -v: verbose (show each created dir)
mkdir -m 2775 teamshare # 2xxx sets setgid; see next section

Shared team directories (group inheritance)

For collaborative areas, set group ownership and the setgid bit so new files/dirs inherit the group:

sudo chgrp developers /srv/projects
sudo chmod 2775 /srv/projects # 2 = setgid on directory
# Optional: enforce default ACLs so new content stays writable by the group
sudo setfacl -d -m g::rwx /srv/projects
sudo setfacl -d -m o::rx /srv/projects

Ownership & sudo basics

  • Creating under system paths (e.g., /opt, /srv) often requires sudo.

  • Change owner/group after creation if needed:

sudo chown alice:developers /srv/projects

Safer deletes (so you don’t nuke a repo by accident)

rm -ri projects # confirm each step
rm -rI projects # one prompt if >3 files (GNU rm)
# Even safer: send to trash/recycle (install `trash-cli`)
trash-put projects
# Delete only empty leaf dirs:
find build -type d -empty -delete
# Guardrails when using variables:
rm -r — “$TARGET_DIR”

Creating Multiple Directories at Once

One of the strengths of mkdir is its ability to create multiple directories in one command:

klingon$ mkdir dir1 dir2 dir3

This command creates dir1, dir2, and dir3 simultaneously. This is useful when preparing project structures or setting up environments quickly.

Creating Parent and Subdirectories

Sometimes you need to create a nested directory structure. For example, you may want /project/code/src. Without options, mkdir fails if the parent directory doesn’t exist. The -p (parents) option solves this:

klingon$ mkdir -p project/code/src

This command creates all non-existing parent directories in the path.

Setting Permissions While Creating a Directory

By default, directories are created with user permissions governed by your umask. If you want to specify permissions at creation, the -m (mode) option is available. For example:

klingon$ mkdir -m 755 shared_folder

Here, the directory shared_folder is created with read, write, and execute permissions for the owner, and read/execute permissions for group and others.

Viewing Your Directory

After creating a directory, you can confirm it with the ls -l command:

klingon$ ls -l

This lists permissions, owners, and timestamps, giving you visibility into the newly created folder.

Modifying a Directory

You do not technically “edit” a directory, but you often modify its properties. Common modifications include:

    1. Changing PermissionsUse chmod to modify who can access the directory:klingon$ Changing OwnershipTo assign a directory to a different user or group, use:bashCopychown username:groupname projects chmod 700 private_folder
    2. Renaming a DirectoryUse the mv (move) command:klingon$ mv oldname newname These commands allow you to maintain security and order in your Linux file system.Removing a DirectoryDirectories are not permanent. You may need to delete them if they are no longer useful.
    3. Remove an Empty DirectoryUse rmdir:klingin$ rmdir projects This only works if the directory is empty.
    4. Remove a Non-Empty DirectoryUse rm -r (recursive) cautiously:klingon$ rm -r projects This will delete the directory and everything inside it. To confirm each deletion, add the -i option:klingon$ rm -ri projects

Best Practices for Directory Management

    1. Always double-check the path before removing a directory, especially with rm -r.
    2. Use meaningful names for directories to keep projects organized.
    3. Employ proper permissions to control access and enhance security.
    4. Structure directories logically when working with projects (e.g., docs/, src/, bin/).

Common errors & quick fixes

  • No such file or directory: parent missing → add -p.

  • File exists: target already exists → use -p to suppress error or choose a new name.

  • Permission denied: create somewhere you own or use sudo.

  • Operation not permitted: immutable bit/readonly FS/SELinux → check with lsattr, remount, or fix context (see below).

Conclusion

The linux mkdir create directory workflow is one of the first skills every Linux administrator must master. The mkdir command provides simplicity for creating folders, while supporting advanced usage like nested structures and permissions. Combined with tools like rmdir, rm, chmod, and chown, you gain complete control over creating, removing, and modifying directories in Linux. By learning these commands, you not only keep your system organized, but also enforce good security and workflow practices.

FAQs: mkdir and Directory Management

1) What permissions does mkdir use by default?
By default, new directories start from 0777 and your current umask subtracts bits. For example, with umask 0022, mkdir newdir becomes 0755. Use mkdir -m 700 secret to override.

2) What’s the difference between directory and file permissions?
On directories, x means “traverse/enter,” r lists names (ls), and w lets you create/rename/delete entries. On files, x means “execute.” That’s why dirs commonly use 755 or 750.

3) How do I create nested directories in one command?
Use parents with -p:

mkdir -p project/code/src

If any parent exists, -p won’t error.

4) Can I create multiple siblings quickly?
Yes—pass multiple names or use brace expansion (from the shell, not mkdir itself):

mkdir dir1 dir2 dir3
mkdir -p app/{src,bin,docs,tests/{unit,integration}}

5) How do I set permissions at creation time?
Use -m with octal or symbolic modes:

mkdir -m 755 public_html
mkdir -m u=rwx,go=rx shared_docs

6) Why does mkdir -m not change existing parent directories when I use -p?
-m applies only to directories that are created in that call. Existing parents keep their current permissions.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>