# chown — Change File Owner and Group

> Change file owner and group with chown — fix ownership after copying, set permissions for web servers and services, and apply changes recursively.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/chown/

<!-- PROSE:intro -->
`chown` changes who owns a file or directory – its user and group. You reach for it constantly: ownership is often wrong after copying or extracting an archive, a web server such as `www-data` needs access to its files, or you want to reclaim control over a project directory. The `chown user:group file` syntax sets both at once, while a leading colon (`:group`) changes the group only. Be careful with the recursive `-R` flag: it rewrites ownership across entire directory trees, and aimed at system paths like `/` it can render your installation unusable. Almost every `chown` call needs `sudo`, because only root may change the owner of files it does not own.
<!-- PROSE:intro:end -->

## Change Owner

`chown <user> <file>` — Change the owner of a file.

```bash
sudo chown www-data index.html
```

`chown <user>:<group> <file>` — Change both owner and group.

```bash
sudo chown www-data:www-data index.html
```

`chown :<group> <file>` — Change only the group (shorthand).

```bash
sudo chown :developers project/
```

`chown <user> <file1> <file2>` — Change owner of multiple files.

```bash
sudo chown www-data *.html *.php
```

## Recursive

`chown -R <user>:<group> <dir>` — Change ownership recursively for a directory.

```bash
sudo chown -R www-data:www-data /var/www/html/
```

`chown -R --preserve-root <user> <dir>` — Recursive with protection against operating on /.

```bash
sudo chown -R --preserve-root deploy:deploy /opt/app/
```

## Options

`chown -v <user> <file>` — Verbose: report each change.

```bash
sudo chown -v www-data *.php
```

`chown -c <user> <file>` — Report only when a change is made.

```bash
sudo chown -c www-data *.php
```

`chown --reference=<ref> <file>` — Set ownership to match a reference file.

```bash
sudo chown --reference=index.html newfile.html
```

`chown -h <user> <symlink>` — Change ownership of a symlink (not its target).

```bash
sudo chown -h www-data /var/www/current
```

## Common Patterns

`chown -R www-data:www-data /var/www/` — Set web server ownership for a website directory.

```bash
sudo chown -R www-data:www-data /var/www/mysite/
```

`find <dir> -type f -exec chown <user> {} +` — Change owner of files only (not directories).

```bash
sudo find /var/www -type f -exec chown www-data {} +
```

`find <dir> -type d -exec chown <user> {} +` — Change owner of directories only.

```bash
sudo find /var/www -type d -exec chown www-data {} +
```

`chown $(id -u):$(id -g) <file>` — Set file ownership to current user.

```bash
sudo chown $(id -u):$(id -g) downloaded-file.tar.gz
```

`chown -R $USER:$USER <dir>` — Reclaim ownership of a directory for current user.

```bash
sudo chown -R $USER:$USER ~/projects/
```

<!-- PROSE:outro -->
## Conclusion

`chown` is quick to reach for whenever a file's ownership is wrong – usually `chown user:group file` is enough, often combined with `find -type f`/`-type d` to treat files and directories separately. Treat the recursive `-R` variant with respect: it reaches into entire directory trees, and a typo in the path – especially in system areas like `/`, `/etc` or `/usr` – can break services or render the system unusable. Check the target path before you confirm, use `--preserve-root` as a safety net, and reach for `-v`/`-c` when in doubt to see exactly which changes happen. Closely related is `chmod`, which sets access permissions, while `ls -l` and `stat` show you the current ownership.

## Further Reading

- [GNU Coreutils: chown invocation](https://www.gnu.org/software/coreutils/manual/html_node/chown-invocation.html) – official reference for every option of GNU chown
- [Wikipedia: chown](https://en.wikipedia.org/wiki/Chown) – background on the Unix concept of file ownership
<!-- PROSE:outro:end -->

## Related Commands

- [chmod](https://www.jpkc.com/db/en/cheatsheets/files-text/chmod/) – set access permissions on files and directories
- [ls](https://www.jpkc.com/db/en/cheatsheets/files-text/ls/) – list directory contents including owner and permissions
- [stat](https://www.jpkc.com/db/en/cheatsheets/files-text/stat/) – detailed file information including ownership

