How would I change all files to 644 and all folders to 755 using chmod
from the linux
command prompt? (Terminal)
7 Answers
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
-
34just for someone else like me, this doesn't work instead try
sudo find /your/location -type f -exec chmod 644 {} \;
for files andsudo find /your/location -type d -exec chmod 755 {} \;
for directories Commented Jun 23, 2015 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! Commented Mar 14, 2018 at 22:17
-
6
-
fails with
unable to execute /bin/chmod: Argument list too long
– vladkrasCommented Dec 28, 2018 at 5: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– MaXi32Commented Jun 23, 2020 at 0:35
The easiest way is to do:
chmod -R u+rwX,go+rX,go-w /path/to/dir
which basically means:
to ch
ange file mod
es -R
ecursively by giving:
u
ser:r
ead,w
rite and eX
ecute permissions,g
roup ando
ther users:r
ead and eX
ecute permissions, but not-w
rite 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
-
9
-
17This 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. Commented Sep 2, 2017 at 21:26
-
9
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:
- Set file/directory to r__r__r__ (0444)
- Add w for owner, to get rw_r__r__ (0644)
- 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.)
-
3
-
-
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!
-
1Life saver! Thanks for the clean solution to this issue! Worked for me when needing to fix permissions issues for a WordPress install!– twkCommented Mar 4, 2019 at 3:43
-
1Works well to make the adjustments within a directory. Commented Nov 20, 2019 at 19:58
Easiest for me to remember is two operations:
chmod -R 644 dirName
chmod -R +X dirName
The +X only affects directories.
-
7
-
2Wow, all these complicated solutions when there is this extremely simple and elegant way to do it! Thank you for sharing.– techjpCommented Feb 11 at 4:28
This worked for me:
find /A -type d -exec chmod 0755 '{}' \;
find /A -type f -exec chmod 0644 '{}' \;
Note: To include files/folders with spaces in their name you need '' around the {}.
-
4Be careful: These commands won't handle files or directories with spaces in their names. The commands in the accepted answer will. Commented Jul 9, 2015 at 14:09
-
1This 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 '{}' \;
Commented Sep 13, 2019 at 6:21
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.
http://superuser.com
:P but this question helped me here, thanks.chmod -R u+rwX,go+rX,go-w /foo