chmod command

'chmod' changes the access permissions of the named files. Synopsis:

chmod [OPTION]... {MODE | --reference=REF_FILE} FILE...

'chmod' never changes the permissions of symbolic links, since the
'chmod' system call cannot change their permissions. This is not a
problem since the permissions of symbolic links are never used.
However, for each symbolic link listed on the command line, 'chmod'
changes the permissions of the pointed-to file. In contrast, 'chmod'
ignores symbolic links encountered during recursive directory
traversals.

If used, MODE specifies the new permissions. For details, see the
section on *Note File permissions::. If you really want MODE to have a
leading '-', you should use '--' first, e.g., 'chmod -- -w file'.
Typically, though, 'chmod a-w file' is preferable, and 'chmod -w file'
(without the '--') complains if it behaves differently from what 'chmod
a-w file' would do.

The program accepts the following options. Also see *Note Common
options::.

'-c'
'--changes'
Verbosely describe the action for each FILE whose permissions
actually changes.

'-f'
'--silent'
'--quiet'
Do not print error messages about files whose permissions cannot be
changed.

'--preserve-root'
Fail upon any attempt to recursively change the file system root,
'/'. Without '--recursive', this option has no effect. *Note
Treating / specially::.

'--no-preserve-root'
Cancel the effect of any preceding '--preserve-root' option.
*Note Treating / specially::.

'-v'
'--verbose'
Verbosely describe the action or non-action taken for every FILE.

'--reference=REF_FILE'
Change the mode of each FILE to be the same as that of REF_FILE.
*Note File permissions::. If REF_FILE is a symbolic link, do not
use the mode of the symbolic link, but rather that of the file it
refers to.

'-R'
'--recursive'
Recursively change permissions of directories and their contents.


An exit status of zero indicates success, and a nonzero value
indicates failure.

chmod by the Numbers

Up to this point, we’ve been setting the mode with letters. It turns out that you can also set the mode numerically. Here’s how it works:

  1. Write the permissions you want the file to have. To make your life easier, write the permissions grouped into sets of three letters. For example, let’s say you want file info.sh to have these permissions

    - rwx r-x r-- info.sh
    
  2. Under each letter, write a digit 1; under each dash write a digit zero. Ignore the dash at the very beginning that tells you whether it’s a file or directory. This gives you three binary numbers.

    - rwx r-x r-- info.sh
      111 101 100
    
  3. Now convert each set of three digits to a single digit using this table:

    BinaryBecomes
    0000
    0011
    0102
    0113
    BinaryBecomes
    1004
    1015
    1106
    1117

    From our example, the 111 101 100 translates to the number 754.

  4. Now use that number in a chmod command to set your desired permissions on the file:

    chmod 754 info.sh

No comments:

Post a Comment