Categories
chmod directory linux permissions

How do I change permissions for a folder and its subfolders/files?

2138

How do I change the permissions of a folder and all its subfolders and files?

This only applies to the /opt/lampp/htdocs folder, not its contents:

chmod 775 /opt/lampp/htdocs

How do I set chmod 755 for all of the /opt/lampp/htdocs folder’s current contents, as well as automatically in the future for new folders/files created under it?

Related: How can I set a default ‘chmod’ in a Linux terminal?

13

  • 4

    Did you intend to write chmod 75 /opt/lampp/htdocs or should that really be chmod 755 /opt/lampp/htdocs?

    Jan 20, 2016 at 10:58

  • 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 1, 2018 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?

    May 2, 2018 at 15:42

  • 3

    @timelmer Couldn’t this question be about programming in bash?

    – Joffrey

    Aug 1, 2018 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 2, 2018 at 17:59

3388

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

24

  • 269

    Anyone care to explain what the {} \; on the end the line means?

    – Nilzor

    Mar 15, 2013 at 10:14

  • 350

    @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, 2013 at 15:46

  • 29

    “but why set the execute bit on all the files” Why not? I’m new to Ubuntu/Linux…

    Jun 28, 2013 at 0: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: nixsrv.com/llthw/ex23

    Jul 26, 2013 at 3: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 8, 2014 at 18:26

870

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

13

  • 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.

    Sep 18, 2010 at 2:53

  • 9

    This did not work for me in the Terminal in Mac OS X. There I did “chmod -R <permissionsettings> <dirname>*” and it worked.

    Jan 25, 2012 at 9:51


  • 4

    Please do also see answer below by @WombleGoneBad . You will want to set permissions differently for files vs folders.

    – Sudhir

    Nov 9, 2012 at 6: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 5, 2013 at 22:36

  • 21

    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 5, 2016 at 18:28

432

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.

9

  • 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).

    Apr 3, 2013 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 4, 2013 at 0:09

  • 10

    This won’t remove x on files if they already have it.

    Jan 30, 2014 at 15:50

  • 9

    In case it’s not entirely clear, uppercase X means “make all directories executable” (but not files).

    Jan 21, 2015 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, 2016 at 19:59