I was tasked to change the owner of a file called hello
to vincent
only if the owner was guillame
.
I tried to use chmod vincent:guillame hello
but this is not working. Please, I need help to solve the task.
Asked
Active
Viewed 304 times
2

Kusalananda
- 333,661

Westfiree
- 21
2 Answers
2
chown
is the command used to change ownership of files, not chmod
.
find hello -user guillame -exec chown vincent {} \;
This uses find
with the file hello
as the "search path". If the hello
file's owner is guillame
, find
will execute chown vincent
with the file as its next argument, changing the file's ownership to vincent
. If the owner of the file is not guillame
, nothing will happen.
This command ought to be portable to any Unix system.
Note that this assumes that the current user has privileges that allow changing the file ownership. You may have to run either chown
or find
itself as root using sudo
or doas
or whatever other way you usually run a utility with escalated privileges on your system.

Kusalananda
- 333,661
-1
For just one file called hello
:
/usr/bin/test "$(stat --format="%U" hello)" = "guillame" && sudo chown vincent foo

Pablo A
- 2,712
-
Note that on Alpine Linux (or any Busybox system), you'd use
stat -c %U filename
(which incidentally also works on system where--format
is valid); on OpenBSD, FreeBSD, NetBSD, and macOS, it'sstat -f %Su filename
. – Kusalananda Aug 31 '23 at 22:36
stat --format="%N" file
. Readman stat chmod chown
. – waltinator Aug 31 '23 at 22:09