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

Updated:   |  Linux Commands articles

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. If the Special Group ID (SGID) bit is NOT set on the parent directory, 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 -ld /tmp/foo
drwxr-xr-x  1  root root 4096 Sep 18 19:02 /tmp/foo

 


Special Group ID (SGID)

If the Special Group ID (SGID) bit is set on the parent directory, the standard permissions of a newly created directory will be drwxr-sr-x (notice the "s").

~]# ls -ld /tmp/foo
drwxr-sr-x  1  root root 4096 Sep 18 19:02 /tmp/foo

 


ACL

ACL entries can be used to further control the permissions of sub directories. For example, let's say the setfacl command is used to set an ACL (access control list) so that the /tmp/foo directory defaults to drwxrwx--- (0770).

setfacl --default --modify u:root:rwx /tmp/foo
setfacl --default --modify g:root:rwx /tmp/foo

 

The getfacl command can be used to see the ACLs. Notice the output include mask, which adjusts the umask.

~]# getfacl /tmp/foo
# file: tmp/foo
# owner: root
# group: root
# flags: -s-
user::rwx
group::rwx
other::---
default:user::rwx
default:user:root:rwx
default:group::rwx
default:group:root:rwx
default:mask::rwx
default:other::---

 

Now when creating a directory below /tmp/foo, the newly created directory should also have the drwxrwx--- (0770) permissions.

~]# mkdir /tmp/foo/bar
~]# ls -ld /tmp/foo/bar
drwxrwx---+ 2 root root 6 Dec  7 21:54 /tmp/foo/bar

 


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

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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