linux chmod/chown syntax and chmod/chown examples

The previous article introduced linux file properties and permissions.

This article introduces the Linux Chmod command and chown command and their use.

linux chmod command

Chmod change file modes or Access Control Lists

Linux / Unix file call permission is divided into three levels: file owner, group and others.

Chmod can be used to control how files are called by others.

syntax

chmod [-fv] [-R [-H | -L | -P]] mode file ...

options

  • -v, –verbose
    output a diagnostic for every file processed
  • -R, –recursive
    change files and directories recursively

mode

The symbolic mode is described by the following grammar:

mode         ::= clause [, clause ...] 
clause       ::= [who ...] [action ...] 
action action       ::= op [perm ...] 
who          ::= a | u | g | o 
op           ::= + | - | = 
perm         ::= r | s | t | w | x | X | u | g | o

who

Reference   Class     Description
 u          owner      file's owner
 g          group      users who are members of
                       the file's group
 o          others     users who are neither the
                       file's owner nor members of 
                       the file's group
 a          all       All three of the above, same as ugo

perm

r       The read bits.
s       The set-user-ID-on-execution 
        and set-group-ID-on-execution bits.
t       The sticky bit.
w       The write bits.
x       The execute/search bits.
X       The execute/search bits if the file
        is a directory or any of the execute/search 
        bits are set in the original (unmodified) 
        mode. Operations with the perm symbol X'' 
        are only meaningful in conjunction with the 
        op symbol+'', and are ignored in all other cases.
u       The user permission bits in the original mode
        of the file.
g       The group permission bits in the original mode 
        of the file.
o       The other permission bits in the original mode 
        of the file.

op

Operator  Description
+         Adds the specified modes to the
          specified classes
-         Removes the specified modes from 
          the specified classes 
=         The modes specified are to be made
          the exact modes for the specified 
          classes

examples

  • test file gives execution permission to all users
$ sudo chmod ugo+x test 
  • groups and others give file write permission
➜ chmod 666 test 

  • chmod 777

Give all users access to read, write and execute files

➜ chmod 777 test

  • chmod 755

File owner read and write execution permission, readable by group and other users, execution permission

  • chmod a+r

a is equivalent to “ugo”

User, group and other users read permissions.

official example

  • chmod 644
    make a file readable by anyone and writable by the owner only.
  • chmod go-w
    deny write permission to group and others.
  • chmod =rw,+X
    set the read and write permissions to the usual defaults, but retain any execute permissions that are currently set.
  • chmod +X
    make a directory or file searchable/executable by everyone if it is already searchable/executable by anyone.
  • chmod 755
    chmod u=rwx,go=rx
    chmod u=rwx,go=u-w
    make a file readable/executable by everyone and writable by the owner only.
  • chmod go=
    clear all mode bits for group and others.
  • chmod g=u-w
    set the group bits equal to the user bits, but clear the group write bit.

linux chown command

Chown change file owner and group.

This command can only be used by root.

syntax

chown [-fhnv] [-R [-H | -L | -P]] owner[:group] file ...

options

  • -R, –recursive
    operate on files and directories recursively
  • -v, –verbose
    output a diagnostic for every file processed
  • -h, –no-dereference
    affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)

examples

  • change the user of the file
$ sudo chown nobody test
  • change file group and user
$ sudo chown www-data:www-data test 

  • Change the owner of /folder and subfiles to “root”.
$ sudo chown -Rh root folder/

Recommended Posts:

Linux common commands tutorial and use examples
GNU sed syntax and sed examples
Awk tutorial: awk syntax and awk examples
linux iostat syntax and iostat examples
linux grep command and grep examples

Add a Comment

Your email address will not be published.