Bash 파일 퍼미션 변경

When working with files in Linux, permissions play a crucial role in determining who can read, write, or execute them. In Bash, you can easily change file permissions using a set of convenient commands. In this blog post, we will explore how to modify file permissions in Linux using the Bash shell.

File Permissions in Linux

Before we dive into changing file permissions, let’s understand the basic concept of permissions in Linux. Each file in Linux has three types of permissions:

  1. Read (r): If a user has read permission on a file, they can view the contents of the file.
  2. Write (w): Write permission allows a user to modify or update the contents of a file.
  3. Execute (x): Execute permission allows a user to run the file if it is a script or a program.

These permissions can be assigned to three different entities:

  1. User (u): The owner of the file.
  2. Group (g): A group of users who share common permissions.
  3. Others (o): All other users who are not the owner or part of the group.

Changing File Permissions using Bash

To change file permissions using Bash, we use the chmod command. The syntax for chmod is as follows:

chmod [options] mode filename

There are two common ways to specify the permissions using the mode parameter:

  1. Numeric mode: The numeric mode represents permissions using three digits, with each digit representing the permission for the user, group, and others. Each digit is calculated by adding the following values:
    • Read (r): 4
    • Write (w): 2
    • Execute (x): 1

    For example, to give read and write permissions to the user, write and execute permissions to the group, and read-only permissions to others, we can use the numeric mode chmod 764 filename.

  2. Symbolic mode: The symbolic mode uses symbols to specify the permissions. It consists of three parts - the target (u, g, o, or a for all), the operator (+, -, or =), and the permission (r, w, or x).

    For example, to add read and write permission to the group, we can use the symbolic mode chmod g+rw filename.

Examples

To illustrate the usage of chmod in Bash, let’s consider some examples:

  1. Changing User Permission: To give execute permission to the user for a file named script.sh, we can use the following command:

    chmod u+x script.sh
    
  2. Changing Group Permission: To revoke write permission from the group for a file named data.txt, we can use the following command:

    chmod g-w data.txt
    
  3. Changing Other’s Permission: To grant read and execute permission to others for a directory named public, we can use the following command:

    chmod o+rx public
    
  4. Changing Permission Recursively: To modify the permissions of a directory and all its contents recursively, use the -R option for chmod. For example, to grant read and write permissions to the group for a directory named docs and its subdirectories and files, we can use the following command:

    chmod -R g+rw docs
    

Conclusion

Understanding and managing file permissions is essential when working with files in Linux. With the chmod command in Bash, you can easily modify file permissions to control access to your files and directories. By using the appropriate numerical or symbolic modes, you can fine-tune the permissions based on your requirements.