Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Changing Permissions

Introduction

File permissions are a fundamental aspect of file system security. They determine who can read, write, or execute a file. In this tutorial, we will explore how to change file permissions using the command line.

Understanding Permissions

Permissions are typically represented by a combination of letters and symbols. The most common format is:

rwxr-xr--

Where:

  • r stands for read
  • w stands for write
  • x stands for execute

These permissions are divided into three groups:

  • Owner
  • Group
  • Others

Changing Permissions with chmod

The chmod command is used to change file permissions. It can be used in symbolic mode or numeric mode.

Symbolic Mode

Symbolic mode uses letters to represent the permission changes. The format is:

chmod who=permissions filename

Where:

  • who can be u (user/owner), g (group), o (others), or a (all)
  • permissions can be r, w, and x

Example:

chmod u+rwx myfile.txt

Numeric Mode

Numeric mode uses a three-digit number to set permissions. Each digit represents the permissions for the owner, group, and others.

The digits are:

  • 4 - read (r)
  • 2 - write (w)
  • 1 - execute (x)

To set multiple permissions, sum the values. For example, 7 means read (4) + write (2) + execute (1).

Example:

chmod 755 myfile.txt

This sets the permissions to rwxr-xr-x.

Examples of Changing Permissions

Granting Execute Permission

To grant execute permission to the owner of the file:

chmod u+x myscript.sh
ls -l myscript.sh
-rwxr--r-- 1 user group 1234 Jan 1 12:34 myscript.sh

Removing Write Permission

To remove write permission from the group for a file:

chmod g-w myfile.txt
ls -l myfile.txt
-rw-r--r-- 1 user group 5678 Jan 1 12:34 myfile.txt

Setting Permissions for All Users

To set the permissions to read and execute for everyone:

chmod a+rx myprogram
ls -l myprogram
-r-xr-xr-x 1 user group 91011 Jan 1 12:34 myprogram

Conclusion

Understanding and changing file permissions is essential for system security and user access management. By using the chmod command in both symbolic and numeric modes, you can effectively manage file permissions on your system.