146

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

I have to create lots of folder with full permission 777.

Commands

mkdir path/foldername
chmod 777 path/foldername 

I don't like to create and give permission in two commands. Can I do this in single command?

Community
  • 1
  • 1
whiterose
  • 4,391
  • 8
  • 24
  • 18
  • 2
    `mkdir temp; chmod 777 temp` is one line. You could make 'temp' a variable and save it as a bash command. Is this what you're looking to do? – styfle Apr 26 '11 at 05:33
  • 2
    hi @white-rose, please see the latest answer, it should be the accepted answer. bonus, it combines mkdir, chmod AND chown in a single bullet! – Costin Gușă Jun 03 '14 at 11:16

8 Answers8

283

According to mkdir's man page...

mkdir -m 777 dirname
alex
  • 479,566
  • 201
  • 878
  • 984
  • 2
    Can u pls tell me about the -m option? – whiterose Apr 26 '11 at 05:42
  • @Whiterose -m option is for Mode. Sets the permission bits for the newly-created directories to the value specified by the Mode variable. The Mode variable takes the same values as the Mode parameter for the chmod command, either in symbolic or numeric form. – TMKasun Jun 09 '14 at 11:14
  • 32
    mkdir -p -m is broken, since the mode is only applied to the last directory in the path you type. For example, mkdir -p -m 707 one/two/three. Even if all three directories are newly created, only the last one will have the requested permissions, and the others, default. install -d -m is broken the same way. – Display Name May 06 '15 at 05:21
  • Beside issue around `-p` that @DisplayName already mentioned above there is also another one. The `-m` will apply only if a directory is actually created. If it already exists its mode will not be changed. Depending on your context this might be good or bad. – Adam Badura Aug 31 '16 at 06:10
  • mkdir with -m MODE works for me with MULTIPLE targets. The above statement for "only applied to last directory in the path" is outdated and no longer correct. EXAMPLE (Ubuntu Trusty, 14): `$ mkdir /tmp/foo2 /tmp/bar2 -m 777 root@db9ab03624f5:/# ls -ld /tmp/*2 drwxrwxrwx 2 root root 4096 Nov 21 16:06 /tmp/bar2 drwxrwxrwx 2 root root 4096 Nov 21 16:06 /tmp/foo2` – Scott Prive Nov 21 '18 at 16:08
  • 2
    mkdir -m777 directory – Hugo Deiró Nov 21 '18 at 16:43
  • 3
    @Crossfit_and_Beer the above statement IS valid (at least for me on Debian 8 and 9). The emphasis is on the -p/--parents option. Do the test: `mkdir -p -m777 /tmp/foo/bar/baz` and you'll see the created directories will have their permissions set in accordance to the current umask, except the last one which will get the desired mode. – Tylla Dec 25 '18 at 20:34
  • @Tylla I don't see the difference between the test you propose, and the test I performed at the time of my comment. In my test result above, I included not just the test but it's result. Both directories I created received identical 777 permissions exactly as I had specified. – Scott Prive Dec 26 '18 at 22:01
  • 2
    @Crossfit_and_Beer the difference is - as I emphasized it - the --parent switch. You should create a chain of dirs like follows: `$ umask 022; mkdir -p -m777 /tmp/foo/bar/baz; ls -lR /tmp/ drwxr-xr-x 3 tylla tylla 4096 dec 26 23:39 foo drwxr-xr-x 3 tylla tylla 4096 dec 26 23:39 bar drwxrwxrwx 2 tylla tylla 4096 dec 26 23:39 baz` where /tmp was empty previously. If you're creating two separate dirs as you did, you won't notice the difference. – Tylla Dec 26 '18 at 22:46
28

When the directory already exist:

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

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

mkdir -m 777 -p /parent/dirs/to/create/your/dir
Pedro Trujillo
  • 1,559
  • 18
  • 19
  • 3
    This will only give permission 777 to the final subdirectory. How can I do this and give permission 777 to all the parent directories too? – Levi Jul 12 '19 at 10:28
  • 4
    I don't have an answer for this but I believe the answer for this is here: https://stackoverflow.com/questions/3740152/how-do-i-change-permissions-for-a-folder-and-all-of-its-subfolders-and-files-in – Pedro Trujillo Jul 15 '19 at 14:22
  • Note that this command is to solve this problem "How to create a directory and give permission in single command". I believe the solution to your question will be something like: `find /your/dirs -type d -exec chmod 755 {} \;` – Pedro Trujillo Jul 29 '19 at 15:10
25
install -d -m 0777 /your/dir

should give you what you want. Be aware that every user has the right to write add and delete files in that directory.

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
  • 2
    bonus, you can also add -g and/or -o and you can have mkdir, chmod and chown in a single shot! – Costin Gușă Jun 03 '14 at 11:15
  • 8
    It's broken. install -d -m 070 one/two/three. Even if all three directories in the path are newly created, only the last one will have the requested permissions set. mkdir -p -m is broken in the same manner. – Display Name May 06 '15 at 05:22
  • @DisplayName: which nevertheless sets the correct permissions for `three`, with the default permissions being sufficient for the path to it. – Markus W Mahlberg May 06 '15 at 08:40
  • @MarkusWMahlberg what's the point of having the correct permissions to just one path component, if the end result the same as if it didn't set them: I gonna have to run `chmod -R` for the whole hierarchy? – Hi-Angel Nov 03 '20 at 11:43
  • @Hi-Angel Principle of least required privilege. – Markus W Mahlberg Mar 28 '23 at 16:17
13

IMO, it's better to use the install command in such situations. I was trying to make systemd-journald persistent across reboots.

install -d  -g systemd-journal -m 2755 -v /var/log/journal
slm
  • 15,396
  • 12
  • 109
  • 124
Badi
  • 155
  • 1
  • 5
9

Just to expand on and improve some of the above answers:

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

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

...no error if existing, make parent directories as needed

The statements are vague and unclear in my opinion. But basically, it says that you can make the directory with permissions specified by "chmod numeric notation" (octals) or you can go "the other way" and use a/your umask.

Side note: I say "the other way" since the umask value is actually exactly what it sounds like -- a mask, hiding/removing permissions rather than "granting" them as with chmod's numeric octal notation.

You can execute the shell-builtin command umask to see 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 it, I'll get some output resembling this:

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

Now, to add just a tiny bit more about those octal permissions. When you make a directory, "your system" take your default directory perms' [which applies for new directories (its value should 777)] and slaps on yo(u)mask, effectively hiding some of those perms'. My umask is 022--now if we "subtract" 022 from 777 (technically subtracting is an oversimplification and not always correct - we are actually turning off perms or masking them)...we get 755 as stated (or "statted") earlier.

We can omit the '0' in front of the 3-digit octal (so they don't have to be 4 digits) since in our case we didn't want (or rather didn't mention) any sticky bits, setuids or setgids (you might want to look into those, btw, they might be useful since you are going 777). So in other words, 0777 implies (or is equivalent to) 777 (but 777 isn't necessarily equivalent to 0777--since 777 only specifies the permissions, not the setuids, setgids, etc.)

Now, to apply this to your question in a broader sense--you have (already) got a few options. All the answers above work (at least according to my coreutils). But you may (or are pretty likely to) run into problems with the above solutions when you want to create subdirectories (nested directories) with 777 permissions all at once. Specifically, if I do the following in mahome with a umask of 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 both yodirectory and yostuff, with only 777 perms for mastuffinyostuff. So it appears that the umask is all that's slapped on yodirectory and yostuff...to get around this we can use a subshell:

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

and that's it. 777 perms for yostuff, mastuffinyostuff, and yodirectory.

YenForYang
  • 2,998
  • 25
  • 22
8

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

mkdir -m777 path/foldername 
Omer Gafar
  • 129
  • 1
  • 6
8

You could write a simple shell script, for example:

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

Once saved, and the executable flag enabled, you could run it instead of mkdir and chmod:

./scriptname path/foldername

However, alex's answer is much better because it spawns one process instead of three. I didn't know about the -m option.

Community
  • 1
  • 1
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
5

Don't do: mkdir -m 777 -p a/b/c since that will only set permission 777 on the last directory, c; a and b will be created with the default permission from your umask.

Instead to create any new directories with permission 777, run mkdir -p in a subshell where you override the umask:

(umask u=rwx,g=rwx,o=rwx && mkdir -p a/b/c)

Note that this won't change the permissions if any of a, b and c already exist though.

John Mellor
  • 12,572
  • 4
  • 46
  • 35
  • 2
    Yeah, if you pass a 4-digit umask the leading digit always has to be zero (though you can omit it). It might be for symmetry with chmod, where the first octal digit sets the setuid, setgid and sticky bits, but if so it's rather pointless since umask doesn't allow you to restrict those. – John Mellor Jun 02 '20 at 18:46