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?
chmod 75 /opt/lampp/htdocs
or should that really be chmod 755 /opt/lampp/htdocs
?
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
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
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.
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.
chmod
docs, where I figured out I could use chmod -R g+wX .
. Bravo!
x
on files if they already have it.
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
sudo chmod 755 -R /directory
didn't work but sudo chmod -R 755 /directory
did. Weird. I'm using Mac os x by the way.
The correct recursive command is:
sudo chmod -R 755 /opt/lampp/htdocs
-R
: change every sub folder including the current folder
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
umask
at 755! You won't be able to list, read or use any files or directories you create!
λ ~ → umask 022 λ ~ → umask -S u=rwx,g=rx,o=rx λ ~ → umask 755 λ ~ → umask -S u=,g=w,o=w
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
.
inherit
be a solution to many cases or will it work with NTFS-3G exclusively?
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
.
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)
-exec
option of find
in order to overcome the Argument list too long
error.
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:
Scans through the file system only once rather than twice. 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. 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
{} +
can do the same thing with find that xargs does in general.
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.
chmod -R 755 directory_name
works, but how would you keep new files to 755 also? The file's permissions becomes the default permission.
For Mac OS X 10.7 (Lion), it is:
chmod -R 755 /directory
And yes, as all other say, be careful when doing this.
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
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.
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
Use chmod -R 755 /opt/lampp/htdocs if you want to change permissions of all files and directories at once. 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. Use chmod 755 $(find /path/to/base/dir -type d) otherwise Better to use the first one in any situation
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.
You can change permissions by using the following command:
sudo chmod go+rwx /opt/lampp/htdocs
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
It's very simple.
In Terminal, go to the file manager. Example: sudo nemo
. Go to /opt/
, and then click Properties → Permission. And then Other. Finally, change to create and delete and file access to read and write and click on button Apply... And it works.
nemo
is the graphical file manager for Cinnamon. It may or may not be installed and requires you to be running a graphical environment.
Use :
chmod 775 -R /folder-name
in your case, it would be :
chmod 775 -R /opt/lampp/htdocs
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.
Success story sharing
{} \;
on the end the line means?chmod 644 {} \;
specifies the command that will be executed byfind
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 offind
).