This subchapter looks at mkdir, a UNIX (and Linux) command.
mkdir is used to make a directory.
Teach Yourself UNIX/Linux System Administration and Shell Programming
table of contents
If you like the idea of this project,
then please donate some money.
more information on donating
This subchapter looks at mkdir, a UNIX (and Linux) command.
mkdir is used to 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.
mkdir will fail if the directory already exists.
mkdir will fail if a file with the same name already exists.
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$
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.
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$
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.
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.
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.
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.
The long options are:
The following items are advanced topics. They are included here so that you can easily find them when needed.
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.
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.
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.
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.
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
$
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.
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).
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).
return to table of contents
free downloadable college text book
free downloadable system administrator and shell programming book
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.
Teach Yourself UNIX/Linux System Administration and Shell Programming
Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.
If you like the idea of this project,
then please donate some money.
send donations to:
Milo
PO Box 1361
Tustin, California 92781
Supporting the entire project:
If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)
Some or all of the material on this web page appears in the
free downloadable college text book on computer programming.
This web site handcrafted on Macintosh computers using Tom Benders Tex-Edit Plus and served using FreeBSD .
UNIX used as a generic term unless specifically used as a trademark (such as in the phrase UNIX certified). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.
Names and logos of various OSs are trademarks of their respective owners.
Copyright © 2012, 2013, 2014 Milo
Created: February 14, 2012
Last Updated: February 2, 2014
return to table of contents
free downloadable college text book
free downloadable system administrator and shell programming book