chmod to change permissions of specific user
You can use next solution:
- change the ownership of the file:
chown user1 /path/to/file
- change permission for the owner, group and other:
chmod 644 /path/to/file
This will give rw
to user1
and r
to user2
For directories you must add x
to give the option to the user to change in this directory:
chmod 755 /path/to/directory
Be careful with -R
because this will change also the subdirectories
To automate the work you can use something like. Be very carefull for the start directory because those commands can change permissions of files you do not want to touch
find /path/to/file -type f -exec chmod 644 {} \;
for files
find /path/to/dir -type d -exec chmod 755 {} \;
for directories
Related videos on Youtube
Legatio
Updated on September 18, 2022Comments
-
Legatio almost 2 years
I have two users, user1 and user2. I also have a file in
/path/to/file
. user1 should be able to have read-write access, while user2 should only have read access.I know that I can change permissions with
chmod u=r /path/to/file
to read-only, however this changes the permissions for everyone? When executing the command as user1 the access changes for user1 and user2 as well. I haven't found a option to specify a user. Is this something where I'd have to use the groups? Orchown
? How would I go about doing this?Is this also possible to do for a whole directory full of files? If there is a dir
/path/to/dir
that contains n files and m subdirs, to change the permissions of every file and every file in the subdirs? -
Legatio about 3 yearsI just edited the question: is this possible for whole dirs as well? I just did
chown user1 ./
andchmod -R 644 ./
and now I don't have any access to the dir, even as user1. -
Atul Vekariya about 3 years@Legatio, please check my edited answer
-
Legatio about 3 yearsthanks now I can access the dir again. Is it possible to add x to the dir and subdirs but not the files in those dirs? I would want user2 to only read the files and not execute them.
-
Atul Vekariya about 3 years@Legatio, yes, thats possible, see my edited answer
-
Legatio about 3 yearshow would I use the 2
find
commands for the concrete example I added to my question? -
Atul Vekariya about 3 years@Legatio, answer edited