How to create directories in Java

Java directory FAQ: How do I create a directory (or directories) in Java? (Also written as, "How do I make a directory in Java?)

To create a directory in Java, just use the "mkdir" or "mkdirs" methods of the Java File class. Here are a few examples to demonstrate this.

Linux mkdir command man page

This page shows the contents of the Linux mkdir command man page. This mkdir command output was created on a CentOS Linux system.

You can see this same mkdir command man page output by entering this command on your own Linux system:

Examples of the Unix mkdir command

Linux directory FAQ: How do I create (make) a directory on Linux or Unix?

The Unix/Linux mkdir command is used to create new Unix/Linux directories (sub-directories). Let's take a look at some mkdir command examples.

How to create one directory

This first example creates a new directory named tmp in your current directory:

mkdir tmp

This example assumes that you have the proper permissions to create a new sub-directory in your current working directory.

mkdir man page

The contents of this page come from the CentOS Linux mkdir man page, i.e., the man page for the Linux mkdir command (also known as the help page for the mkdir command).

Linux mkdir command examples

As its name implies, the Linux mkdir ("make directory") command lets you create new directories.

Creating a new directory in your current directory is very simple. In our first mkdir example, we'll show how to create a new directory named "dir1":

mkdir dir1

If you want to create several directories at one time you can use a mkdir command like this:

Linux mkdir - How to create multiple directory levels at one time

Linux mkdir question: I'm about to go to work on a new server, and I want to create subdirectories named docs, bin, and lib, and the docs directory has two subdirectories named personal and business. How can I create these directories and subdirectories with one command?

Answer: Use the "-p" option of the Unix/Linux mkdir command. The answer is shown below: