Image Archive Script

Hello,
I am working through a test Linux script from a website, and am running into some issues. The point of the script is to find image files throughout the system, copy them to one centralized directory,ignore duplicate images with the same name, and output a text file of all the images copied.
The script is below.

#!/bin/sh
if [ ! -d $1 ] ; then 
echo "Exiting due to non-existing $1"
exit #Check if source drive exists#
#
fi


if [ ! -d $2 ] ; then mkdir $2 #checking if destination directory exists #

else  echo -e "Usage::# phar image_path archive_path" #outputs helpful error message #

find $1 -name "IMG_*.JPG" # find files #
fi

for i in $1; do  #finds duplicate files and copies the non duplicate files.
    find $i -type f -name 'IMG_*.jpg' -exec cp {} $2 \;
    for j in $2; do
        find $2 -type f -name $i | while read filename; do if test "$i"="$j"; then mv $i new_filename; fi done
    done
done 

#then
#    mv -f "IMG_*.JPG.JPG"
#
#    else if  diff -q $file1 $file2  ; then echo "files are the same" ; exit # Checks if two files are the same.#
#    fi

# output a list of the files copied and moved
# readlink -f.-name"iMG".JPG>files_moved.txt

Thanks for the help.

The issues are WHAT?

Some comments:

  • Unless you have good reasons (which?), why do you limit the execution to the sh shell?
  • Your usage/error msg comes if $2 exists - is that expected behaviour?
  • What are you using the results of the first find for?
  • Do you want to copy or move the files?
  • Do you know about the -v option to both cp and mv ?

-The example I was following was limiting the function to a bash shell only

  • I want it to output an error message if both directories exist to tell the user how to run the program
  • I am using the first find to find all the images that match this format IMG_0000.JPG
  • I want to copy the files into an archive folder without any sub folders.
  • No

Heya ComSciguy17

What already has been said:

  1. The usage message comes if "$2" is passed (See 3.)
    Anyway, the user passes a target dir, sees the usage message, and a list of files found in $1 - that list is not -- cannot be -- used -- later on again, --> unrequired/obsolete.

In addition:
2. Use more (often) quotes around variables (imagine file names with spaces, though not wanted, but might happen anyway.
3.
[ -d "$1" ] Checks if $1 is a directory
[ -z "$1" ] Checks if an argument has been passed at all
If no argument has been passed, say so, dont use the (unescaped) variable-name, as that part will be empty if no argument has been passed.
Likewise for the destination.
Example:

[ -z "$2" ] && echo "Usage: ${0##*/} SEARCHDIR TARGETDIR" && exit 1
[ ! -d "$2" ] && ! mkdir -p "$2" && echo "Could not create destination: \"$2\"" && exit 1
  1. Place the results of find into variables (lists), as that could help to remove duplicates.
    Some of the find commands, look (to my eyes) unused...

Last but not least, since not really relevant, try to stick to one coding style, i see like 3 or even 4 different styles.
Makes the reading easier (specialy without syntax highlighting).
But maybe you just need to figure your coding style first, so no worries. :wink:

And to repeat RudiC, since i dont feel that has been provided yet...

Elaborate, dont just say what you want, but what is, what you have tried to fix, where you stuck?

Have a nice evening.
hth