How To Make A New Directory In Linux

How To Make A New Directory In Linux

Reading time1 min
#Linux#Commands#Tech#mkdir#Shell#Scripting

Mastering Directory Creation in Linux: Beyond 'mkdir' Basics

Think creating a directory in Linux is just about typing mkdir? Think again. While the command itself is straightforward, leveraging advanced options and understanding the nuances of directory creation can be a game-changer — improving your scripting efficiency, system organization, and reducing costly errors.

In this post, we’ll dive deeper into the practical how-tos of creating directories in Linux. Whether you’re a newbie or a seasoned sysadmin, mastering these tips will enhance your command-line toolkit.


The Basics: mkdir Command Refresher

At its core, creating a directory in Linux is as simple as:

mkdir my_new_directory

This creates a new folder called my_new_directory in your current location.

But this barely scratches the surface. What if you need to create nested folders, set specific permissions at creation, or avoid errors when directories already exist? Let’s explore.


1. Creating Nested Directories with -p

One common scenario: you want to create several levels of directories at once, but some or all of them don’t yet exist.

Instead of running:

mkdir /home/user/projects/code/python/scripts

which will throw an error if any intermediate directory doesn't exist, use the -p (parents) option:

mkdir -p /home/user/projects/code/python/scripts

This will recursively create all missing parent directories without throwing errors.

Pro Tip: Use -p when scripting to avoid failing if the parent directories are missing.


2. Setting Permissions with -m

By default, newly created directories inherit permissions based on system umask. Sometimes, you want more control — for example, creating a directory with specific access rights right off the bat.

Use the -m (mode) option to set permissions explicitly:

mkdir -m 755 my_public_directory

This creates my_public_directory with permissions:

  • Owner: read/write/execute
  • Group: read/execute
  • Others: read/execute

You can combine mode specifications with nested creation:

mkdir -p -m 700 /home/user/private/project

Here, only you (the owner) will have full access to these new directories from the start.


3. Avoiding Errors When Directory Exists: Use -v and Error Handling

Attempting to create a directory that already exists triggers an error:

mkdir existing_dir
mkdir: cannot create directory ‘existing_dir’: File exists

To make your scripts more resilient:

  • Use mkdir -p, which ignores the error and continues silently if directories exist.
  • Use the verbose option (-v) to output what’s happening for easier debugging.

Example:

mkdir -pv /tmp/mydir/testdir

Output might look like:

mkdir: created directory '/tmp/mydir'
mkdir: created directory '/tmp/mydir/testdir'

This feedback is invaluable when running automated scripts and troubleshooting folder creation steps.


4. Creating Directories with Special Characters and Spaces

Filenames or directories sometimes contain spaces or special characters that need proper handling to avoid confusing your shell.

For example:

mkdir My\ Documents

or quoting it:

mkdir "My Documents"

Both commands create a folder named My Documents. Quoting is usually clearer and safer when developing scripts.


5. Using Variables & Command Substitution in Scripts

Dynamic directory names improve automation flexibility:

today=$(date +%Y-%m-%d)
mkdir -p "/backup/$today"

This creates a backup folder named after today’s date automatically each time the script runs — no manual renaming necessary!


Summary Table of mkdir Options Covered

OptionPurposeExample
-pCreate parent directories as neededmkdir -p /tmp/a/b/c
-mSet file mode (permissions)mkdir -m 700 private_data
-vVerbose outputmkdir -pv /var/log/new_logs

Conclusion

Directory creation in Linux might seem trivial, but mastering advanced options like recursive parent creation (-p), permission setting (-m), and verbosity (-v) gives you better control over your filesystem structure and script robustness.

Next time you run that simple mkdir, remember — there’s power under the hood waiting to be unlocked!

Feel free to experiment with these options as part of your daily workflow or automation scripts — your Linux environment will thank you.


Got any creative mkdir tips or questions? Drop them below!