Owner Permissions are Unix File Permissions given to the specific owner to open,read,write or execute the file Group Permissions: Group permissions are Unix File Permissions to specific group of users to open,read,write and execute the file. Chmod command sets UNIX file permissions. First you need to telnet or SSH to the target computer, then in the command prompt you need to execute a chmod command. First you need to telnet or SSH to the target computer, then in the command prompt you need to execute a chmod command.
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 only for the mentioned folder:
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 link too:
AdamAdam22.1k5656 gold badges126126 silver badges190190 bronze badges
closed as off-topic by jww, Yu Hao, Mike Szyndel, Alexey Malev, Mark RotteveelJun 17 '14 at 11:48
This question appears to be off-topic. The users who voted to close gave this specific reason:
- 'Questions about general computing hardware and software are off-topic for Stack Overflow unless they directly involve tools used primarily for programming. You may be able to get help on Super User.' – jww, Yu Hao, Mike Szyndel, Mark Rotteveel
16 Answers
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
):To change all the files to 644 (
Francesco Boi-rw-r--r--
):2,90533 gold badges2828 silver badges4747 bronze badges
WombleGoneBadWombleGoneBad26.6k11 gold badge99 silver badges22 bronze badges
Check the -R option
chmod -R <permissionsettings> <dirname>
In the future, you can save a lot of time by checking the man page first:
So in this case:
Peter Mortensen14.2k1919 gold badges8888 silver badges114114 bronze badges
Steve RobillardSteve Robillard11.4k33 gold badges2929 silver badges3030 bronze badges
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:The
danronmoonX
(that is capital X
, not small x
!) is ignored for files (unless they are executable for someone already) but is used for directories.2,86044 gold badges2626 silver badges5050 bronze badges
PetePete3,48711 gold badge88 silver badges33 bronze badges
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:
Arpit14.1k88 gold badges6161 silver badges8686 bronze badges
ravi ranjanravi ranjan4,06922 gold badges1414 silver badges1717 bronze badges
To set to all subfolders (recursively) use -R
8,6711313 gold badges5252 silver badges9595 bronze badges
The correct recursive command is:
-R
: change every sub folder including the current folder5,25533 gold badges1919 silver badges4040 bronze badges
Pramendra GuptaPramendra Gupta12k44 gold badges2929 silver badges3434 bronze badges
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
.6,31522 gold badges2121 silver badges3737 bronze badges
You might want to consider this answer given by nik on superuser and use 'one chmod' for all files/folders like this:
Community♦
Iam ZeshIam Zesh97222 gold badges1212 silver badges3434 bronze badges
Use:
However, be careful with that. It can really hurt you if you change the permissions of the wrong files/folders.
Peter Mortensen14.2k1919 gold badges8888 silver badges114114 bronze badges
Nate StarnerNate Starner
Here's another way to set directories to 775 and files to 664.
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:Cheers
chrisfargenchrisfargen1,40211 gold badge1313 silver badges1010 bronze badges
chmod -R 755 directory_name
works, but how would you keep new files to 755 also? The file's permissions becomes the default permission.81011 gold badge1515 silver badges2424 bronze badges
Sanchit GuptaSanchit Gupta
You want to make sure that appropriate files and directories are chmod-ed/permissions for those are appropriate. For all directories you want
And for all the images, JavaScript, CSS, HTML...well, you shouldn't execute them. So use
But for all the logic code (for instance PHP code), you should set permissions such that the user can't see that code:
Peter Mortensen14.2k1919 gold badges8888 silver badges114114 bronze badges
NikolaiDanteNikolaiDante
For Mac OS X 10.7 (Lion), it is:
And yes, as all other say, be careful when doing this.
Peter Mortensen14.2k1919 gold badges8888 silver badges114114 bronze badges
wmartinwmartin
I think Adam was asking how to change umask value for all processes that tying to operate on
/opt/lampp/htdocs
directory.The user file-creation mode mask (umask) is use to determine the file permission for newly created files. It can be used to control the default file permission 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 umask you want.If files / directories be created for example by php, you need to modify php code
if you will create new files / folders from your bash session, you can set umask value in your shell profile ~/.bashrc Or you can set up umask in
/etc/bashrc
or /etc/profile
file for all users.add the following to file:umask 022And to change permissions for already created files you can use find.Hope this helps.
NIMISHAN97611 gold badge1313 silver badges2323 bronze badges
ViktorViktor
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).Second solution is to generate list of all files with
find
command and supply this list to the chmod
command (as suggested by @lamgesh).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
- 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
2,96533 gold badges2626 silver badges3737 bronze badges
It's very simple.
In Terminal go to file manager. example:
jwwsudo nemo
. Go /opt/
then click Properties → Permission. and then Other. Finally, change to create and delete and file acess to read and write and click on button apply... And work. 55.9k4242 gold badges245245 silver badges538538 bronze badges
Debian.Debian.
Not the answer you're looking for? Browse other questions tagged linuxpermissionsdirectorychmod or ask your own question.
If you are a Linux user, or a webmaster managing your own website (which is probably hosted on a Linux server), you will surely come across a situation when you try to upload a file or modify a document and receive the error “You do not have the permissions to upload file to the folder“. And after some googling, the solution is often as easy as setting the file permission to “775” or “777”. So what exactly does “777” mean? And why must it be ‘7’, and not ‘8’ or ‘9’?
Related: How to Rename Files in Linux
Understanding File Permissions
Unix systems (including Linux and Mac OS X) come with a file control mechanism to determine who can access a particular file or folder and what actions they can do to it. There are two parts to the file control mechanism, namely Classes and Permissions. Classes determines who can access the file while the Permissions determines the kind of action the user can do to the file.
There are three Classes – Owner, Group, Others.
- The Owner is the usually the creator of the files/folders. In Linux, files or folders that you created in your Home directory are usually owned by you, unless you specifically change the ownership.
- The Group contains a group of users who share the same permissions and user privilege.
- Others means the general public.
As for permissions, there are 3 type of actions that you can perform on a file/folder. You can either read, write or execute.
- Read – You can only view the file, but cannot modify the content of the file. When applied on Folder, you can only view the files in the folder, but you can’t delete from or add files into the folder.
- Write – You can edit and modify the file. For Folders, you can delete and add files into the folder.
- Execute – Execute is mainly used when you need to run the file (commonly used when you need to run a script).
When you combine the Classes and the Permissions, you will be able to control who can access the file and what actions they are able to do with it.
For example, the owner will usually have all the permissions (read, write and execute) to access the file. If you are not the owner of the file/folder, you have to change the Ownership of the file to your name, or change the permissions of Group or Others to read, write or execute. In a web server, if you are not able to upload a file, it is probably because you are not the owner of the destination folder, or you are not given sufficient permissions to add files to the folder.
What’s about the number?
With the basic understanding of the Classes and Permissions, let’s delve into it further and see how the “777” or “775” come about.
Every file and folder contain a 8-bit data that control the permissions. At its basic binary form, it will be “000”, which means no permissions of any form is granted. When you set a “Read” permission, it will add 4-bit to the data, making it “100” (in binary format) or a “4” in the usual decimal format. Setting a “Write” permission will add 2-bit to the data, making it “010” and “2” in decimal form. Lastly, setting an “Execute” permission adds 1-bit to the data, which will result in “001”, or “1” in decimal form. In short:
- Read is equivalent to ‘4’.
- Write is equivalent to ‘2’.
- Execute is equivalent to ‘1’
When we want to set permissions, we just add up the number. For example, to set the permissions to read and write, we will use ‘6’ (4 + 2) for the permission. For read, write and execute, we will use ‘7’ (4 + 2 + 1) for the permission. Here’s the different permutation:
0 – no permission
1 – execute
2 – write
3 – write and execute
4 – read
5 – read and execute
6 – read and write
7 – read, write, and execute
1 – execute
2 – write
3 – write and execute
4 – read
5 – read and execute
6 – read and write
7 – read, write, and execute
Depending on the permissions you want to grant to the file, you just set the number accordingly.
What about the 3 digits ‘777’? Well, the first digit is assigned to the Owner, the second digit is assigned to the Group and the third digit is assigned to the Others. So for a file with ‘777’ permission, everyone can read, write and execute the file. Here are some of the commonly used permissions:
- 755 – This set of permission is commonly used in web server. The owner has all the permissions to read, write and execute. Everyone else can only read and execute, but cannot make changes to the file.
- 777 – Everyone can read write and execute. In a web server, it is not advisable to set ‘777’ permission for your files and folders as it allows anyone to add malicious code to your server. However, in some cases, you will need to set the 777 permissions before you can upload any file to the server (For example, uploading images in WordPress)
- 644 – Only the owner can read and write. Everyone else can only read. No one can execute the file.
- 655 – Only the owner can read and write, but not execute the file. Everyone else can read and execute, but cannot modify the file.
Setting File Permissions in Command Line
In Linux, you can easily change the file permissions by right-clicking the file or folder and select “Properties”. There will be a Permission tab where you can change the file permissions. In the terminal, the command to use to change file permission is “
chmod
“. In short, “chmod 777” means making the file readable, writable and executable by everyone.Hopefully, this article can help you understand better about the file permissions in Unix system and the origin of the magical number “777”.
Become a pro at Linux with this bundle
Linux is the go-to operating system of programmers and computer geeks due to its sheer computing power and incredible customizability. But that also makes it much harder to learn the ropes of Linux, which is why you should invest in the Linux Power User Bundle. Requiring absolutely zero previous knowledge, this course bundle will teach you from the ground up to become a pro at using Linux. For a limited time, it can be yours for just $19.