Skip to content

Latest commit

 

History

History
120 lines (78 loc) · 2.83 KB

File metadata and controls

120 lines (78 loc) · 2.83 KB

chown

The chown (change owner) command is used to change the ownership of files and directories in a Linux filesystem.

Basic Usage

  • Syntax:

    chown [options] [owner][:group] file...
  • Examples:

    chown alice file.txt
    • Changes the owner of file.txt to alice.
    chown alice:developers project/
    • Changes the owner of the project/ directory to alice and the group to developers.
    chown :developers file.txt
    • Changes the group ownership of file.txt to developers.
    sudo chown -R bob /var/www/
    • Recursively changes the ownership of all files and directories in /var/www/ to bob.

Common Options

  • -R or --recursive:

    • Recursively changes ownership for all files and directories within the specified directory.
    chown -R alice:developers /home/alice
    • Applies ownership changes to all files and subdirectories in /home/alice.
  • --reference=[rfile]:

    • Sets ownership of a file or directory to match the ownership of another file.
    chown --reference=reference.txt file.txt
    • Sets the ownership of file.txt to match that of reference.txt.
  • -h or --no-dereference:

    • Changes the ownership of symbolic links themselves, rather than the files they point to.
    chown -h alice symlink
    • Changes the owner of the symlink symbolic link to alice.
  • -v or --verbose:

    • Verbosely displays the ownership changes being made.
    chown -v alice file.txt
    • Outputs the ownership change for file.txt to alice.
  • --help:

    • Displays help information about the chown command.
    chown --help
    • Shows usage information and options.

Quick Tips

  • Changing Owner and Group Together:

    • Specify both the owner and group separated by a colon.
    chown bob:staff file.txt
    • Changes the owner to bob and the group to staff.
  • Skipping Group Change:

    • Use chown owner: file to change only the owner without affecting the group.
    chown alice: file.txt
    • Changes the owner to alice and leaves the group unchanged.
  • Using Wildcards:

    • Apply ownership changes to multiple files using wildcards.
    chown bob *.txt
    • Changes the owner of all .txt files in the current directory to bob.
  • Recursive Ownership Changes:

    • Be cautious with the -R option, especially when running as root, as it can modify ownership of many files and directories.

Summary

The chown command is a powerful tool for managing file and directory ownership in Linux. It allows you to change the owner, group, or both, of files and directories. Understanding how to use chown effectively is crucial for maintaining proper access control and security on your system.