I would like to change permissions of a folder and all its sub folders and files in one step (command) in Linux.
I have already tried the below command but it works
You might want to consider this answer given by nik on superuser 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)
For anyone still struggling with permission issues, you can back up one folder from the root directory of your project, then add yourself (user) to the directory and give permission to edit everything inside (tested on Mac OS).
To do that you would run this command (preferred):
sudo chown -R username:foldername .*
or simply:
sudo chmod -R 755 foldername
but as mentioned above, need to be careful with the second method.
There are two answers of finding files and applying chmod
to them. 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 {} \;
Second solution is to generate list of all files with 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 eye and more readable than the first one. If there are large number of files, the second solution returns error : Argument list too long.
So my suggestion is
chmod -R 755 /opt/lampp/htdocs
if you want to change permissions of all files and directories at once.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.chmod 755 $(find /path/to/base/dir -type d)
otherwiseTo 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
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 {} \;
The correct recursive command is:
sudo chmod -R 755 /opt/lampp/htdocs
-R
: change every sub folder including the current folder