Bash Change Owner
chown lets you change which user and group own a file, using a simple user:group syntax.
What chown Does
Every file on a Linux system has an owning user and an owning group recorded alongside its permissions. chown (change owner) updates one or both of these, which is essential when files need to move between users, such as after copying files in as root or handing a project off to another account.
Checking and Changing an Owner
$ ls -l report.csv
-rw-r--r-- 1 root root 4096 Jul 16 08:40 report.csv
$ sudo chown alice report.csv
$ ls -l report.csv
-rw-r--r-- 1 alice root 4096 Jul 16 08:40 report.csvThe user:group Syntax
chown accepts a compact user:group syntax. Writing chown alice:staff file changes both the owning user and the owning group in one step. Omitting the group (chown alice file) changes only the user, and leading with a colon (chown :staff file) changes only the group without touching the user.
Changing User and Group Together
$ chown bob:devs project/
$ ls -ld project/
drwxr-xr-x 4 bob devs 4096 Jul 16 08:55 project/Recursive Ownership Changes
Directories often need every file inside them reassigned at once, which is where the -R (recursive) flag comes in. This is common after deploying an application, where files might be extracted or copied in as root but need to be owned by the service account that actually runs the app, such as www-data.
Recursive chown
$ sudo chown -R www-data:www-data /var/www/app
$ ls -l /var/www/app | head -n 2
total 24
-rw-r--r-- 1 www-data www-data 1830 Jul 16 09:02 index.php- Handing a deployed application's files to the web server user after copying them in as root
- Fixing ownership after extracting an archive that was created on another machine
- Sharing a project directory with a teammate by changing the owner to their account
- Copying ownership from a reference file with chown --reference=source.txt target.txt