Skip to main content

How to Change File Permissions

• 2 min read
bash chmod file permissions security unix permissions

Quick Answer: Change File Permissions in Bash

To change file permissions, use chmod +x script.sh for symbolic mode (adds execute for all) or chmod 755 script.sh for numeric mode (sets exact permissions). Numeric mode is more precise for scripts; symbolic mode is easier to remember. Most commonly: chmod +x for executability and chmod 644 for readable files.

Quick Comparison: Permission Change Methods

MethodFormatPrecisionBest ForReadability
Symbolic (+/-x)chmod +x fileRelativeAdding/removing permsExcellent
Numeric (755)chmod 755 fileAbsoluteSetting exact permsGood
Recursive (-R)chmod -R +x dir/Both modesDirectories & contentsGood
Reference filechmod --reference=ref fileExact copyMatching permissionsPoor

Bottom line: Use symbolic mode for simple changes (+x, -w); use numeric for setting exact permissions.


Changing File Permissions with chmod

The chmod command modifies file permissions. You can use either symbolic mode (like +x to add execute) or numeric mode (like 755) to set exact permissions. Understanding both is important for shell scripting.

Method 1: Symbolic Mode (The Simple Way)

Symbolic mode is easier to read and remember. It uses relative changes to add or remove specific permissions:

# Make file executable
chmod +x script.sh

# Remove execute permission
chmod -x script.sh

# Make file writable
chmod +w file.txt

# Make file read-only
chmod -w file.txt

# Remove all permissions
chmod -rwx file.txt

# Add all permissions
chmod +rwx file.txt

When to Use Symbolic Mode

Use symbolic mode when:

  • You’re adding or removing one permission (most common case)
  • You want human-readable, self-documenting code
  • You don’t want to change other permissions
  • You’re working interactively at the command line
  • The current permissions don’t matter, only the change

Common Symbolic Permissions

#!/bin/bash

# Make script executable by owner
chmod u+x script.sh

# Make readable by all
chmod a+r file.txt

# Make writable only by owner
chmod u+w,g-w,o-w file.txt

# Remove write for group and others
chmod go-w file.txt

# Make readable and executable by all
chmod a+rx script.sh

Numeric Mode: Precise Control

Numeric mode sets exact permissions (read=4, write=2, execute=1):

# 755 = rwxr-xr-x (owner can do everything, others can read/execute)
chmod 755 script.sh

# 644 = rw-r--r-- (owner can read/write, others can read)
chmod 644 file.txt

# 700 = rwx------ (only owner can access)
chmod 700 private.txt

# 777 = rwxrwxrwx (everyone can do everything - rarely needed)
chmod 777 shared.txt

Symbolic Breakdown

Understanding symbolic notation: u, g, o, a for user/group/other/all:

#!/bin/bash

# u = user (owner)
chmod u+x script.sh     # Owner can execute

# g = group
chmod g+r file.txt      # Group can read

# o = others
chmod o-w file.txt      # Others cannot write

# a = all (user, group, other)
chmod a+r file.txt      # Everyone can read

# Combine them
chmod u+rw,g+r,o-w file.txt  # Owner: read/write, Group: read, Others: no write

Real-World Example: Deploy Script

#!/bin/bash

deploy_app() {
  local app_dir="/opt/myapp"

  # Create directories with appropriate permissions
  mkdir -p "$app_dir"/{bin,lib,data,logs}

  # Copy files
  cp -r source/* "$app_dir"

  # Set permissions
  chmod 755 "$app_dir"                  # Directory: owner full, others can enter
  chmod 755 "$app_dir/bin"              # Executables dir
  chmod 755 "$app_dir/bin"/*            # All scripts executable
  chmod 644 "$app_dir/lib"/*            # Library files readable
  chmod 700 "$app_dir/data"             # Data dir private
  chmod 700 "$app_dir/logs"             # Log dir private

  echo "Application deployed with correct permissions"
}

deploy_app

Recursive Changes

Change permissions on directories and contents:

#!/bin/bash

# Recursively make all files readable
chmod -R a+r /home/user/documents

# Recursively make all scripts executable
chmod -R a+x /usr/local/bin

# Recursively restrict directory
chmod -R go-rwx /home/user/.ssh

# Be careful with -R!

Practical Example: Secure File Handling

#!/bin/bash

create_secure_file() {
  local filename="$1"
  local content="$2"

  # Create file with restricted permissions (owner only)
  touch "$filename"
  chmod 600 "$filename"

  # Write content
  echo "$content" > "$filename"

  # Verify permissions
  ls -l "$filename"
}

create_secure_file "/tmp/secret.txt" "password123"
# Output: -rw------- (only owner can read/write)

Understanding Common Permissions

# 755 - Standard for executables and directories
# u=rwx (7), g=rx (5), o=rx (5)
chmod 755 script.sh

# 644 - Standard for files
# u=rw (6), g=r (4), o=r (4)
chmod 644 file.txt

# 600 - Private file
# u=rw (6), g=0, o=0
chmod 600 secret.txt

# 700 - Private directory
# u=rwx (7), g=0, o=0
chmod 700 private_dir

# 777 - Everyone everything (rarely needed, security risk)
chmod 777 shared_file

Batch Permission Changes

#!/bin/bash

# Make all shell scripts in directory executable
for script in /home/user/scripts/*.sh; do
  [ -f "$script" ] && chmod +x "$script"
done

# Make all files readable
chmod -R a+r /var/www/html

# Make log directory writable by group
chmod g+w /var/log/myapp

Restoring Permissions

If you accidentally change permissions:

#!/bin/bash

# Restore common permissions
restore_permissions() {
  local dir="$1"

  # Fix regular files (644)
  find "$dir" -type f ! -path '*/\.*' -exec chmod 644 {} \;

  # Fix scripts (755)
  find "$dir" -type f -name "*.sh" -exec chmod 755 {} \;

  # Fix directories (755)
  find "$dir" -type d -exec chmod 755 {} \;
}

restore_permissions "/home/user/myproject"

Important Notes

  • chmod without -R only affects the specified file
  • Use -R carefully - it affects everything recursively
  • Numeric mode is absolute (replaces all permissions)
  • Symbolic mode is relative (adds or removes specific perms)
  • You can only change permissions on your own files (unless root)
  • Use ls -l to see current permissions

Quick Reference

# Make executable
chmod +x file

# Make readable
chmod +r file

# Make writable
chmod +w file

# Remove write
chmod -w file

# Numeric mode
chmod 755 file    # rwxr-xr-x
chmod 644 file    # rw-r--r--
chmod 600 file    # rw-------

# Recursive
chmod -R 755 dir

# Change for specific user/group
chmod u+x file    # User
chmod g+r file    # Group
chmod o-w file    # Others
chmod a+r file    # All

Summary

Use chmod to set file permissions correctly. Symbolic mode (+x, -w) is easier for one-off changes, while numeric mode (755, 644) is better for precise control. Always verify permissions with ls -l after making changes.