Set permissions all files in directory linux

How to set default file permissions for all folders/files in a directory?

I want to set a folder such that anything created within it (directories, files) inherit default permissions and group. Lets call the group «media». And also, the folders/files created within the directory should have g+rw automatically.

umask does relate to permissions but I do not believe it does anything with setting a default group that is not the user him/herself.

5 Answers 5

setfacl -d -m g::rwx / setfacl -d -m o::rx /
# file: ..// # owner: # group: media # flags: -s- user::rwx group::rwx other::r-x default:user::rwx default:group::rwx default:other::r-x 

g+s will ensure that new content in the directory will inherit the group ownership. setfacl only changes the chmod, in your case sets the permission to o=rx

Note that ACL must be enabled (included as one of the mount options for the mounted file system) for the file permissions to be inherited.

You might want to consider using ‘X’ instead so it will only set execute permission on directories not files setfacl -d -m g::rwX /

This is an addition to Chris’ answer, it’s based on my experience on my Arch Linux rig.

Using the default switch ( -d ) and the modify switch ( -m ) will only modify the default permissions but leave the existing ones intact:

If you want to change folder’s entire permission structure including the existing ones (you’ll have to do an extra line and make it recursive with -R ):

# Gives group read,write,exec permissions for currently existing files and # folders, recursively. setfacl -R -m g::rwx /home/limited.users/directory # Revokes read and write permission for everyone else in existing folder and # subfolders. setfacl -R -m o::x /home/limited.users/directory # Gives group rwx permissions by default, recursively. setfacl -R -d -m g::rwx /home/limited.users/directory # Revokes read, write and execute permissions for everyone else. setfacl -R -d -m o::--- /home/limited.users/directory 

(CREDIT to markdwite in comments for the syntax of the revoke all privileges line)

Читайте также:  Arm xilinx linux gnueabi gcc

Источник

Change all files and folders permissions of a directory to 644/755

If someone (@animuson) would be so kind to explain me, why this chmod question is off-topic and all others (14,438 results) here aren’t.

Little late, but this one command will also do the accepted answer in one shot: chmod -R a=r,a+X,u+w /your/path

Good question, doesn’t deserve closing. These should rather be moved to a stackoverflow sub site than closed.

@hugoderhungrige it means go ask it on a Server site like: http://superuser.com 😛 but this question helped me here, thanks.

7 Answers 7

One approach could be using find:

for directories

find /desired_location -type d -print0 | xargs -0 chmod 0755 

for files

find /desired_location -type f -print0 | xargs -0 chmod 0644 

just for someone else like me, this doesn’t work instead try sudo find /your/location -type f -exec chmod 644 <> \; for files and sudo find /your/location -type d -exec chmod 755 <> \; for directories

I ran the original solution and it messed up my permissions on files and directories. watch out! the solution on the comment worked, thanks!

What if I want only the subfolder to be chmod 755 when specifying the desired_location ? Because this also will make the parent folder 755

chmod -R u+rwX,go+rX,go-w /path/to/dir 

to ch ange file mod es -R ecursively by giving:

  • u ser: r ead, w rite and e X ecute permissions,
  • g roup and o ther users: r ead and e X ecute permissions, but not -w rite permission.

Please note that X will make a directory executable, but not a file, unless it’s already searchable/executable.

+X — make a directory or file searchable/executable by everyone if it is already searchable/executable by anyone.

Please check man chmod for more details.

Читайте также:  Astra linux samba gui

This answer, while neat, does have a problem: a file that is executable before running the command will be executable afterwards. See the answer of @JohnAllsup for a command that does not have this flaw.

The shortest one I could come up with is:

which works on GNU/Linux, and I believe on Posix in general (from my reading of: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/chmod.html).

  1. Set file/directory to r__r__r__ (0444)
  2. Add w for owner, to get rw_r__r__ (0644)
  3. Set execute for all if a directory (0755 for dir, 0644 for file).

Importantly, the step 1 permission clears all execute bits, so step 3 only adds back execute bits for directories (never files). In addition, all three steps happen before a directory is recursed into (so this is not equivalent to e.g.

chmod -R a=r /foo chmod -R u+w /foo chmod -R a+X /foo 

since the a=r removes x from directories, so then chmod can’t recurse into them.)

Источник

Оцените статью
Adblock
detector