Follow Us:

mkdir recursive – Creating Linux Directories Recursively

The mkdir command helps us to create the directories. By default, you can create single-level directories. However, you can create multi-level directories by using a few additional parameters. In this article, we will use the mkdir command to create recursive directories. Let’s get started!

recursive-mkdir-linux

Creating directories using the mkdir command

We will start by creating a single directory using mkdir. You can refer to the below mkdir man while creating the directories:

firewallbuddy@ubuntu:~$ mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
--help display this help and exit
--version output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/mkdir>
or available locally via: info '(coreutils) mkdir invocation'

To create a simple directory, use the below command without any argument:

mkdir <directory_name>

For Example:

firewallbuddy@ubuntu:~$ mkdir Hello

This will create a Hello directory into the current directory. To verify the newly created directory, you can use the list command.

firewallbuddy@ubuntu:~$ ls -l
total 4
drwxrwxr-x 2 firewallbuddy firewallbuddy 4096 Jan 6 22:02 Hello

Additionally, you can enable the verbose with the mkdir command. It will print the action of the command. You can use the below option with mkdir for verbose:

mkdir -v <directory_name>

or

mkdir --verbose <directory_name>

We will create another directory, i.e., example with verbose.

firewallbuddy@ubuntu:~$ mkdir -v example
mkdir: created directory 'example'

You can also use the same command to create multiple directories under the parent directory.

mkdir --verbose <directory1_name> <directory2_name> .... <directoryN_name>

Here, we created five directories, i.e.,Β  Hello Welcome to Firewall Buddy under the parent directory.

firewallbuddy@ubuntu:~$ mkdir -v Hello Welcome to Firewall Buddy
mkdir: created directory β€˜Hello’
mkdir: created directory 'Welcome'
mkdir: created directory 'to'
mkdir: created directory 'Firewall'
mkdir: created directory 'Buddy'

By default, mkdir checks the parent directory. If the parent directory doesn’t exist, it will print an error without creating the directories.

firewallbuddy@ubuntu:~$ mkdir this/is/an/example
mkdir: cannot create directory β€˜this/is/an/example’: No such file or directory

Creating multiple directories recursively using mkdir

The mkdir uses the parents option to create the recursive directories. By using this option, mkdir will check the parent directory. If the parent directory doesn’t exist, it will create a new directory.

You can use the below option with mkdir to create the directories recursively:

mkdir -p <first_directory>/<second_directory>/<third_directory>

or

mkdir --parents <first_directory>/<second_directory>/<third_directory>

Now, Let’s create multiple directories using this option.

mkdir -p -v this/is/an/example

This command will create the directories recursively.

firewallbuddy@ubuntu:~$ mkdir -p -v this/is/an/example
mkdir: created directory 'this'
mkdir: created directory 'this/is'
mkdir: created directory 'this/is/an'
mkdir: created directory 'this/is/an/example'

You can check the directory structure using the below command:

firewallbuddy@ubuntu:~$ tree this
this
└── is
└─── an
└──── example

The mkdir allows us to use multiple options, i.e., verbose and parents, in one go. For Example:

mkdir -vp this/is/an/example

Further Reading

Conclusion

The mkdir command helps us to create the directories in the computing system. It accepts different options and allows us to create the directories differently. We used the parents or p option and created multiple directories in one go.

Did you like this article? Please share it on social media platforms and show us some love πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *

Copy link

iptrainer.net is now firewallbuddy.com!