FreeKB - Linux Commands mkdir (make directory)
Linux Commands - mkdir (make directory)

The mkdir command is used to make a directory. In this example, the "foo" directory is created.

mkdir /tmp/foo

 

The ls (list) command with the -l (long) option can be used to view the standard permissions and ownership of the newly created directory. By default, the standard permissions of a newly created directory will be drwxr-xr-x. The "d" indicates it is a directory. Additionally, the directory will be owned by the user that created the directory (root in this example).

ls -l /tmp
. . .
drwxr-xr-x  1  root root 4096 Sep 18 19:02 foo

 

Permission denied will be displayed if you do not have write permission to the parent directory. For example, let's say you want to create a directory under /opt. In this example, only root has write permission to the /opt directory. In other words, only root will be able to create a directory under /opt. All other users will get permission denied.

This can be resolved by giving sudo permission to the mkdir command.

ls -ld /opt
. . .
drwxr-xr-x 1 root root 4096 Sep 18 13:49

 

Parents

The -p or --parents option can be used to create parent directories (if they do not exist). In this example, the "foo" and "bar" directories will be created if they do not exist. Without the -p or --parents option, if a subdirectory does not exist (foo in this example), then the target directory (bar in this example) would not be created.

mkdir --parents /tmp/foo/bar

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 2c314 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |