LINFO

The mkdir Command



The mkdir command is is used to create new directories.

A directory, referred to as a folder in some operating systems, appears to the user as a container for other directories and files. However, Unix-like operating systems treat directories as merely a special type of file that contains a list of file names and their corresponding inode numbers. Each inode number refers to an inode, which is located in inode tables (which are kept at strategic locations around the filesystem) and which contains all information about a file (e.g., size, permissions and date of creation) except its name and the actual data that the file contains.

mkdir has the following syntax:

mkdir [option] directory_name(s)

directory_name is the name of any directory that the user is asking mkdir to create. Any number of directories can be created simultaneously.

Thus, for example, the following command would create three directories within the current directory (i.e., the directory in which the user is currently working) with the names dir_1, dir_2 and dir_3:

mkdir dir_1 dir_2 dir_3

If a directory name provided as an argument (i.e., input) to mkdir is the same as that of an existing directory or file in the same directory in which the user is asking mkdir to create the new directory, mkdir will return a warning message such as mkdir: cannot create directory `dir_1': File exists and will not create a file with that name. However, it will then continue to create directories for any other names provided as arguments.

It is necessary for a user to have write permission (i.e., permission from the system to create or change a file or directory) in the parent directory (i.e., the directory in which the new directory is to be created) in order to be able to create a new directory.

Directories created by mkdir automatically include two hidden directories, one representing the directory just created (and represented by a single dot) and the other representing its parent directory (and represented by two consecutive dots). This can be seen by using the ls (i.e., list) command with its -a option, which tells ls to show all directories and files, (including hidden ones) in any directory provided to it as an argument, or in the current directory if there are no arguments, i.e.,

ls -a

mkdir's -m option is used to control the permissions of new directories. New directories are by default created with the read, write and execute (i.e., run as a program if a program) permissions enabled for the owner (i.e., the creator of the directory by default) and group and the read and execute permissions enabled for other users. Thus, for example, to create a directory named dir_4 for which all three types of permissions were enabled for all users, the sequence 777 would be employed after -m, for example:

mkdir -m 777 dir_4

The first digit represents the owner, the second represents the group and the third represents other users. The number 7 represents all three types of permission (i.e., read, write and execute), 6 stands for read and write only, 5 stands for read and execute, 4 is read only, 3 is write and execute, 2 is write only, 1 is execute only and 0 is no permissions.

Thus, for example, to create a new directory named dir_5 for which the owner has read and write permissions, the group has read permission and other users have no permissions, the following would be used:

mkdir -m 640 dir_5

The -p (i.e., parents) option creates the specified intermediate directories for a new directory if they do not already exist. For example, it can be used to create the following directory structure:

mkdir -p food/fruit/citrus/oranges

It is very easy to confirm that this series of directories has been created by using the du (i.e., disk usage) command with the name of the first directory as an argument. In the case of the above example this would be

du food

Other options include -v (i.e., verbose), which returns a message for each created directory, --help, which returns brief information about mkdir, and --version, which returns the version number of the currently installed mkdir program.

Additional information about mkdir can be obtained from the mkdir man page.

Directories can be removed with the rmdir and rm commands.






Created August 22, 2005.
Copyright © 2005 The Linux Information Project. All Rights Reserved.