Bash Script To Delete Directory, then Re-Create With Source Content

Below is a Bash script that deletes a directory, creates a new directory with the same name, and then copies all files (including hidden files and folders) from another given directory path into the newly created directory with permission.

To ensure that hidden files and directories are also copied, you can use the shopt command to enable the dotglob option before performing the copy operation. Here’s the script:

#!/bin/bash

# Check if both target and source directories are provided as command-line arguments
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <path/to/target_directory> <path/to/source_directory> <permissions>"
    exit 1
fi

TARGET_DIR="$1"
SOURCE_DIR="$2"
PERMISSIONS="$3"

# Ensure the target directory exists
if [ ! -d "$TARGET_DIR" ]; then
    echo "Error: Target directory '$TARGET_DIR' does not exist."
    exit 1
fi

# Remove existing target directory
rm -r "$TARGET_DIR"

# Check if the removal was successful
if [ $? -eq 0 ]; then
    echo "Directory '$TARGET_DIR' deleted successfully."
else
    echo "Error: Failed to delete directory '$TARGET_DIR'."
    exit 1
fi

# Create a new directory with the same name as the deleted one
mkdir "$TARGET_DIR"

# Check if the creation was successful
if [ $? -eq 0 ]; then
    echo "Directory '$TARGET_DIR' created successfully."
else
    echo "Error: Failed to create directory '$TARGET_DIR'."
    exit 1
fi

# Enable dotglob to include hidden files and directories during the copy
shopt -s dotglob

# Copy all files (including hidden) from the source directory to the target directory
cp -r "$SOURCE_DIR"/* "$TARGET_DIR"

# Check if the copy was successful
if [ $? -eq 0 ]; then
    echo "Files copied from '$SOURCE_DIR' to '$TARGET_DIR' successfully."
else
    echo "Error: Failed to copy files from '$SOURCE_DIR' to '$TARGET_DIR'."
    exit 1
fi

# Disable dotglob after the copy
shopt -u dotglob

# Set permissions for the target directory and its contents
chmod -R "$PERMISSIONS" "$TARGET_DIR"

echo "Directory deletion, creation, file copy, and permission set completed successfully."

You can run the command in terminal:

./recreate_directory.sh /path/to/target_directory /path/to/source_directory 777

This script uses shopt -s dotglob to enable the dotglob option before the copy operation and shopt -u dotglob to disable it after the copy. This ensures that hidden files and directories are included in the copy process.

Add rsync to even show progress bar

Certainly! To display more advanced progress information during the file copy operation, you can use the rsync command instead of cp. rsync provides a progress bar by default. Here’s the modified script:

#!/bin/bash

# Prompt user for target directory path
read -p "Enter the target directory path: " TARGET_DIR

# Prompt user for source directory path
read -p "Enter the source directory path: " SOURCE_DIR

# Prompt user for permission code
read -p "Enter the permission code: " PERMISSIONS

# Ensure the target directory exists
if [ ! -d "$TARGET_DIR" ]; then
    echo "Error: Target directory '$TARGET_DIR' does not exist."
    exit 1
fi

# Remove existing target directory
rm -r "$TARGET_DIR"

# Check if the removal was successful
if [ $? -eq 0 ]; then
    echo "Directory '$TARGET_DIR' deleted successfully."
else
    echo "Error: Failed to delete directory '$TARGET_DIR'."
    exit 1
fi

# Create a new directory with the same name as the deleted one
mkdir "$TARGET_DIR"

# Check if the creation was successful
if [ $? -eq 0 ]; then
    echo "Directory '$TARGET_DIR' created successfully."
else
    echo "Error: Failed to create directory '$TARGET_DIR'."
    exit 1
fi

# Copy all files (including hidden) from the source directory to the target directory using rsync
rsync -a --info=progress2 "$SOURCE_DIR"/ "$TARGET_DIR"/

# Check if the copy was successful
if [ $? -eq 0 ]; then
    echo "Files copied from '$SOURCE_DIR' to '$TARGET_DIR' successfully."
else
    echo "Error: Failed to copy files from '$SOURCE_DIR' to '$TARGET_DIR'."
    exit 1
fi

# Set permissions for the target directory and its contents
chmod -R "$PERMISSIONS" "$TARGET_DIR"

echo "Directory deletion, creation, file copy, and permission set completed successfully."

In this version, the rsync command is used with the -a (archive) option, which preserves the file permissions and other attributes. The --info=progress2 option displays a progress bar during the copy operation. This provides more advanced information about the progress of the file copy. The rest of the script remains the same.

Please note that the rsync command may need to be installed on your system if it’s not already available. You can install it using the package manager specific to your Linux distribution (e.g., sudo apt-get install rsync on Ubuntu).

Leave a comment

Your email address will not be published. Required fields are marked *