238

How would I change all files to 644 and all folders to 755 using chmod from the linux command prompt? (Terminal)

emotality
  • 12,795
  • 4
  • 39
  • 60
hugo der hungrige
  • 12,382
  • 9
  • 57
  • 84
  • 6
    If someone (@animuson) would be so kind to explain me, why this chmod question is off-topic and all others (14,438 results) here aren't... – hugo der hungrige Jan 30 '14 at 11:44
  • 1
    Little late, but this one command will also do the accepted answer in one shot: chmod -R a=r,a+X,u+w /your/path – Kladskull Nov 05 '14 at 15:12
  • Good question, doesn't deserve closing. These should rather be moved to a stackoverflow sub site than closed. – Erik Friesen Jan 06 '15 at 17:35
  • 1
    @hugoderhungrige it means go ask it on a Server site like: `http://superuser.com` :P but this question helped me here, thanks. – emotality Feb 02 '15 at 19:19
  • This question comes first in Google, so it should be answered, instead of being closed. – kenorb May 21 '15 at 11:44
  • 4
    Short answer: [`chmod -R u+rwX,go+rX,go-w /foo`](http://superuser.com/q/91935/87805) – kenorb May 21 '15 at 11:46

7 Answers7

405

One approach could be using find:

for directories

find /desired_location -type d -print0 | xargs -0 chmod 0755

for files

find /desired_location -type f -print0 | xargs -0 chmod 0644
T.Todua
  • 53,146
  • 19
  • 236
  • 237
hugo der hungrige
  • 12,382
  • 9
  • 57
  • 84
  • 31
    just for someone else like me, this doesn't work instead try `sudo find /your/location -type f -exec chmod 644 {} \;` for files and `sudo find /your/location -type d -exec chmod 755 {} \;` for directories – NineCattoRules Jun 23 '15 at 17:11
  • I ran the original solution and it messed up my permissions on files and directories. watch out! the solution on the comment worked, thanks! – Mahsa Mortazavi Mar 14 '18 at 22:17
  • 6
    Why (?) it is better tham `chmod -R a=r,u+w,a+X /foo`? – Peter Krauss Sep 10 '18 at 00:23
  • fails with `unable to execute /bin/chmod: Argument list too long` – vladkras Dec 28 '18 at 05:13
  • What if I want only the subfolder to be chmod 755 when specifying the desired_location ? Because this also will make the parent folder 755 – MaXi32 Jun 23 '20 at 00:35
  • I would argue you could combine the two approaches: `sudo chmod -R 755 "/your/location"; sudo find "/your/location" -type f -exec chmod 644 {} \;`. (Not that it makes *that* much of a difference) – WoodrowShigeru Nov 19 '20 at 12:15
130

The easiest way is to do:

chmod -R u+rwX,go+rX,go-w /path/to/dir

which basically means:

to change file modes -Recursively by giving:

  • user: read, write and eXecute permissions,
  • group and other users: read and eXecute permissions, but not -write permission.

Please note that X will make a directory executable, but not a file, unless it's already searchable/executable.

+X - make a directory or file searchable/executable by everyone if it is already searchable/executable by anyone.

Please check man chmod for more details.

See also: How to chmod all directories except files (recursively)? at SU

T.Todua
  • 53,146
  • 19
  • 236
  • 237
kenorb
  • 155,785
  • 88
  • 678
  • 743
  • 9
    chmod -R a=r,u+w,a+X /foo – John Allsup Dec 26 '16 at 12:39
  • 16
    This answer, while neat, does have a problem: a file that is executable before running the command will be executable afterwards. See the answer of @JohnAllsup for a command that does not have this flaw. – mzuther Sep 02 '17 at 21:26
  • 8
    @mzuther unless this problem is actually feature for you – RiaD Dec 27 '18 at 20:13
71

The shortest one I could come up with is:

chmod -R a=r,u+w,a+X /foo

which works on GNU/Linux, and I believe on Posix in general (from my reading of: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/chmod.html).

What this does is:

  1. Set file/directory to r__r__r__ (0444)
  2. Add w for owner, to get rw_r__r__ (0644)
  3. Set execute for all if a directory (0755 for dir, 0644 for file).

Importantly, the step 1 permission clears all execute bits, so step 3 only adds back execute bits for directories (never files). In addition, all three steps happen before a directory is recursed into (so this is not equivalent to e.g.

chmod -R a=r /foo
chmod -R u+w /foo
chmod -R a+X /foo

since the a=r removes x from directories, so then chmod can't recurse into them.)

John Allsup
  • 1,072
  • 8
  • 9
48

On https://help.directadmin.com/item.php?id=589 they write:

If you need a quick way to reset your public_html data to 755 for directories and 644 for files, then you can use something like this:

cd /home/user/domains/domain.com/public_html
find . -type d -exec chmod 0755 {} \;
find . -type f -exec chmod 0644 {} \;

I tested and ... it works!

Christian Michael
  • 2,128
  • 1
  • 19
  • 27
25

Easiest for me to remember is two operations:

chmod -R 644 dirName
chmod -R +X dirName

The +X only affects directories.

Mark D
  • 5,368
  • 3
  • 25
  • 32
9

This worked for me:

find /A -type d -exec chmod 0755 {} \;
find /A -type f -exec chmod 0644 {} \;
Jair Reina
  • 2,606
  • 24
  • 19
  • 4
    Be careful: These commands won't handle files or directories with spaces in their names. The commands in the accepted answer will. – Chad Nouis Jul 09 '15 at 14:09
  • 1
    This caveat only applies, if {} isn't surrounded by quotes … thus, there's no reason to play around with print0 and xargs -0, the following suffices: `find /A -type X -exec chmod Y '{}' \;` – Michael Stumpfl Sep 13 '19 at 06:21
6

Do both in a single pass with:

find -type f ... -o -type d ...

As in, find type f OR type d, and do the first ... for files and the second ... for dirs. Specifically:

find -type f -exec chmod --changes 644 {} + -o -type d -exec chmod --changes 755 {} +

Leave off the --changes if you want it to work silently.

mza
  • 111
  • 2
  • 4