How to create a directory and give permission in single command
Solution 1
According to mkdir's man page...
mkdir -m 777 dirname
Solution 2
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
Solution 3
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.
Solution 4
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
Solution 5
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.
whiterose
Updated on July 08, 2022Comments
-
whiterose almost 2 years
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?
-
whiterose about 13 yearsThanks delan.. I am also wrote that command in shell script.. But i want to do in single command.
-
whiterose about 13 yearsCan u pls tell me about the -m option?
-
Costin Gușă about 10 yearsbonus, you can also add -g and/or -o and you can have mkdir, chmod and chown in a single shot!
-
TMKasun about 10 years@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.
-
Display Name about 9 yearsmkdir -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 about 9 yearsIt'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.
-
Markus W Mahlberg about 9 years@DisplayName: which nevertheless sets the correct permissions for
three
, with the default permissions being sufficient for the path to it. -
Adam Badura almost 8 yearsBeside 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. -
Omer Gafar about 6 yearsthere is no space between -m and 777
-
Scott Prive over 5 yearsmkdir 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
-
Hugo Deiró over 5 yearsmkdir -m777 directory
-
Tylla over 5 years@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. -
Scott Prive over 5 years@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.
-
Tylla over 5 years@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. -
Levi almost 5 yearsThis will only give permission 777 to the final subdirectory. How can I do this and give permission 777 to all the parent directories too?
-
Pedro Trujillo almost 5 yearsI don't have an answer for this but I believe the answer for this is here: stackoverflow.com/questions/3740152/…
-
Pedro Trujillo almost 5 yearsNote 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 {} \;
-
John Mellor about 4 yearsYeah, 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.
-
wearego almost 4 yearsCould you argue why it's better?
-
Hi-Angel over 3 years@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? -
Raid almost 3 years@wearego Well I guess for one it allows setting ownership as well..
-
Akmal over 2 yearsI never read something so long trying to explain what can be explained in three lines.