0

I'm trying to write a script to compare files in one directory against another (including sub-directories) & see if they exist. What I have so far is:

#!/bin/sh

# This will compare files in 'dropbox>camera uploads' against pics in local 'pictures' folder.
# If present they are moved to 'duplicate' folder if not to 'not_duplicate' inside dropbox
#
# Mar '20
#
#

DROPBOX_LOC="/home/unencrypted/Dropbox/CameraUploads"
DROPBOX_LOC_DUPLICATE="/home/unencrypted/Dropbox/CameraUploads/duplicate"
DROPBOX_LOC_NOT_DUPLICATE="/home/unencrypted/Dropbox/CameraUploads/not_duplicate"
PICS_LOC="/home/user/Pictures"

for file in $DROPBOX_LOC/*
# NOTE: Use of " " preserves whitespace in single variable!

do
    echo "Checking for: $file"
    #ls -l $file

   if [[ -f "$PICS_LOC/${file##*/}" ]]
        then
        echo "$file is a duplicate"
        # Move to duplicate folder
        continue
     else
        echo "$file is a NOT a duplicate"
        # Move to not duplicate folder
        fi
done
exit 0

But this returns:

Checking for: /home/unencrypted/Dropbox/CameraUploads/2015-11-07 16.58.49.jpg
./delete_dropbox_pics.sh: 22: ./delete_dropbox_pics.sh: [[: not found
/home/unencrypted/Dropbox/CameraUploads/2015-11-07 16.58.49.jpg is a NOT a duplicate

Can anyone please suggest a fix (including how to search sub-directories)?

leo8382
  • 91
  • 8
  • Single square brackets? Your `continue` is redundant. –  Apr 25 '20 at 21:10
  • 2
    Validate your script, via https://shellcheck.net, You're using `sh` as the shebang but your test is using `[[` which does not exists in `POSIX sh`. any how that link will good for you. – Jetchisel Apr 25 '20 at 21:10
  • 1
    Btw., if you really want to check for exact duplicates, you might want --beside the filename-- to check e.g. on `md5sum`'s. –  Apr 25 '20 at 21:16
  • Oh, change the shebang to `#!/bin/bash`. –  Apr 25 '20 at 21:18
  • There are apps written to find duplicates, to name a few, `fdupes` and `rmlint` and good luck for reinventing the wheel :-) – Jetchisel Apr 25 '20 at 21:22
  • 1
    in additional to replace [[ ]] with [ ] in your if constructions, "for file in $DROPBOX_LOC/*" and "including sub-directories" will not works, you should use find command, which can recursively find the files in subdirectories. – Saboteur Apr 25 '20 at 21:27
  • 1
    Does this answer your question? [Bash syntax error: "\[\[: not found"](https://stackoverflow.com/questions/3401183/bash-syntax-error-not-found) – compuphys Apr 26 '20 at 10:10
  • Thanks for all the suggestions. I actually stumbled across FSlint Janitor which does does what I want. I corrected some of my mistakes but looks like I need to move to using find as pointed out. – leo8382 Apr 28 '20 at 21:18

1 Answers1

0

Why not use diff command.

diff -r <directory1> <directory2>

-r, --recursive recursively compare any subdirectories found

Digvijay S
  • 2,665
  • 1
  • 9
  • 21
  • Thanks for the suggestion but the Dropbox is a small subset of the picture folder, I'm trying to see what duplicates exist in the Dropbox folder – leo8382 Apr 28 '20 at 21:15