
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Chown Command - Change Owner of File in Linux
As a Linux user, you may often come across situations where you need to change the owner of a file or directory. This is where the chown command comes in handy. The chown command, short for "change owner," allows you to change the ownership of a file or directory to a specific user or group. In this article, we will discuss the chown command in detail, its syntax, options, and some examples of how to use it effectively.
Understanding File Ownership in Linux
Before we dive into the chown command, let's take a moment to understand file ownership in Linux. In Linux, every file and directory is associated with an owner and a group. The owner is the user who created the file or directory, and the group is a collection of users who have specific permissions to access that file or directory. Every user in Linux is a member of at least one group.
By default, when a user creates a file or directory, they become the owner of that file or directory. The user's primary group also becomes the group associated with the file or directory. However, you can change the owner or group of a file or directory using the chown command.
Syntax of the chown Command
The basic syntax of the chown command is as follows ?
chown [OPTIONS] [USER][:[GROUP]] FILENAME
Here, OPTIONS are the various flags that you can use with the chown command. USER is the new owner of the file or directory, and GROUP is the new group. The GROUP parameter is optional. If you don't specify a group, the group associated with the file or directory will not be changed.
FILENAME is the name of the file or directory whose owner and group you want to change. You can specify multiple file names separated by spaces.
Options for the chown Command
The chown command comes with several options that you can use to customize its behavior. Here are some of the most commonly used options ?
-R or --recursive ? Recursively change the ownership of all files and directories under the specified directory.
-v or --verbose ? Display a message for each file or directory whose ownership is changed.
-c or --changes ? Display a message only if the ownership of a file or directory is changed.
-f or --quiet or --silent ? Suppress error messages.
-h or --no-dereference ? Do not follow symbolic links.
-L or --dereference ? Follow symbolic links.
Examples of Using the chown Command
Now that we have a basic understanding of the chown command and its syntax and options, let's take a look at some examples of how to use it.
Example 1: Changing the Owner of a File
To change the owner of a file, you can use the chown command followed by the name of the new owner and the name of the file. For example, to change the owner of a file called file.txt to a user called john, you would use the following command ?
sudo chown john file.txt
If you want to change the group as well, you can include it after the username separated by a colon. For example, to change both the owner and group of file.txt to a user called john and a group called devs, you would use the following command ?
sudo chown john:devs file.txt
Example 2: Changing the Owner of a Directory Recursively
To change the ownership of all files and directories under a specific directory recursively, you can use the -R option with the chown command. For example, to change the ownership of all files and directories under a directory called myfolder to a user called jane and a group called admins, you would use the following command ?
sudo chown -R jane:admins myfolder/
This command will change the ownership of all files and directories under myfolder/, including any subdirectories.
Example 3: Displaying a Message for Each Changed File
If you want to display a message for each file or directory whose ownership is changed, you can use the -v option with the chown command. For example, the following command will change the ownership of a file called file.txt to a user called john and display a message for the change:
sudo chown -v john file.txt
This will display a message like the following ?
changed ownership of 'file.txt' from user1 to john
Example 4: Suppressing Error Messages
If you want to suppress error messages, you can use the -f or --quiet or --silent option with the chown command. For example, the following command will change the ownership of a file called file.txt to a user called john and suppress any error messages ?
sudo chown -f john file.txt
Example 5: Following Symbolic Links
By default, the chown command does not follow symbolic links. However, if you want to change the ownership of a symbolic link and follow it to the target file or directory, you can use the -L or --dereference option with the chown command. For example, the following command will change the ownership of a symbolic link called link.txt and follow it to the target file ?
sudo chown -L john link.txt
Conclusion
The chown command is a powerful tool for changing the ownership of files and directories in Linux. It allows you to change the owner and group of a file or directory to a specific user or group. You can use various options with the chown command to customize its behavior, such as recursively changing ownership, displaying messages for changed files, suppressing error messages, and following symbolic links. By using the chown command effectively, you can manage file ownership in Linux with ease.