#!/bin/bash
# Script to find all files with a certain extension and move them to the recycle bin
# Check if the user provided the correct arguments
if [ "$#" -lt 2 ]; then
echo "Usage: $0 <directory> <extension>"
echo "Example: $0 /path/to/search .txt"
exit 1
fi
# Get the directory and extension from the arguments
directory=$1
extension=$2
# Validate the directory
if [ ! -d "$directory" ]; then
echo "Error: Directory '$directory' does not exist."
exit 1
fi
# Find the root of the shared directory using df and awk
share_root=$(df -h "$directory" | awk 'NR==2 {print $NF}')
# Ensure the share_root was determined
if [ -z "$share_root" ]; then
echo "Error: Unable to determine the shared folder root."
exit 1
fi
# Define the recycle bin path
recycle_bin="$share_root/#recycle"
# Check if the recycle bin directory exists
if [ ! -d "$recycle_bin" ]; then
echo "Error: Recycle bin directory ('$recycle_bin') does not exist. Enable the recycle bin for this shared folder."
exit 1
fi
# Use the find command to search for files
echo "Searching for files with the extension '$extension' in '$directory'..."
files=$(find "$directory" -type f -name "*$extension")
# Check if any files were found
if [ -z "$files" ]; then
echo "No files with the extension '$extension' were found."
exit 0
fi
# Display the files to the user
echo "The following files were found:"
echo "$files"
# Ask the user if they want to move the files to the recycle bin
read -p "Do you want to move these files to the recycle bin? (y/n): " confirm
# If the user confirms, move the files
if [[ "$confirm" == "y" || "$confirm" == "Y" ]]; then
echo "Moving files to the recycle bin..."
while IFS= read -r file; do
mv -v "$file" "$recycle_bin/"
done <<< "$files"
echo "Files have been moved to the recycle bin."
else
echo "No files were moved."
fi