How to Create a Simple Bash Script to Sync Your Project Folder and Set Correct Permissions.

How to Create a Simple Bash Script to Sync Your Project Folder and Set Correct Permissions.

Created by: laczi.ostr

How to Create a Simple Bash Script to Sync Your Project Folder and Set Correct Permissions

Bash scripts provide a simple yet powerful way to automate repetitive tasks in Linux. In this article, we'll walk through the creation of a basic Bash script to sync your project folder from your home directory's `Documents` folder to the `www` directory for web deployment. We'll also cover how to set correct file and folder permissions in the destination folder.

Introduction

When developing web projects, it’s common to move files from your project folder to a web server directory like `www`. Manually copying files and setting permissions every time can be time-consuming and error-prone. This article will guide you through automating these tasks with a simple Bash script that syncs your project files to the web server folder and ensures correct file and folder permissions.

Step-by-Step Guide

1. Prerequisites

Before we dive into the script creation, ensure you have the following:

2. Writing the Bash Script

Let’s create a script named sync_project.sh that performs the following:

To do this, open your terminal and follow these steps:

#!/bin/bash

# Define source and destination directories
SOURCE_DIR=~/Documents/project/
DEST_DIR=/var/www/project/

# Sync the project folder from Documents to www
rsync -av --delete $SOURCE_DIR $DEST_DIR

# Set correct permissions for the web folder
# 755 for directories and 644 for files
find $DEST_DIR -type d -exec chmod 755 {} \;
find $DEST_DIR -type f -exec chmod 644 {} \;

# Print a message indicating success
echo "Project synced and permissions set successfully!"

Explanation

In the above script:

3. Making the Script Executable

To use the script, you need to make it executable. Navigate to the directory where you saved sync_project.sh and run the following command:

chmod +x sync_project.sh

Now, you can execute the script by running:

./sync_project.sh

4. Automating the Script with Cron

To automate the syncing process, you can add the script to your crontab. This will allow the script to run at specified intervals, ensuring your project is always up-to-date in the web server directory. To edit your crontab, use the following command:

crontab -e

Add the following line to run the script every day at midnight:

0 0 * * * /path/to/your/sync_project.sh

Conclusion

By following this guide, you've learned how to automate the process of syncing a project folder from your home directory to the web server folder and set correct file permissions using a Bash script. This saves time and ensures your files are always properly managed. Automating the task with cron further simplifies your workflow. With this simple script, you'll be able to focus more on coding and less on manual file management.

References

1. "Linux rsync Command Tutorial with Examples," Linuxize 2. "Understanding Linux File Permissions," DigitalOcean 3. "How to Use Cron to Automate Tasks on a Linux VPS," Hostinger