mkdir
This subchapter looks at mkdir, a UNIX (and Linux) command.
mkdir is used to make a directory.
make a directory
Use the mkdir command to make a new directory (folder).
$ mkdir testdir
admins-power-mac-g5:~ admin$
The example includes an extended prompt that shows the current working directory. Using the ls command will confirm that the directory now exists in the current working directory.
common errors
mkdir will fail if the directory already exists.
mkdir will fail if a file with the same name already exists.
multiple directories
You can name multiple directories with the mkdir command. Simply list each of the new diretcories after the mkdir with a space character between each directory name.
$ mkdir directory1 directory2 directory3
admins-power-mac-g5:~ admin$
create with permissions
Use the -m option to create a directory with specific permissions. The old school version of the -m switch used octal permissions.
The permission chocies are the same as with the chmod command. Replace mode (a=rwx in the following example) with an appropriate mode argument. In the following example, themode is a=rwx, which would give all users read, write, and execute permissions.
$ mkdir -m mode directory1 directory2 directory3
admins-power-mac-g5:~ admin$
This option is often used in shell scripts to lock down temporary directories.
use a path
All of the previous examples involved the current working directory. You can use cd to move to a new directory and create a directory there. Or you can give an entire path name to a specific directory, using either a relative or absolute path name to create the directory anywhere in the file system.
$ mkdir directory1/directory2/directory3
admins-power-mac-g5:~ admin$
common error
The most common error in using mkdir with a path is one or more of the intervening directories havent been made yet. mkdir will fail.
creating parent directories
There is a solution to this problem. You can use the -p option to create directories along the path. You can therefore, create a series of parent and child directories all at once.
If you use the -p option and the directory already exists, the mkdir command will continue on through your path and create new directories as needed down to the last one you list.
$ mkdir -p directory1/directory2/directory3
admins-power-mac-g5:~ admin$
Intermediate directories are created with the permission bits of rwxrwxrwx (0777) as modified by the current umask, plus the write and search permission for the owner.
view directories made
You can use the -v to get a list of the directories created. This is most often used with the -p option, but you can use it by itself.
$ mkdir -v directory1/directory2/directory3
$
$ mkdir -pv directory1/directory2/directory3
$
The -v option is considered nonstandard and should not be used in shell scripts.
spaces in names
To create a directory with space in the directory name, you need to use quoting for the entire name so that the shell treats it as a single name rather than a series of individual directory names separeted by spaces.
$ mkdir "directory name"
$
While it is common to use spaces in file and directory names in graphic user interfaces (such as Macintosh, OS/2, and Windows, as well as Gnome or KDE), this may cause failure with older scripts or programs. Some programs may convert the spaces in a directory name into %20, such as converting "Name with spaces" into "Name%20with%20spaces". This might cause confusion or failure.
long options
The long options are:
-m | | --mode=MODE |
-p | | --parents |
-v | | -verbose |
-Z | | --context=CONTEXT |
| | --help |
| | --version |
advanced information
The following items are advanced topics. They are included here so that you can easily find them when needed.
scripting mkdir
If you create a directory in a script, you will want to check first to see if the script actually exists. The following line of code will create a directory called tmp
$ test ! -d $HOME/tmp && mkdir $HOME/tmp
This line relies on the short circuit evaluation by the test
statement. The first part of the test
checks to see if the specified directory does not exist. If the specified directory already exists, the test
fails and further evaluation stops. If the test
passes, then the mkdir
command is run.
create an entire directory tree
You can create an entire directory tree all at once. The example will create the following diretcory tree (which will be confirmed with ls commands):
example
|
--------------------------------
| | |
docs notes src
|
applet
|
------------------------------------------
| | | |
css html javascript php
$ mkdir -p example/{src/applet/{css,html,php,javascript},docs,notes}
$ ls example
docs notes src
$ ls example/src
applet
$ ls example/src/applet
css html javascript php
$
The {} curly braces are used to create directories at the same level and the directories at the same level are separated by the , comma.
create a directory and change to it
You can use the following function to both mkdir a new directory and cd to the newly created directory, all in one command.
$ function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
$
This function will work even if the directory name has spaces.
various operating systems
The mkdir command appears in the Linix, Mac OS X, OS/2, PC-DOS (also called MS-DOS), UNIX, and Windows operating systems. It also appears in the PHP programming language. In OS/2, PC-DOS, and Windows, the command can be abbreviated md.
set security context
When running SELinux, you can use the -Z or --context switch to set the security context. You must give the full context as user:role:type.
An example from the CentOS forum:
$ mkdir --context=system_u:object_r:httpd_sys_content_t:s0 /var/www/example.org
$
history
The mkdir command appeared in Version 1 of AT&T UNIX. In early versions of UNIX, the mkdir command had to be setuid root because the kernel did not yet have a mkdir syscall. The mkdir command made a directory with the mknod syscall, followed by linking in the . and .. directories.
The mkdir syscall originated in 4.2 BSD and was added to System V in Release 3.0.
other
On November 8, 2010, Ramesh Natarajan named this the number 35 most frequently used UNIX/Linux command at this web page 50 Most Frequently Used UNIX / Linux Commands (With Examples).
comments, suggestions, corrections, criticisms
free music player coding example
Coding example: I am making heavily documented and explained open source code for a method to play music for free almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).
View music player in action: www.musicinpublic.com/.
Create your own copy from the original source code/ (presented for learning programming).
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
Names and logos of various OSs are trademarks of their respective owners.