Categories
shellinfo tips

CHOWN – Manage file ownership in Linux

The chown command in Linux is used to change the owner and group of files and directories. Here’s the basic syntax:

chown [OPTION]... [OWNER][:[GROUP]] FILE...

Change the owner of a file: To change the owner of a file, you can use the chown command followed by the new owner’s username and the file name. For example, to change the owner of a file named file.txt to a user named john, you would use:

chown john file.txt

Change the owner of a directory: Similarly, to change the owner of a directory and all the files within it, you can use the -R (or –recursive option. For example, to change the owner of a directory named dir to john, you would use:

chown -R john dir


Change the owner and group of a file: To change both the owner and the group of a file, you can use the chown command followed by the new owner’s username, a colon, the new group’s name, and the file name. For example, to change the owner of file.txt to john and the group to admin, you would use:

chown john:admin file.txt

Change the owner of a symbolic link: By default, chown changes the owner of the file or directory that a symbolic link points to. If you want to change the owner of the symbolic link itself, you can use the -h (or –no-dereference) option. For example, to change the owner of a symbolic link named link to john, you would use:

chown -h john link

Remember that you need to have the necessary permissions to change the owner of a file or directory. Typically, this means you need to be the root user or use the sudo command.