fchown - Linux
Overview
fchown is a command-line utility used to change the ownership of a file or directory, setting both the user ID (uid) and group ID (gid) simultaneously. It operates on filesystems that support ownership management.
Syntax
fchown [-R] [-h] [-v] [-f] [-a] uid:gid file...
Options/Flags
-
-R
(recursive): Change ownership of the files and subdirectories within directories recursively. -
-h
(human-readable): Print the user and group names instead of their numeric IDs. -
-v
(verbose): Print the name of each affected file. -
-f
(force): Suppress error messages and continue changing ownership even if some files cannot be modified. -
-a
(append): Instead of changing ownership, append the specified user and group to the file’s existing ownership.
Examples
-
Set the ownership of
myfile
to user ID 1000 and group ID 100:fchown 1000:100 myfile
-
Recursively change ownership of all files in the
directory
directory:fchown -R 1000:100 directory
-
Print the file names and human-readable ownership after changing:
fchown -v -h 1000:100 myfile
Common Issues
-
Permission Denied: Ensure the user running the command has sufficient permissions to change ownership of the target files.
-
Invalid User or Group: Verify that the specified user and group IDs exist on the system.
-
Read-only Filesystem: Changing ownership is not possible on read-only filesystems.
Integration
fchown can be combined with other commands, such as:
-
find
: Locate files based on criteria and change ownership accordingly. -
xargs
: Change ownership of multiple files at once by piping the output offind
.
Related Commands
- chown: Changes ownership for files or directories.
- chgrp: Changes the group ownership of a file or directory.
- ls -l: Displays file ownership attributes.