Linux change group in folder

12 Linux Chown Command Examples to Change Owner and Group

The concept of owner and groups for files is fundamental to Linux. Every file is associated with an owner and a group. You can use chown and chgrp commands to change the owner or the group of a particular file or directory.

In this article, we will discuss the ‘chown’ command as it covers most part of the ‘chgrp’ command also. Even if you already know this command, probably one of the examples mentioned below might be new to you.

1. Change the owner of a file

# ls -lart tmpfile -rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile # chown root tmpfile # ls -l tmpfile -rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile

2. Change the group of a file

# ls -l tmpfile -rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile # chown :friends tmpfile # ls -l tmpfile -rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile

If you observe closely, the group of the file changed from ‘family’ to ‘friends’. So we see that by just adding a ‘:’ followed by the new group name, the group of the file can be changed.

3. Change both owner and the group

# ls -l tmpfile -rw-r--r-- 1 root family 0 2012-05-22 20:03 tmpfile # chown himanshu:friends tmpfile # ls -l tmpfile -rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile
# ls -l tmpfile_symlnk lrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile

So we see that the symbolic link ‘tmpfile_symlink’ links to the file ‘tmpfile’. Lets see what happens if chown command is issued on a symbolic link:

# chown root:friends tmpfile_symlnk # ls -l tmpfile_symlnk lrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile # ls -l tmpfile -rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile

When the chown command was issued on symbolic link to change the owner as well as the group then its the referent of the symbolic link ie ‘tmpfile’ whose owner and group got changed. This is the default behavior of the chown command. Also, there exists a flag ‘–dereference’ for the same.

5. Using chown command to forcefully change the owner/group of symbolic file.

# ls -l tmpfile_symlnk lrwxrwxrwx 1 himanshu family 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile # chown -h root:friends tmpfile_symlnk # ls -l tmpfile_symlnk lrwxrwxrwx 1 root friends 7 2012-05-22 20:03 tmpfile_symlnk -> tmpfile

6. Change owner only if a file is owned by a particular user

Using chown “–from” flag, you can change the owner of a file, only if that file is already owned by a particular owner.

# ls -l tmpfile -rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile # chown --from=guest himanshu tmpfile # ls -l tmpfile -rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile # chown --from=root himanshu tmpfile # ls -l tmpfile -rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile
  • In the example above, we verified that the original owner/group of the file ‘tmpfile’ was root/friends.
  • Next we used the ‘–from’ flag to change the owner to ‘himanshu’ but only if the existing owner is ‘guest’.
  • Now, as the existing owner was not ‘guest’. So, the command failed to change the owner of the file.
  • Next we tried to change the owner if the existing owner is ‘root’ (which was true) and this time command was successful and the owner was changed to ‘himanshu’.
Читайте также:  Сервер печати на линукс

On a related note, if you want to change the permission of a file, you should use chmod command.

If you are a beginner, you should start by reading the basics of file permissions.

7. Change group only if a file already belongs to a certain group

Here also the flag ‘–from’ is used but in the following way:

# ls -l tmpfile -rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile # chown --from=:friends :family tmpfile # ls -l tmpfile -rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile

Since the file ‘tmpfile’ actually belonged to group ‘friends’ so the condition was correct and the command was successful.

So we see that by using the flag ‘–from=:’ we can change the group under a particular condition.

NOTE: By following the template ‘–from=:’, condition on both the owner and group can be applied.

8. Copy the owner/group settings from one file to another

This is possible by using the ‘–reference’ flag.

# ls -l file -rwxr-xr-x 1 himanshu family 8968 2012-04-09 07:10 file # ls -l tmpfile -rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile # chown --reference=file tmpfile # ls -l tmpfile -rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile

In the above example, we first checked the owner/group of the reference-file ‘file’ and then checked the owner/group of the target-file ‘tmpfile’. Both were different. Then we used the chown command with the ‘–reference’ option to apply the owner/group settings from the reference file to the target file. The command was successful and the owner/group settings of ‘tmpfile’ were made similar to the ‘file’.

9. Change the owner/group of the files by traveling the directories recursively

This is made possible by the ‘-R’ option.

# ls -l linux/linuxKernel -rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/linuxKernel # ls -l linux/ubuntu/ub10 -rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/ubuntu/ub10 # ls -l linux/redhat/rh7 -rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/redhat/rh7 # chown -R himanshu:family linux/ # ls -l linux/redhat/rh7 -rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/redhat/rh7 # ls -l linux/ubuntu/ub10 -rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/ubuntu/ub10 # ls -l linux/linuxKernel -rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/linuxKernel

So we see that after checking the owner/group of all the files in the directory ‘linux’ and its two sub-directories ‘ubuntu’ and ‘redhat’. We issued the chown command with the ‘-R’ option to change both the owner and group. The command was successful and owner/group of all the files was changed successfully.

Lets see what happens if we issue the ‘chown’ command to recursively change the owner/group of files in a directory that is a symbolic link to some other directory.

Читайте также:  Как узнать свою видеокарту linux

Here is a symbolic link directory ‘linux_symlnk’ that links to the directory ‘linux’ (already used in example ‘9’ above) :

$ ls -l linux_symlnk lrwxrwxrwx 1 himanshu family 6 2012-05-22 22:02 linux_symlnk -> linux/

Now, lets change the owner (from himanshu to root) of this symbolic link directory recursively :

# chown -R root:friends linux_symlnk # ls -l linux_symlnk/ -rw-r--r-- 1 himanshu friends 0 2012-05-22 21:52 linuxKernel drwxr-xr-x 2 himanshu friends 4096 2012-05-22 21:52 redhat drwxr-xr-x 2 himanshu friends 4096 2012-05-22 21:52 ubuntu

In the ouput above we see that the owner of the files and directories was not changed. This is because by default the ‘chown’ command cannot traverse a symbolic link. This is the default behavior but there is also a flag ‘-P’ for this.

11. Using chown to forcefully change the owner/group of a symbolic link directory recursively

This can be achieved by using the flag -H

# chown -R -H guest:family linux_symlnk # ls -l linux_symlnk/ total 8 -rw-r--r-- 1 guest family 0 2012-05-22 21:52 linuxKernel drwxr-xr-x 2 guest family 4096 2012-05-22 21:52 redhat drwxr-xr-x 2 guest family 4096 2012-05-22 21:52 ubuntu

So we see that by using the -H flag, the owner/group of all the files/folder were changed.

12. List all the changes made by the chown command

Use the verbose option -v, which will display whether the ownership of the file was changed or retained as shown below.

# chown -v -R guest:friends linux changed ownership of `linux/redhat/rh7' to guest:friends changed ownership of `linux/redhat' retained to guest:friends ownership of `linux/redhat_sym' retained as guest:friends ownership of `linux/ubuntu_sym' retained as guest:friends changed ownership of `linux/linuxKernel' to guest:friends changed ownership of `linux/ubuntu/ub10' to guest:friends ownership of `linux/ubuntu' retained as guest:friends ownership of `linux' retained as guest:friends

Источник

Change folder permissions and ownership

I would like the user to have full rights on this folder (as well as all sub-directories and files in it):

currently owned by root. I have found numerous posts (in this forum and elsewhere) on how to do this for files but I can’t find a way to do it for whole folders.

9 Answers 9

Use chown to change ownership and chmod to change rights.

As Paweł Karpiński said, use the -R option to apply the rights for all files inside of a directory too.

Note that both these commands just work for directories too. The -R option makes them also change the permissions for all files and directories inside of the directory.

sudo chown -R username:group directory 

will change ownership (both user and group) of all files and directories inside of directory and directory itself.

sudo chown username:group directory 

will only change the permission of the folder directory but will leave the files and folders inside the directory alone.

As enzotib mentioned, you need to use sudo to change the ownership from root to yourself.

Note that if you use chown : (Note the left-out group), it will use the default group for that user.

If you want to change only the group, you can use:

BEWARE of recursively taking ownership of ANY directory. Think before you leap. Don’t be chown copypastin’ from the internet, kids. Just because you want to install a node package and it won’t let you, don’t sudo chown -R just because the fist hit from googling the error message says to. Reckless sudo chown -R -ing can kill your OS.

Читайте также:  Linux два рабочих места

It needs to be said that using the -R option only applies to files and folders that exist in the directory already. It does NOT apply to future creations. For example, if you create another folder or file as root within the folder you’ve already changed permissions on, you will have the same experiences you’re having now until you chmod\chown them again.

Make the current user own everything inside the folder (and the folder itself):

very helpful for newbies (like me) when don’t know what to type in ‘usergroup’ for sudo chown : -R /.blabla

If you prefer, this can be done with a GUI as well. You will need to open Nautilus as root to do so. Press Alt + F2 to access the «Run Applications» dialog and enter gksu nautilus

Next, browse to and right click on the folder you would like to modify. Then, select «Properties» from the context menu. You can now select the user or group that you would like to be the «Owner» of the folder as well as the permissions you would like to grant them. Finally, press «Apply Permissions to Enclosed Files» to apply the changes recursively.

Though it seems this does not always work for some operations in a deep folder tree. If it does not work use the appropriate terminal command.

Источник

How To Change Group Ownership Of Files and Directories with chgroup in Linux?

Linux provides different tools for the similar or same functionality. chgrp is the shortcuts for change group where used to change files group ownership. In this tutorial we will look different uses cases for chgrp and examples. chgrp provides similar functionality to chown

Syntax

Syntax is simple where we provide options , group name and files in a row.

Change File Group

We will start with a simple example where we will just just given file group. In this example we will change file named test to the group named root . We generally need root privileges in order to change group. In this example we will use sudo to get root privilege.

Change File Group Recursively

Another useful use case for chgrp is changing group ownership recursively. We will use -r option for this operation. We will change the group to the ismail in the directory named /home/ismail recursively.

$ sudo chgrp -R ismail /home/ismail/

Change File Group Verbosely

While changing group we may need more information about the operation. We call this as verbose mode. We can see operations verbosely with the -v option like below. In this example we will change directory named /home/ismail/.local into group named ismail recursively and print operations verbosely. We can see that if current file or folder is all ready assigned to the group we want to change we will get a message retained as

$ sudo chgrp -R -v ismail /home/ismail/.local/

Change File Group Verbosely

Verbose Output Only Group Ownership Change

We may want to see verbose output only if the group ownership of a file or directory is changed. In this situation we can use -c option. We will use previous example but only show changed files and folder. As we can see changed files will annotated with from root to ismail which means groups ownership is changed from root to ismail.

$ sudo chgrp -R -c ismail /home/ismail/.local/

Источник

Оцените статью
Adblock
detector