mkdir command in UNIX allows users to create directories or folders as they are referred to in some operating systems. The
mkdir command can create multiple directories at once and also set permissions when creating the directory. The user running the command must have appropriate permissions on the parent directory to create a directory or will receive a permission denied error.
To create a directory in UNIX or Linux using the
mkdir command pass the name of directory to the
mkdir mydirectory ls mydirectory
To create multiple directories in UNIX or Linux using the
mkdir command pass the names of directories to be created to the
mkdir command. The names of directories should be separated by spaces.
mkdir foo bar baz ls foo bar baz
To create parent directories using the
mkdir command pass the
-p option. Suppose that the directory path
foo/bar/baz is to be created. This can be created with
mkdir as follows.
mkdir foo cd foo mkdir bar cd bar mkdir baz tree foo foo └── bar └── baz
This may also be achieved in a single command with the
mkdir -p foo/bar/baz tree foo foo └── bar └── baz
mkdir command will return a permission denied error when trying to create a directory in a parent directory that the user does not have permissions for.
mkdir /etc/foo mkdir: cannot create directory ‘/etc/foo’: Permission denied
In this instance to create a directory in this location the user will need
sudo permissions or to contact a system administrator.
To set permissions when creating a directory pass the
-m option. This accepts a number value to set the file mode. If no options are passed to
mdkir the directory will be created with read, write and execute permissions for the user (755).
In the following example the directory is created to be world readable.
mkdir -m 777 foo ls -la total 12 drwxr-xr-x 3 george users 4096 Sep 9 20:59 . drwxr-xr-x 8 george users 4096 Sep 9 20:47 .. drwxrwxrwx 2 george users 4096 Sep 9 20:59 foo
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Listening to BBC Radio with mpv
The BBC publishes high quality 320 kbps HLS AAC streams that can be used to listen to radio from the command-line using mpv. Here are the URLs and some aliases to start listening quickly.
Vim: So long Pathogen, hello native package loading
Vim 8 added a native way to load third-party packages. This means, for me at least, there is no longer any need to use a third party plugin helper like Pathogen.
Linux and Unix more command tutorial with examples
Tutorial on using more, a UNIX and Linux command for viewing the contents of a file or files one screen at a time. Examples of viewing a file, viewing multiple files, searching using regular expressions and opening the file in a text editor.