2120

I would like to change the permissions of a folder and all its subfolders and files in one step (command) in Linux.

I have already tried the below command but it works only for the mentioned folder:

chmod 775 /opt/lampp/htdocs

Is there a way to set chmod 755 for /opt/lampp/htdocs and all of its content including subfolders and files?

Also, in the future, if I create a new folder or file inside htdocs, how can the permissions of that automatically be set to 755?

I had a look at this Stack Overflow question too:

How can I set a default 'chmod' in a Linux terminal?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Adam Halasz
  • 55,145
  • 63
  • 144
  • 211
  • Are you trying to script the command? Perhaps bash, csh or zsh? – jww Jun 16 '14 at 10:01
  • 4
    Did you intend to write `chmod 75 /opt/lampp/htdocs` or should that really be `chmod 755 /opt/lampp/htdocs`? – HelloGoodbye Jan 20 '16 at 10:58
  • sudo chown -R username:group directory .............It will change ownership (both user and group) of all files and directories inside of directory and directory itself. – Akash Kandpal Dec 29 '17 at 07:25
  • 9
    @ArthurDent, because this question (while perfectly valid and helpful) is more suited to a system-focused SE site, such as SuperUser or ServerFault. It's not expressly related to programming. – timelmer May 01 '18 at 20:38
  • 38
    @timelmer Sure, but I ask again, why are "permissions", "folder", and "cmod" tags? When would cmod ever be used 'expressly' for programming? – Arthur Dent May 02 '18 at 15:42
  • 3
    @timelmer Couldn't this question be about programming in bash? – Joffrey Aug 01 '18 at 14:44
  • 11
    If you want to get really picky, Bash is actually a scripting language, but then so is php, which SO doesn't seem to mind. – timelmer Aug 02 '18 at 17:59
  • 1
    So where's the link to this (obviously very helpful) question on a more appropriate SE site? – Patrick Sanan May 06 '19 at 15:17
  • Its both. scripting question and configuration topic. That is why its closed and not migrated. – Anonymous Type Aug 03 '19 at 02:59
  • 1
    @timelmer linux commands are programming, whoever says otherwise probably really likes adding these crazya** commands into linux core & distros. **ANYWAYS** program name + arguments is a language construct, and even from the natural language point of view this is the very definition of program-ing x) . – jave.web Feb 06 '20 at 00:52
  • 1
    this work for me -> sudo chmod -R -f 777 /home/$USER/$APP_ROOT – Javid Sattar Mar 01 '20 at 13:50
  • https://www.runwithcode.com/category/communication-commands/ – Shekhar May 05 '20 at 10:49
  • In my case, I also needed to add the "x" flag for executable scripts: `find . -type f -name "*.sh" -exec chmod +x {} \;` – Ilya Levin Jan 26 '21 at 09:58

21 Answers21

3365

The other answers are correct, in that chmod -R 755 will set these permissions to all files and subfolders in the tree. But why on earth would you want to? It might make sense for the directories, but why set the execute bit on all the files?

I suspect what you really want to do is set the directories to 755 and either leave the files alone or set them to 644. For this, you can use the find command. For example:

To change all the directories to 755 (drwxr-xr-x):

find /opt/lampp/htdocs -type d -exec chmod 755 {} \;

To change all the files to 644 (-rw-r--r--):

find /opt/lampp/htdocs -type f -exec chmod 644 {} \;

Some splainin': (thanks @tobbez)

  • chmod 755 {} specifies the command that will be executed by find for each directory
  • chmod 644 {} specifies the command that will be executed by find for each file
  • {} is replaced by the path
  • ; the semicolon tells find that this is the end of the command it's supposed to execute
  • \; the semicolon is escaped, otherwise it would be interpreted by the shell instead of find
Bob Stein
  • 14,529
  • 8
  • 77
  • 98
WombleGoneBad
  • 33,714
  • 1
  • 12
  • 2
  • 267
    Anyone care to explain what the `{} \;` on the end the line means? – Nilzor Mar 15 '13 at 10:14
  • 347
    @Nilzor `chmod 644 {} \;` specifies the command that will be executed by `find` for each file. `{}` is replaced by the file path, and the semicolon denotes the end of the command (escaped, otherwise it would be interpreted by the shell instead of `find`). – tobbez Mar 29 '13 at 15:46
  • 29
    "but why set the execute bit on all the files" Why not? I'm new to Ubuntu/Linux... – FlavorScape Jun 28 '13 at 00:31
  • 15
    @FlavorScape Womble is setting the execute bit exclusively on the directories returned by find. The execute permission on a directory allows a user class to list the contents of that directory and to cd into it. Generally speaking you want both r and x on a directory for it to be accessible to you, even though there might be strange edge cases where you'd set only one or the other. See this guide for more info: http://nixsrv.com/llthw/ex23 – Alexandr Kurilin Jul 26 '13 at 03:11
  • 29
    With the exception of sleepynate, every response neglected to address the aspect of his question regarding the setting of permissions to files/folders created in the *future*. – JGarrido Jan 08 '14 at 18:26
  • 1
    @tobbez doesn't the {} \; pass the output of find into the chmod command? In other words, I would think that {} \; specifies the parameters (file/path) that are passed from the find command to be subsequently received by, in this case, the chmod command's pipeline. – Ejoso May 26 '14 at 06:41
  • Because I downloaded a repository with bad permissions. Hundreds of files, half of them need to be executable. So I'd chmod all of them to build the project. – felwithe Jul 14 '14 at 23:25
  • 1
    Hmm.. a sticky question to inherit. Further, what about fstab ? Generally many LAMP apps (WP, say) want or expect 755 on everything under docroot, and the 600's are managed by exception elsewhere. But do validate for CIFS mounts as chmod can be ignored despite success being reported. (Not to suggest remote storage under www or alternative location.) – mckenzm Dec 12 '14 at 01:49
  • @mckenzm Was that a subtle allusion (set the _sticky_ bit and contents will _inherit_ permissions?) – jackvsworld Jul 21 '15 at 04:01
  • Yeah, trying not to get chatty, none of the answers cover the v or c flags either, which when used recursively confirm or echo the changed artifacts. Good for assessing "drift". A WP user of old, I liked the "Serverbuddy" plugin, which would check a site for 755. Didn't want to get into chown or sticky bits, or the next thing we know we are talking about ACL and third party schemes. chmod 755 -Rc {target} works. – mckenzm Jul 21 '15 at 08:05
  • 3
    For this, I had to do `/path/to/directory/*` to get it to work. Hope this helps somebody. – Goose May 18 '16 at 13:33
  • 3
    You should wrap at least the placeholder into single quotes, so that potentially existing spaces in filenames do not result in multiple arguments; i.e.: `-exec chmod 755 '{}' \;` — …and when using single quotes for the placeholder, it is easier to remember the possibility of escaping the semicolon with single quotes as well; i.e.: `-exec chmod 755 '{}' ';'` – sun Sep 01 '16 at 11:58
  • Duckduckgo shows this answer for the search `linux make directories public and files public chmod` – bgStack15 Sep 01 '16 at 15:25
  • 1
    Wouldn't you want 755 on a directory of executable scripts? – Jake Hm Dec 16 '16 at 15:03
  • 1
    When I do this, new files that are being created inside the directory are not inheriting the correct permissions, this command works fine on any existing files but every time I make a new file I need to run the command again. – Mark Kramer Mar 09 '17 at 00:38
  • It doesn't work for me :( it iterates through a bunch of empty string directories. I think it's because I'm in fish and not bash. I should just switch back to bash. – 6005 Oct 12 '17 at 22:21
  • Just to add, I also added -maxdepth 1 to the command to make it quicker in my case, as I only needed to apply to new folders created in the root of the target and not to all existing subfolders of those (which already had the permission, else wouldn't exist) – Liam Wheldon May 16 '18 at 12:59
  • 1
    *" It might make sense for the directories"* if this is not clear for anyone, that is because a user needs execute permission on the directory to traverse/open it. [source](https://serverfault.com/a/357109/371474) – Accountant م Jul 11 '18 at 00:27
  • Related: [Change all folder permissions with one command](https://unix.stackexchange.com/q/349053/80216) at U & L. – G-Man Says 'Reinstate Monica' Oct 13 '18 at 02:03
  • 1
    find . -type d -exec chmod 777 {} \; – Smart Coder Jun 17 '19 at 17:45
  • this work for me -> sudo chmod -R -f 777 /home/$USER/$APP_ROOT – Javid Sattar Mar 01 '20 at 13:54
  • https://chmodcommand.com/ can be used to translate the octal strangeness. For example, [https://chmodcommand.com/chmod-755/](https://chmodcommand.com/chmod-755/), [https://chmodcommand.com/chmod-644/](https://chmodcommand.com/chmod-644/), and [https://chmodcommand.com/chmod-777/](https://chmodcommand.com/chmod-777/). – Peter Mortensen Sep 05 '21 at 10:44
  • That can be done in short as `sudo chmod -R 777 /path/to/folder` – APu Apr 16 '22 at 10:03
863

Check the -R option

chmod -R <permissionsettings> <dirname>

In the future, you can save a lot of time by checking the man page first:

man <command name>

So in this case:

man chmod
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Steve Robillard
  • 13,289
  • 3
  • 35
  • 32
  • 38
    it stands for manual page and is a linux command that shows the man page for a command (most linux commands have a man page). try man ls or man man. – Steve Robillard Sep 18 '10 at 02:53
  • 9
    This did not work for me in the Terminal in Mac OS X. There I did "chmod -R *" and it worked. – Einar Ólafsson Jan 25 '12 at 09:51
  • 4
    Please do also see answer below by @WombleGoneBad . You will want to set permissions differently for files vs folders. – Sudhir Nov 09 '12 at 06:06
  • 157
    I think by now, it's actually faster to post here or use Google than to use `man`. Particularly for something like `grep` where if you are new to `man` it can be very time consuming to locate examples in the document, yet Google or SO provide examples within seconds. – ely Mar 05 '13 at 22:36
  • Great answer, man! Can you clarigy on how to start from the folder I'm in right now? – Konstantin Aug 13 '14 at 07:34
  • 2
    The first 2 lines of the answer are good. But checking out the proper call on the Stack Overflow is much faster than browsing the man pages. – Display Name May 27 '15 at 14:53
  • @Adam For questions like this, you should really use the `man` command. – Dodgie Oct 03 '15 at 00:06
  • @SteveRobillard One concern with the solution though, when it comes to `x` / execute permission, when you are doing `-x`; you don't want to accidentally make a directly not `cd`able. – ϹοδεMεδιϲ Apr 24 '16 at 08:41
  • 20
    For the record, those that are not familiar with Linux may not be familiar with man. I only know of it's existence, but trying to figure out how to use it would require a google adventure in and of itself. Possibly a worthwhile venture, but you should realize that I found THIS VERY post via google and it answered the EXACT question I was asking. Resources, my friend. Resources are invaluable. And you may think this was pointless and that he should have just checked the man, but this has helped me and HUNDREDS of others (thousand even). So please consider that. – Soundfx4 May 05 '16 at 18:28
  • 4
    Too many times do I see see the 'check the man page' as a way to shame you, quite annoying really. – landed Jun 08 '17 at 17:34
  • @landed it is not presented to shame anyone it is meant to show them how to fish rather than giving them a fish (i.e. how to help themselves). – Steve Robillard Jun 08 '17 at 19:40
  • Please, oh please, use the man pages. They are soooo useful. People think answer like `man chmod` are meant to shame. They are not. Often the answer is right there. You just have to read. It is annoying being asked to read for someone else. You will get a lot of good help if you explain why you still have a question after reading the man page. – Seth May 23 '19 at 13:18
  • for my case in Laravel it sovled by running chmod -R 777 projectName – MANSOOR KOCHY Apr 12 '22 at 15:52
431

If you want to set permissions on all files to a+r, and all directories to a+x, and do that recursively through the complete subdirectory tree, use:

chmod -R a+rX *

The X (that is capital X, not small x!) is ignored for files (unless they are executable for someone already) but is used for directories.

danronmoon
  • 3,682
  • 5
  • 33
  • 56
Pete
  • 4,327
  • 1
  • 11
  • 3
  • 31
    Great answer. Just note that * will not match hidden files (names beginning with a dot). It may make more sense, then, to use . (for the current directory). – Aaron Adams Apr 03 '13 at 22:53
  • 26
    This is the correct answer. For my case, I wanted to set all directories to **775** and all files to **664**. @Pete's excellent answer led me to the `chmod` docs, where I figured out I could use `chmod -R g+wX .`. Bravo! – chadoh Aug 04 '13 at 00:09
  • 9
    This won't remove `x` on files if they already have it. – Benoit Duffez Jan 30 '14 at 15:50
  • 7
    In case it's not entirely clear, uppercase X means "make all directories executable" (but not files). – Matt Browne Jan 21 '15 at 19:25
  • 7
    @BenoitDuffez if your goal is to also remove the executable bit from files, you can combine directives, like "a-x+rX" to remove "x" from everything and then set "r" to everything and "x" to directories only. – nunks Dec 19 '16 at 19:59
  • 2
    In `chmod -R a+rX` and `chmod -R g+wX`, `a` is all and `g` is group. There is also `u` user/owner and `o` other. In those commands, you're adding a read/write bit and an execute bit (capital `X` denotes "only if the file is a directory") – Rohmer Mar 30 '17 at 19:47
  • Great. So what I regularly want to do when I re-chmod my folder is: `sudo chmod -R u+rwX,g+rwX,o+rX .` setting 664 on files, and 775 on folders. – isaaclw Apr 09 '19 at 19:10
  • `sudo chmod -R a=-x,u=rwX,g=,o= folder` will set owner rw, others no access, directory with rwx. This will clear existing x on files – Pawel Cioch Jan 31 '21 at 06:20
  • 1
    seeing as I'm pretty sure this is the answer most people want, I wish it could be moved to the top... It takes a while to seek through all of these answers! – Evan Morrison Jul 23 '21 at 23:09
177

You can use -R with chmod for recursive traversal of all files and subfolders.

You might need sudo as it depends on LAMP being installed by the current user or another one:

sudo chmod -R 755 /opt/lampp/htdocs
Hashim Aziz
  • 2,547
  • 4
  • 27
  • 52
ravi ranjan
  • 5,496
  • 2
  • 17
  • 18
91

The correct recursive command is:

sudo chmod -R 755 /opt/lampp/htdocs

-R: change every sub folder including the current folder

PencilBow
  • 944
  • 9
  • 15
Pramendra Gupta
  • 14,279
  • 4
  • 32
  • 34
88

To set to all subfolders (recursively) use -R

chmod 755 /folder -R

And use umask to set the default to new folders/files cd /folder umask 755

Topera
  • 11,829
  • 14
  • 65
  • 101
  • 25
    DO NOT set your `umask` at 755! You won't be able to list, read or use any files or directories you create! – sleepynate Sep 18 '10 at 02:42
  • 4
    this sets all to 755, including files.! – Patrick Mutwiri Oct 27 '15 at 18:17
  • @sleepynate What on earth are you talking about? 755 means owner can read, write and execute, and all other users can read and execute, but not write to the object – svin83 Oct 08 '17 at 10:28
  • 4
    @NicHartley @svin83 umask is the bits that will be masked off of the permissions, so it is effectively the inverse of chmod. ```λ ~ → umask 022 λ ~ → umask -S u=rwx,g=rx,o=rx λ ~ → umask 755 λ ~ → umask -S u=,g=w,o=w``` – sleepynate Sep 14 '18 at 15:59
  • There shouldn't be any need for strikeout (that is what the revision history is for). The answer should appear as if it was written today. – Peter Mortensen Sep 06 '21 at 19:02
59

chmod 755 -R /opt/lampp/htdocs will recursively set the permissions. There's no way to set the permissions for files automatically in only this directory that are created after you set the permissions, but you could change your system-wide default file permissions with by setting umask 022.

sleepynate
  • 7,596
  • 3
  • 25
  • 38
  • 11
    sleepynate's answer is the ONLY correct one. The poster asked two questions: 1) How do I fix permissions on files and folders, and 2) how do I change the defaults (meaning "future" files). As nate said, the first is with "chmod", and the second is with "umask". I'd also add that there is NO WAY to change umask on just 1 directory... umask is "all or nothing" per user. However nothing stops you from creating a new user, who shares groups with you, so that you can just 'sudo -u someuser [create file]". That's thinking outside the box a little, but it's a fine workaround. – Scott Prive Jun 02 '15 at 21:19
  • I think you are right, but can't `inherit` be a solution to many cases or will it work with NTFS-3G exclusively? – Sebastian Schlicht Jul 29 '15 at 19:35
  • without give permission to folders may be created in the future, one may not be able to copy a folder,say `myforlder` from other places(like a PC) to this target directory(on a server) cause it will try to create a new folder in the target directory with name `myfolder` which you do not have permission to do so. As I have `root`, I just use `chown -R username:usergroup /directory`. – Jason Goal Nov 01 '18 at 22:58
32

You might want to consider this answer given by nik on Super User and use "one chmod" for all files/folders like this:

chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Iam Zesh
  • 1,737
  • 2
  • 19
  • 39
23

Here's another way to set directories to 775 and files to 664.

find /opt/lampp/htdocs \
\( -type f -exec chmod ug+rw,o+r {} \; \) , \
\( -type d -exec chmod ug+rwxs,o+rx {} \; \)

It may look long, but it's pretty cool for three reasons:

  1. Scans through the file system only once rather than twice.
  2. Provides better control over how files are handled vs. how directories are handled. This is useful when working with special modes such as the sticky bit, which you probably want to apply to directories but not files.
  3. Uses a technique straight out of the man pages (see below).

Note that I have not confirmed the performance difference (if any) between this solution and that of simply using two find commands (as in Peter Mortensen's solution). However, seeing a similar example in the manual is encouraging.

Example from man find page:

find / \
\( -perm -4000 -fprintf /root/suid.txt %#m %u %p\n \) , \
\( -size +100M -fprintf /root/big.txt %-10s %p\n \)

Traverse the filesystem just once, listing setuid files and direct‐
tories into /root/suid.txt and large files into /root/big.txt.

Cheers

Paresh Mangukiya
  • 37,512
  • 17
  • 201
  • 182
chrisfargen
  • 1,497
  • 1
  • 13
  • 11
21

Use:

sudo chmod 755 -R /whatever/your/directory/is

However, be careful with that. It can really hurt you if you change the permissions of the wrong files/folders.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Nate Starner
  • 511
  • 2
  • 15
16

chmod -R 755 directory_name works, but how would you keep new files to 755 also? The file's permissions becomes the default permission.

Rick
  • 1,146
  • 1
  • 20
  • 27
Sanchit Gupta
  • 1,273
  • 2
  • 11
  • 8
12

For Mac OS X 10.7 (Lion), it is:

chmod -R 755 /directory

And yes, as all other say, be careful when doing this.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
wmartin
  • 165
  • 1
  • 6
11

You want to make sure that appropriate files and directories are chmod-ed/permissions for those are appropriate. For all directories you want

find /opt/lampp/htdocs -type d -exec chmod 711 {} \;

And for all the images, JavaScript, CSS, HTML...well, you shouldn't execute them. So use

chmod 644 img/* js/* html/*

But for all the logic code (for instance PHP code), you should set permissions such that the user can't see that code:

chmod 600 file
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
NikolaiDante
  • 147
  • 1
  • 4
11

For anyone still struggling with permission issues, navigate up one directory level cd .. from the root directory of your project, add yourself (user) to the directory and give permission to edit everything inside (tested on macOS).

To do that you would run this command (preferred):

sudo chown -R username: foldername .*

Note: for currently unsaved changes, one might need to restart the code editor first to be able to save without being asked for a password.

Also, please remember you can press Tab to see the options while typing the username and folder to make it easier for yourself.


Or simply:

sudo chmod -R 755 foldername

but as mentioned above, you need to be careful with the second method.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Jakub A Suplicki
  • 4,056
  • 1
  • 21
  • 30
10

There are two answers of finding files and applying chmod to them.

The first one is find the file and apply chmod as it finds (as suggested by @WombleGoneBad).

find /opt/lampp/htdocs -type d -exec chmod 755 {} \;

The second solution is to generate a list of all files with the find command and supply this list to the chmod command (as suggested by @lamgesh).

chmod 755 $(find /path/to/base/dir -type d)

Both of these versions work nice as long as the number of files returned by the find command is small. The second solution looks great to the eye and more readable than the first one. If there are a large number of files, the second solution returns error: Argument list too long.

So my suggestion is

  1. Use chmod -R 755 /opt/lampp/htdocs if you want to change permissions of all files and directories at once.
  2. Use find /opt/lampp/htdocs -type d -exec chmod 755 {} \; if the number of files you are using is very large. The -type x option searches for specific type of file only, where d is used for finding directory, f for file and l for link.
  3. Use chmod 755 $(find /path/to/base/dir -type d) otherwise
  4. Better to use the first one in any situation
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Prabhu
  • 5,045
  • 4
  • 35
  • 44
8

I think Adam was asking how to change the umask value for all processes that are trying to operate on the /opt/lampp/htdocs directory.

The user file-creation mode mask (umask) is used to determine the file permissions for newly created files. It can be used to control the default file permissions for new files.

so if you will use some kind of FTP program to upload files into /opt/lampp/htdocs you need to configure your FTP server to use the umask you want.

If files / directories need be created, for example, by PHP, you need to modify the PHP code:

<?php
    umask(0022);
    // Other code
?>

If you will create new files / folders from your Bash session, you can set umask value in your shell profile ~/.bashrc file.

Or you can set up a umask in /etc/bashrc or /etc/profile file for all users.

Add the following to the file:

umask 022

Sample umask Values and File Creation Permissions
If umask value set to    User permission     Group permission     Others permission
000                            all                 all                   all
007                            all                 all                   none
027                            all             read / execute               none

And to change permissions for already created files, you can use find.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Viktor
  • 341
  • 3
  • 5
3

You can change permissions by using the following command:

sudo chmod go+rwx /opt/lampp/htdocs
MD SHAYON
  • 7,911
  • 47
  • 35
1

It's very simple.

In Terminal, go to the file manager. Example: sudo nemo. Go to /opt/, and then click PropertiesPermission. And then Other. Finally, change to create and delete and file access to read and write and click on button Apply... And it works.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Debian.
  • 43
  • 1
0

For already created files:

find . \( -type f -exec chmod g=r,o=r {} \; \) , \( -type d -exec chmod g=rx,o=rx {} \; \)

For future created files:

sudo nano /etc/profile

And set:

umask 022

Common modes are:

  • 077: u=rw,g=,o=
  • 007: u=rw,g=rw,o=
  • 022: u=rw,g=r,o=r
  • 002: u=rw,g=rw,o=r
0

Use :

chmod 775 -R /folder-name

in your case, it would be :

chmod 775 -R /opt/lampp/htdocs
Tosca
  • 345
  • 6
  • 12
-1

Use:

sudo chmod -R a=-x,u=rwX,g=,o= folder

Owner rw, others no access, and directory with rwx. This will clear the existing 'x' on files.

The symbolic chmod calculation is explained in Chmod 744.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Pawel Cioch
  • 2,652
  • 1
  • 25
  • 29