Sobes.tech
Junior

How to change file permissions?

sobes.tech AI

Answer from AI

In Linux/Unix systems, there are three types of permissions: read, write, and execute, as well as three categories of users: owner, group, and others.

Permissions can be changed using the chmod command.

1. Numeric method:

# r=4, w=2, x=1
# Each digit corresponds to the sum of permissions for owner, group, and others respectively
# For example: 777 (rwx for all), 755 (rwx for owner, rx for group and others)
chmod 755 filename

2. Symbolic method:

# u (owner), g (group), o (others), a (all - ugo)
# +, - (add/remove permission), = (set specified permissions)
# r (read), w (write), x (execute)
chmod u+x filename         # Add execute permission for owner
chmod go-w filename        # Remove write permission for group and others
chmod a=rw- filename       # Set read/write permissions for all, remove execute

Examples:

  • Make a file executable only for the owner:
    chmod u+x filename
    
  • Set read and write permissions for the owner, read for group and others:
    chmod 644 filename
    

Additional options:

  • -R: Recursive change of permissions (for directories and their contents).
    chmod -R 755 directory_name
    

Access permissions can be viewed using the ls -l command.

# Example output of ls -l
# -rwxr-xr-x 1 user group 1024 Nov 11 10:00 filename

Where:

  • The first character - indicates it is a file. d indicates a directory.
  • The next three characters (rwx) are permissions for the owner.
  • The following three characters (r-x) are permissions for the group.
  • The last three characters (r-x) are permissions for others.