Which command you will use to change the owner and group of test folder to who?

Utilize the “User” for the username or replace it using the user ID, also known as UID. Add the group name in the “Group” part of the GID (group ID). At the end of the chown command, add files or directories for which you want to change the ownership.

Changing the owner of a directory

We have created a separate user named “utest” and a group “ugroup” for demonstrating the examples. To change the ownership of any directory, utilize the chown command with the username and path of the directory.

$ sudo chown utest ./samplefolder/

Now, write out the “ls” command and check if your directory ownership is updated or not.

$ ls -l -d ./samplefolder/

Which command you will use to change the owner and group of test folder to who?

Changing the ownership of all sub-directories

Utilize the “-R” option of the chown command for changing the owners of all the files or folders present in a directory.

$ sudo chown -R utest ./samplefolder/

List out the directory content to view the results.

$ ls -l ./samplefolder/

Which command you will use to change the owner and group of test folder to who?

Changing the owner of a file

First of all, list out the file content to know about its ownership.

$ ls -l samplefile.txt

Which command you will use to change the owner and group of test folder to who?

After that, specify the name of the user who you want to be the new owner of this file. In our case, we have chosen “utest” to avail the ownership of “samplefile.txt.”

$ sudo chown utest samplefile.txt

Which command you will use to change the owner and group of test folder to who?

Again, verify the updated ownership changes.

$ ls -l samplefile.txt

Which command you will use to change the owner and group of test folder to who?

You can also use the “User ID” or “UID” of any user for this purpose. For that, retrieve the user ID of the new user by utilizing the “id” command with the “-u” option.

$ id -u utest

Which command you will use to change the owner and group of test folder to who?

Add the User ID instead of the username to make the ownership changes.

$ sudo chown 1001 samplefile.txt

Which command you will use to change the owner and group of test folder to who?

Changing the owners of multiple files

Specify the file names at the end of the chown command to change the ownership of multiple files at once.

$ sudo chown utest samplefile.txt samplefile2.txt

Which command you will use to change the owner and group of test folder to who?

Write out the below-given command to confirm the changes.

$ sudo ls -al samplefile.txt samplefile2.txt

Which command you will use to change the owner and group of test folder to who?

Changing the owner of directory and file at once

Follow the below-given method for changing the owners of the file and directory instantly.

$ sudo chown utest samplefile.txt test

This command will make “utest” the new owner of the “test directory” and the “samplefile.txt.”

Which command you will use to change the owner and group of test folder to who?

Changing the owner using wildcards

Wildcards are used to select a specific file group according to the given pattern. The chown command will then change the owner of the files after retrieving them from the wildcard execution.

$ sudo chown utest s*.*
$ sudo ls -l  s*.*

Which command you will use to change the owner and group of test folder to who?

Conclusion

For data security concerns, you may want to specify the ownership of files and directories. In Linux, we use the chown command-line utility for changing the ownership of directories or files. You can also utilize this command for changing directories and multiple file ownership at once. In this post, all of these statements are justified by providing practical examples.

We know that Linux is a multiuser operating system so every file or directory belongs to an owner and group.

To change ownership of files or directories we use chown command in the Linux system. This command is also available in the IBM i operating system. The chgrp command is also used to change only the group ownership of the file in the Linux system.

Syntax

The general syntax of the chown command is as follows

chown [OPTION]... [OWNER] [: [GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...

A brief description of options available in the chown command -

Sr.No.option & Description1-c, --changes
Gives a diagnosis for all the files that actually changed.
2.-f, --silent, --quite
It suppresses most of the error messages.
3-v, --verbose
Give a diagnosis for all the processed files.
4-R, --recursive
It changes files and directories recursively.
5--help
Displays a help message and then exits.
6--version
It gives info about the version and then exits.

Checking the ownership

To check the ownership of a file in the Linux system, we use the ls -l command as shown below.

$ ls -l 

Changing the ownership of a file

To change ownership of a file in the Linux system we need to administrative permission or sudo privilege.

The general syntax for changing ownership of a file is as follows:

$ sudo chown  

Here, we will change the ownership of a file ‘file.txt’ Vikash to Gautam using the chown command.

First, we will check the ownership of ‘file.txt’ using the below command.

vikash@tutorialspoint:~/shadow$ ls -l file.txt
-rw-rw-r-- 1 vikash vikash 34 Jan 11 20:59 file.txt
vikash@tutorialspoint:~/shadow$

We can see that the owner of ‘file.txt’ is vikash and the group ownership of the ‘file.txt’ is vikash. To change the ownership, we will execute command as shown below.

vikash@tutorialspoint:~/shadow$ sudo chown gautam file.txt
[sudo] password for vikash:
vikash@tutorialspoint:~/shadow$

After changing the ownership of the file, we will again check again the ownership of files to ensure that ownership is changed or not.

vikash@tutorialspoint:~/shadow$ ls -l file.txt
-rw-rw-r-- 1 gautam vikash 34 Jan 11 20:59 file.txt
vikash@tutorialspoint:~/shadow$

To check more information and available options in the chown command, we use the --help option with the chown command as follows:

$ chown --help

Conclusion: In this article, we learned to change the ownership of files using the chown command in the Linux operating system with available options and suitable examples. To change only the group ownership of the file, we use the chgrp command in the Linux system.

Which command is used to change owner files and directories?

The chown command is used to change the file owner or group.

Which command would give the owner and group?

Check Owner and Group Before Making Changes The chown command --from option lets you verify the current owner and group and then apply changes.

Which of the following commands can be used to change both the owner and group settings of a file?

To change both the owner and the group of a file use the chown command followed by the new owner and group separated by a colon ( : ) with no intervening spaces and the target file.

What is the command for change ownership and permissions?

You can change the ownership of a file or folder using the chown command.