How to Use Bash script Change folder Permissions Recursively

Introduction

Learn how to use a bash script to change folder permissions recursively with practical examples. This guide includes step-by-step instructions and script samples to effectively manage file permissions in Linux.

Managing file permissions is a fundamental task for any system administrator. Often, you need to change the permissions of a directory and all its contents. This guide will demonstrate how to use a bash script to change folder permissions recursively, with examples covering both basic and advanced scenarios.

What is Recursive Permission Change?

Recursive permission change applies the specified permissions to the target directory and all its subdirectories and files. This approach is particularly useful for ensuring consistent access controls across complex directory structures.

Why Use Bash Scripts for Permission Changes?

Bash scripts allow for automation, saving time and reducing errors when changing permissions across multiple files and directories. Scripts can be customized for specific needs and can handle complex scenarios more efficiently than manual methods.

Example Scenario: Changing Permissions for huuphan.com Directory

Directory Structure

Let's consider a directory named huuphan.com with the following structure:

[root@huupv huupv]# tree huuphan.com/ huuphan.com/ ├── a │ ├── abc.txt │ └── b │ └── c └── K 4 directories, 1 file

Listing Files and Folders

To list all files and folders under huuphan.com:

[root@huupv huupv]# find /home/huupv/huuphan.com -type d -exec ls -ld {} + > chown_folder.txt

The find command with -type d lists all directories, and -exec ls -ld {} lists their details. The output is saved to chown_folder.txt.

Reading the Output

To read the contents of chown_folder.txt:

[root@huupv huupv]# cat chown_folder.txt

Example output:

drwxrwxr-x. 4 huupv huupv 4096 Mar 9 22:38 /home/huupv/huuphan.com drwxrwxr-x. 3 huupv huupv 4096 Mar 9 22:38 /home/huupv/huuphan.com/a drwxrwxr-x. 3 huupv huupv 4096 Mar 9 22:37 /home/huupv/huuphan.com/a/b drwxrwxr-x. 2 huupv huupv 4096 Mar 9 22:37 /home/huupv/huuphan.com/a/b/c drwxrwxr-x. 2 huupv huupv 4096 Mar 9 22:38 /home/huupv/huuphan.com/K

Bash Script to Change Folder Permissions Recursively

Here is a bash script to change permissions recursively:

#!/bin/bash # Author: HuuPV while read -r line; do directory=$(echo $line | awk '{print $9}') chown $1:$2 $directory done < chown_folder.txt

Explanation

  • while read -r line; do ... done < chown_folder.txt: Reads each line of chown_folder.txt.
  • directory=$(echo $line | awk '{print $9}'): Extracts the directory path from each line.
  • chown $1:$2 $directory: Changes the owner and group of the directory.

Running the Script

To run the script, use the following command:

[root@huupv huupv]# ./chown_folder.sh nobody huupv

This command changes the owner to nobody and the group to huupv for all directories listed in chown_folder.txt.

Advanced Script with Error Handling and Logging

For a more robust solution, include error handling and logging:

#!/bin/bash # Author: HuuPV LOG_FILE="chown_permissions.log" log() { echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" } while read -r line; do directory=$(echo $line | awk '{print $9}') if chown $1:$2 $directory; then log "Changed ownership of $directory to $1:$2" else log "Failed to change ownership of $directory" fi done < chown_folder.txt

Explanation

  • LOG_FILE="chown_permissions.log": Defines the log file.
  • log() { ... }: Logs messages with timestamps.
  • if chown $1:$2 $directory; then ... else ... fi: Adds error handling for the chown command.

Common Use Cases

Changing Permissions for Web Server Files

To change permissions for web server files:

#!/bin/bash WEB_DIR="/var/www/html" WEB_PERMISSIONS="755" chmod -R "$WEB_PERMISSIONS" "$WEB_DIR" echo "Web directory permissions set to $WEB_PERMISSIONS."

Securing Backup Directories

To secure backup directories:

#!/bin/bash BACKUP_DIR="/backup" BACKUP_PERMISSIONS="700" chmod -R "$BACKUP_PERMISSIONS" "$BACKUP_DIR" echo "Backup directory permissions set to $BACKUP_PERMISSIONS."

Frequently Asked Questions

What does chmod -R do?

The -R option in chmod stands for "recursive," applying the permission change to the specified directory and all its contents.

Can I change permissions for specific file types only?

Yes, use find with chmod to target specific file types. For example, to change permissions for all .sh files:

find /path/to/dir -type f -name "*.sh" -exec chmod 755 {} \;

How can I revert permission changes?

To revert permission changes, maintain a backup of the original permissions and use a similar chmod script to restore them.

Conclusion

This guide has demonstrated how to use bash scripts to change folder permissions recursively. By automating this process, you can manage permissions efficiently and reduce the risk of errors. Whether you're handling web server files or securing backup directories, understanding and utilizing recursive permission changes is essential for effective system administration. Thank you for reading the huuphan.com page!


Bash script Change folder Permissions Recursively


Comments

Popular posts from this blog

zimbra some services are not running [Solve problem]

Bash script list all IP addresses connected to Server with Country Information

Zimbra Client host rejected Access denied fixed