Sorry to be making a meal of this but I cannot make my script work. Even with some help from chatGPT all I am doing is digging a deeper hole.
The first script is here:-
#!/bin/bash
# Replace 'path/to/master/directory/A' and 'path/to/servant/directory/B' with your actual directory paths
MASTER_DIR="/home/alastair/Documents"
SERVANT_DIR="/home/alastair/03_July_Old_Files"
# Backup directory where the duplicates will be moved before deletion
BACKUP_DIR="/home/alastair/Temp_dupes"
# First, we run fdupes to find duplicate files between A and B
# The -r flag specifies that it should recurse into subdirectories
# The -N flag ensures that the newest file in each set of duplicates is preserved (optional)
DUPLICATE_FILES=$(fdupes -r "$MASTER_DIR" "$SERVANT_DIR")
# Create the backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Move duplicate files to the backup directory
echo "Moving duplicate files to the backup directory..."
while IFS= read -r -d '' duplicate_file; do
mv -- "$duplicate_file" "$BACKUP_DIR"
done < <(printf '%s\0' "$DUPLICATE_FILES")
echo "Duplicates moved to the backup directory: $BACKUP_DIR"
# Now, you can manually review the duplicates in the backup directory and ensure they are safe to delete.
# After reviewing, you can choose to delete the duplicates from the servant directory (B) if you're satisfied.
# Uncomment the following lines to delete the duplicates:
# echo "Deleting duplicates from $SERVANT_DIR..."
# fdupes -dN "$SERVANT_DIR"
# Alternatively, if you want to delete the duplicates individually, you can do it like this:
# for duplicate_file in "$BACKUP_DIR/Temp_dupes"/*; do
# rm "$duplicate_file"
# done
echo "Review the duplicates in the backup directory before proceeding with deletion."
This has been commented by chatGPT and I have removed the -N option in fdupes because I do not want the date to be used. The result I get is this:-
alastair@HP-Z640-1:~> ./delete_duplicates_with_backup.sh
Moving duplicate files to the backup directory...
mv: cannot stat '/home/alastair/03_July_Old_Files/Documents/Packed_Food_Labels/Mains_Chilled_or_Frozen/Vegetable Tajine with Couscous'$'\n''(GF,V).odt'$'\n''/home/alastair/Documents/Packed_Food_Labels/Mains_Chilled_or_Frozen/Vegetable Tajine with Couscous'$'\n''(GF,V).odt'$'\n\n''/home/alastair/03_July_Old_Files/Documents/Packed_Food_Labels/Mains_Chilled_or_Frozen/Tomato & Green Bean Breedie'$'\n''(GF, V).odt'$'\n''/home/alastair/Documents/Packed_Food_Labels/Mains_Chilled_or_Frozen/Tomato & Green Bean Breedie'$'\n''(GF, V).odt': No such file or directory
Duplicates moved to the backup directory: /home/alastair/Temp_dupes
Review the duplicates in the backup directory before proceeding with deletion.
alastair@HP-Z640-1:~>
Temp_dupes is created but the directory is empty and I have the errors shown. The problem may be the result of the files having too many characters or file names with spaces but it doesn’t work
Several other suggestions were tried (more than 18) but I ended up going round in circles. I did see however that when I has the second and subsequent suggested solutions I received the error message from Kate mentioned above. Here is another suggested solution:-
#!/bin/bash
# Replace 'path/to/master/directory/A' and 'path/to/servant/directory/B' with your actual directory paths
MASTER_DIR=/home/alastair/Documents
SERVANT_DIR=/home/alastair/03_July_Old_Files
# Backup directory where the duplicates will be moved before deletion
BACKUP_DIR=/home/alastair/Temp_dupes
# First, we run fdupes to find duplicate files between A and B
# The -r flag specifies that it should recurse into subdirectories
# The -N flag ensures that the newest file in each set of duplicates is preserved (optional)
DUPLICATE_FILES=$(fdupes -r -N "$MASTER_DIR" "$SERVANT_DIR")
# Create the backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Move duplicate files to the backup directory using cpio
echo "Moving duplicate files to the backup directory..."
while IFS= read -r duplicate_file; do
echo "$duplicate_file" | cpio -pd "$BACKUP_DIR"
done <<< "$DUPLICATE_FILES"
echo "Duplicates moved to the backup directory: $BACKUP_DIR"
# Now, you can manually review the duplicates in the backup directory and ensure they are safe to delete.
# After reviewing, you can choose to delete the duplicates from the servant directory (B) if you're satisfied.
# Uncomment the following lines to delete the duplicates:
# echo "Deleting duplicates from $SERVANT_DIR..."
# fdupes -dN "$SERVANT_DIR"
# Alternatively, if you want to delete the duplicates individually, you can do it like this:
# for duplicate_file in "$BACKUP_DIR"/*; do
# rm "$duplicate_file"
# done
echo "Review the duplicates in the backup directory before proceeding with deletion."
And this gives me:-
[14:50:32  LSP Client Warning] Failed to find server binary: bash-language-server
Please check your PATH for the binary
See also https://github.com/bash-lsp/bash-language-server for installation or details
But when I check my system I cannot identify what provided bash-language-server.
I would appreciate a bit of guidance from a pro please.