2

I have a folder with images in it and it is organized by year and month folders (ie. /2015/08/image.jpg). Whenever an image is added, it is also copied and resized like so:

image-1024x768.jpg
image-150x150.jpg
image-300x225.jpg
image-600x450.jpg
image.jpg

I would like to make a script which will search through each year and month folder and copy only the original file into another folder on my system.

I think I need to use regular expressions, but I don't know how.

EDIT: Here are some sample file names. The ones with a star(*) next to them are the only ones that should be copied.

1450366623-20151217-109x300.png
1450366623-20151217-150x150.png
1450366623-20151217-372x1024.png
1450366623-20151217-600x1650.png
1450366623-20151217.png*
image-3-1024x768.jpeg
image-3-150x150.jpeg
image-3-225x300.jpeg
image-3-300x225.jpeg
image-3-600x450.jpeg
image-3-600x800.jpeg
image-3-768x1024.jpeg
image-3-768x576.jpeg
image-3.jpeg*
IMG_0939-1-1024x768.jpg
IMG_0939-1-150x150.jpg
IMG_0939-1-300x225.jpg
IMG_0939-1-600x450.jpg
IMG_0939-1-768x576.jpg
IMG_0939-1-e1451106610990-150x150.jpg
IMG_0939-1-e1451106610990-225x300.jpg
IMG_0939-1-e1451106610990-600x800.jpg
IMG_0939-1-e1451106610990-768x1024.jpg
IMG_0939-1-e1451106610990.jpg*
IMG_0939-1.jpg*

EDIT 2: I said that the images are organized in month/year folders, which I'm sure means it should just be recursive. However, there are other folders next to 2015 that I would prefer don't get copied.

When these pictures are copied, is it possible to copy the folder structure also?

EDIT 3: Thank you to everyone that has answered my question. I must apologize and regretfully admit that I am in over my head with this project and have decided to abandon it. I'm not even sure how to implement most of your answers.

  • Are all your files called image? If not, please give some examples of real names. (Don't forget to edit your question rather than putting your response here in the comments.) – roaima Jan 18 '16 at 16:37
  • 1
    Why shouldn't IMG_0939-1-e1451106610990.jpg be copied? – glenn jackman Jan 18 '16 at 19:49
  • My mistake. You're right, that should be copied, too. – kylesureline Jan 20 '16 at 7:03
4

You don't need a regular expression, you need extended globbing patterns. I assume your shell is bash:

shopt -s extglob
cd parent-directory-of-2015
for original in */*/!(*-+([0-9])x+([0-9]).jpg); do
    echo "an original: $original"
done

That pattern will exclude files with matching "-digitsxdigits.jpg"

0

Looks like the x within the size of the image in the name could be used, so I'd write it something like this. looking for any file thats name does not have the x using a substring then copy all of them, leaving the others alone.

#!/bin/bash

typeset -i count
let count=0

MAXF="$(find /path -type f -name "*.*" | wc -l)"

find /path   -type f -name "*.*" | while [[ $count -ne $MAXF ]] ;
 do read FILENAME;

name=${FILENAME##*/}
substring="x"

if [[ "$name" != *"$substring"* ]] ; then
     cp -v "$FILENAME" /path/newname
     ((count++))
fi
((count++))
 done

I did not test this... its off the cuff, but that'll set up up to complete what other things you already have in mind to do with your little project.

for this part of your question "When these pictures are copied, is it possible to copy the folder structure also?" is yes, but it can become a complexed script, if you are only coping some files, and not all of them into a like structor but in a different parent directory.

because it has to be on a different partition or parent directory name in order to have the same subDirectory structor. here is a sniplet of one of my scripts that I do something simluar in it.

This hopegully will help you to figure out how to use variables and command line and the logic to get'er done. the ... indecate code before this would be in place in order for the rest of this code to work properly. the first three statments sets up your dir paths to use srtipt_dir may not really be needed within your script it all depends on how and what you are doing within the script to make it a needed item. move_to can be changed to copy_to.

this is a two loop set up I am using, working off one "base" Dir at a time, then going though every file within that dir before moving on to another "base" dir within a tree and its subdirectories. Checking in a different branch of the tree structor to see if the same dir name is already there, if not then I move it into that new branch off the tree, if it is then I copy the contents of the branch into that other branch off the tree structor then delete whatever is left in the old branch of the tree (dir structor).

use echo a lot to check your work first before putting it into action

#!/bin/bash

script_dir="path"
files_dir="path"
move_to="path/dirName"

........ 

    # if this is say /home/picures/myFotos
    # then this statment will check the last dir name is there
    # if it is not there. It will then create it.

   # you will have to then take off of the old dir structor and 
   # recreate it off of this statement

    if [[ ! -d "$move_to" ]] ; then
            mkdir -v "$move_to"
        fi

      # set for 5th Directory to be Artist Dir
        echo;echo
        OldDirName=${DIRNAME#*/*/*/*/*/}                        
        echo "1 - OldDirName $OldDirName"
        echo
        OldDirName=${OldDirName//\/*}
        echo "2 - OldDirName $OldDirName"
        echo
        echo;echo "OldDirName : $OldDirName"
        echo
    # if no artist on file tag present then keep orginal DirName
        if [ ! -z "$changeDirName" ] ; then

            newDirName="$changeDirName"
        else
            #if no Artist MetaData 
            newDirName="$OldDirName"
        fi

########### CHANGE NAME OF PARENT DIR WHERE IT IS AT FIRST
        echo;echo "Old Dir Name != newDirName " echo "this is MP3Count - "$MP3Count""


        echo;echo "[[ "$OldDirName" != "$newDirName" ]]"


        if [[ "$OldDirName" != "$newDirName" ]] ; then 


            oldDirPathNewName=""$working_dir"/"$newDirName""
            Dir="$newDirName"
            echo;echo "oldDirPathNewName "$oldDirPathNewName""
            echo ""$OldDirName" not = to "$newDirName""

           #change name of dir to artist/band name
            echo "mv "$OldDirName" to "$oldDirPathNewName""

            #change old dir name to new dir name
            #mv -v "$working_dir"/"$OldDirName" "$oldDirPathNewName"

            mv -v "$working_dir"/"$OldDirName" "$oldDirPathNewName"
            echo
        else
            Dir="$OldDirName"
        fi





            from=""$working_dir"/"$Dir""
            to=""$move_to"/"$Dir""          

    #check if the desanation directory is already there if not move it or 
    # copy it into it then remove left overs 

        if [[ ! -d "$to" ]] ; then
        #move_to"/"$Dir" ]] ; then



            echo "inside ck if move to parent / artist to dir is there"
            echo
            echo ""$move_to"/"$Dir" is not there moving "$Dir""
            echo                                    
            #then move the new dir name to a different
            # place for safe keeping
            echo
            echo;echo "just befor move "
            echo
            echo

            echo "$from -> $to"
            mv -vf "$from" "$to"

            echo    
            echo "DirIsaidtoDo Left : $DirIsaidtoDo"
            echo        

        else


            echo ""$move_to"/"$stringNewDir" is there moving within it into"
            echo
            echo "$move_to"/"$stringNewDir"
            echo

            echo
            cp -Rv "${from}"/* "$to"  
            echo
            rm -rv "$from"
            echo
            echo "DirIsaidtoDo Left : $DirIsaidtoDo"
            echo
        fi

    fi                           

            echo "********** NEXT FILE ***********************"     

        done
        let DirCountDn++
        echo "Dir Count Dn "$DirCountDn"" 
        echo "******************************************" 
        let AmountDirs--
        echo "Amount Dirs to go $AmountDirs"
        echo
        let DirIsaidtoDo--

        echo;echo;echo


    done #FOR DIR Names

to create more then one dir in a path dir cretion order, this creates all dirNames setting up a parent dir and its subDir too

 mkdir -pv path/dirName/DirName/DirName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.