How to create a directory and give permission in one command

How to create a directory and grant permission in a single command on Linux?

I need to create many folders with a full resolution of 777 .

Teams

 mkdir path/foldername chmod 777 path/foldername 

I do not like to create and give permissions in two teams. Can I do this on one command?

+72
linux chmod mkdir
Apr 26 '11 at 5:30
source share
7 answers

According to the mkdir man page ...

 mkdir -m 777 dirname 
+159
Apr 26 2018-11-11T00:
source share
 install -d -m 0777 /your/dir 

should give you what you want. Keep in mind that each user has the right to write and delete files in this directory.

+16
Apr 23 '14 at 13:15
source share

You can write a simple shell script, for example:

 #!/bin/bash mkdir "$1" chmod 777 "$1" 

After saving and the flag of the executable file, you can run it instead of mkdir and chmod:

 ./scriptname path/foldername 

However, alex answer is much better because it generates one process instead of three. I did not know about the -m option.

+6
Apr 26 2018-11-11T00:
source share

IMO, it is better to use the install command in such situations. I tried to make systemd-journald persistent on reboots.

 install -d -g systemd-journal -m 2755 -v /var/log/journal 
+6
Apr 6 '17 at 9:01
source share

You can use the following command to create a directory and give permissions at the same time

 mkdir -m777 path/foldername 
+5
May 16 '18 at 13:17
source share

Just to expand and improve some of the answers above:

First, I will check the mkdir man page for GNU Coreutils 8.26 - it gives us this information about the options '-m' and '-p' (can also be given as -mode = MODE and are parents, respectively):

... set [s] the file mode (as in chmod), and not a = rwx - umask

... no error, if it exists, create parent directories if necessary

Statements are vague and unclear in my opinion. But basically it means that you can create a directory with the permissions indicated by the "numeric note chmod" (octal), or you can go "differently" and use / umask.

Side note: I say “differently”, since the umask value is actually what it sounds like - a mask by hiding / removing permissions rather than “providing” them, as with the octal chmod notation.

You can run the command created by the umask shell to find out what your 3-digit umask is; for me it's 022 . This means that when I execute mkdir yodirectory in a given folder (say mahome) and stat , I get output similar to this:

  755 richard:richard /mahome/yodirectory # permissions user:group what I just made (yodirectory), # (owner,group,others--in that order) where I made it (ie in mahome) # 

Now, to add just a little more about those eight-step resolutions. When you create a directory, "your system" takes your default perms directory "[which applies to new directories (its value should be 777)] and a slap on the yo (u) mask, effectively hiding some of these perms." My umask is 022 - now if we “subtract” 022 from 777 (technically, subtraction is redundant and not always correct - we actually turn off perms or mask them) ... we get 755, as indicated (or "statted") earlier .

We can omit “0” before 3-digit octal (so that they should not be 4-digit), because in our case we did not want (or rather did not mention) any stickybits, setuids or setgids (you may want to study them By the way, they can be useful since you are going to 777). In other words, 0777 implies (or equivalently) 777 (but 777 is not necessarily equivalent to 0777 - since 777 only sets permissions, not setuids, setgids, etc.)

Now, to apply this to your question in a broader sense, you already have several options. All of the above answers work (at least for my main reasons). But you can (or most likely) run into problems with the above solutions when you want to create subdirectories (subdirectories) with 777 access rights at the same time. In particular, if I do the following in mahome with umask 022:

 mkdir -m 777 -p yodirectory/yostuff/mastuffinyostuff # OR (you can swap 777 for 0777 if you so desire, outcome will be the same) install -d -m 777 -p yodirectory/yostuff/mastuffinyostuff 

I will get perms 755 for yodirectory and yostuff , only 777 perms for mastuffinyostuff . So it seems that umask is all that is removed on yodirectory and yostuff ... to get around this, we can use a subshell:

( umask 000 && mkdir -p yodirectory/yostuff/mastuffinyostuff )

and what is he. 777 perms for yostuff, mastuffinyostuff and yodirectory.

+3
Feb 27 '17 at 7:05
source share

When the directory already exists:

 mkdir -m 777 /path/to/your/dir 

If the directory does not exist and you want to create parent directories:

 mkdir -m 777 -p /parent/dirs/to/create/your/dir 
0
Jan 22 '19 at 22:54
source share



All Articles