Skip to main content

Create multiple parent directories with mkdir

You have a directory /home/mydir and you want to create two new directories like this: /home/mydir/backup/something

When you run mkdir /home/mydir/backup/something you will get an error:

mkdir: cannot create directory ‘/home/mydir/backup/something’: No such file or directory

Normally, you would have to make both directories separately like mkdir /home/mydir/backup /home/mydir/backup/something

Add the -p flag (make parent directories along the way if needed) to fix it:

mkdir -p /home/mydir/backup/something

mkdir created both backup and something directory in the same path