mkdir – create a directory
mkdir command is one of the basic Unix commands which allows you to create new directories.
Basic mkdir usage
In its simplest form, mkdir takes one or more directory names as command line parameters.
Creating only one directory:
Creating a few directories at a time:
It’s very easy to verify that these commands have been successful, but getting the list of directories matching the newdir mask in /tmp:
Troubleshooting mkdir errors
Sometimes you get the mkdir cannot create directory error, usually that’s because specified directory exists or some element of path is missing or restricts you with file/directory permissions.
mkdir and non-existent parent directories
Sometimes you want to create a whole branch of directories tree, with a number of (initially empty) branched directories.
For example, if you decided to create a /tmp/mydir/newdir, and there is no /tmp/mydir directory present in /tmp, you will get an error:
The reason you get an error is because /tmp/mydir directory does not exist, so your request to create a newdir subdirectory in /tmp/mydir is invalid. The normal approach would be to create /tmp/mydir first, and then issue the same command again:
However, there is a special command line option in mkdir for taking care of non-existent parent directories like this: it’s -p parameter.
Here is the full example of using it, first you see that without the -p you would get an error, and then we verify that the /tmp/mynewdir parent directory was created as part of the mkdir -p command line:
See Also
- basic Unix commands
- Remove directory with rmdir
- Remove files and directories with rm
- Using md5deep for Comparing Directories in Unix
- Unix Commands
- How to Find Directories Larger Than 1GB in Linux
- How To Change Ownership of Files and Directories in Unix
mkdir command is one of the basic Unix commands which allows you to create new directories.
Basic mkdir usage
In its simplest form, mkdir takes one or more directory names as command line parameters.
Creating only one directory:
Creating a few directories at a time:
It’s very easy to verify that these commands have been successful, but getting the list of directories matching the newdir mask in /tmp:
Troubleshooting mkdir errors
Sometimes you get the mkdir cannot create directory error, usually that’s because specified directory exists or some element of path is missing or restricts you with file/directory permissions.
mkdir and non-existent parent directories
Sometimes you want to create a whole branch of directories tree, with a number of (initially empty) branched directories.
For example, if you decided to create a /tmp/mydir/newdir, and there is no /tmp/mydir directory present in /tmp, you will get an error:
The reason you get an error is because /tmp/mydir directory does not exist, so your request to create a newdir subdirectory in /tmp/mydir is invalid. The normal approach would be to create /tmp/mydir first, and then issue the same command again:
However, there is a special command line option in mkdir for taking care of non-existent parent directories like this: it’s -p parameter.
Here is the full example of using it, first you see that without the -p you would get an error, and then we verify that the /tmp/mynewdir parent directory was created as part of the mkdir -p command line:
See Also
- basic Unix commands
- Remove directory with rmdir
- Remove files and directories with rm
- Using md5deep for Comparing Directories in Unix
- Unix Commands
- How to Find Directories Larger Than 1GB in Linux
- How To Change Ownership of Files and Directories in Unix