Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create nonexistent subdirectories recursively using Bash?

People also ask

How do I make a directory recursive in Linux?

The -p option is used to create multiple child directories with mkdir command in a recursive manner. In order to create directories recursively non of the specified directories should exist because all of them are created automatically. In the following example, we create the directories aaa , bbb and ccc .

Does mkdir create subdirectories?

Creation of an entire directory tree can be accomplished with the mkdir command, which (as its name suggests) is used to make directories. The -p option tells mkdir to create not only a subdirectory but also any of its parent directories that do not already exist.

How can I list subdirectories recursively?

Linux recursive directory listing using ls -R command. The -R option passed to the ls command to list subdirectories recursively.

How do I create a folder and subfolder in Linux?

mkdir Command Syntax in Linux Tip: Use cd to navigate to the directory where you want to create a sub-directory. You can also use the direct path. Use ls to list the directories in the current location.


You can use the -p parameter, which is documented as:

-p, --parents

no error if existing, make parent directories as needed

So:

mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"

mkdir -p "$BACKUP_DIR/$client/$year/$month/$day"

While existing answers definitely solve the purpose, if you’re looking to replicate a nested directory structure under two different subdirectories, then you can do this:

mkdir -p {main,test}/{resources,scala/com/company}

It will create the following directory structure under the directory from where it is invoked:

├── main
│   ├── resources
│   └── scala
│       └── com
│           └── company
└── test
    ├── resources
    └── scala
        └── com
            └── company

The example was taken from this link for creating an SBT directory structure.