Joe Bloggs
Joe Bloggs

Reputation: 41

Error trying to copy certain file types over to a new directory

I have a file filled with photos called 'flashmem' (/root/flashmem), I am trying to move all photos in the format IMG_0000.JPG as you can see from the regex into another directory called 'photoarch' (/root/photoarch)($1), I want to move all duplicates into a txt file ($2). Below I have attached my code, what i'm typing into the command line showing my errors as well as what my root directory looks like. Any help is appreciated, I have been trying to get this working for ages so please no negative feedback on this question! Thanks.

Command line error: https://i.sstatic.net/dethl.jpg My root directory: https://i.sstatic.net/Un27C.jpg

#!/bin/sh
if ["$#" -ne2 ]; then
    echo "Illegal number of parameters"
fi
find "$1" -regextype posix-extended -regex 'IMG_[0-9]{4}\.[JPG]{3}$' -exec cp --backup=numbered -t "$2" \;
find "$2" -type f-exec md5sum '{}' ';' |sort| uniq--all-repeated=separate-w 32 > /root/duplicate.txt

Upvotes: 1

Views: 39

Answers (1)

janos
janos

Reputation: 124714

You have a couple of syntax errors in the script.

  • Should be [ $# -ne 2 ] instead of ["$#" -ne2 ]
  • Should probably exit with non-zero if there are no 2 parameters
  • The -exec cp ... in the find command lacks the parameter placeholder {}
  • The second find pipeline has many options stuck together without separating spaces
  • The regex IMG_[0-9]{4}\.[JPG]{3}$ in the first find looks weird. Why not end with JPG instead of [JPG]{3}?

Putting the above together, this should work better:

#!/bin/sh

if [ $# -ne 2 ]; then
    echo "Illegal number of parameters"
    exit 1
fi

find "$1" -regextype posix-extended -regex 'IMG_[0-9]{4}\.JPG$' -exec cp --backup=numbered -t {} "$2" \;
find "$2" -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 32 > /root/duplicate.txt

Upvotes: 2

Related Questions