Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have to get pictures from a folder – with subfolders which are updated automatically – with their extensions.

These files have to be copied in a folder where a website based on PHP will edit them (by renaming and creating an XML file) to be downloadable and integrated in an XML feed.

Because of the rename function of the script, when I perform the copy gain, all the files are duplicated, because the script has renamed the original ones already.

I've tried a few things with rsync but I'm looking for something more powerful because I can't copy files with an external "history".

#!/bin/bash
find  '/home/name/picture' -name '*.jpg' | while read FILE ; do rsync --backup  --backup-dir=incremental --suffix=.old  "$FILE" /var/www/media ; done
wget --spider 'http://myscript.php' ; 
#exit 0

PS: As a little addition, I'd like to replace '.' with a 'space' just after the *.jpeg copy. My PHP script has some problem to define files with comma because of the extension. I'm finking about a command with find – like I did before – with a sed function? Is that a good idea?

share|improve this question
2  
So, am I reading this right, when you copy with rsync, it creates all the files again, because they've been renamed, and you get duplicates? –  slhck Nov 14 '11 at 16:17
    
Yes slhck. I'm sorry it's quite hard to explain clearly with my bad english. :) –  Benoitt Nov 14 '11 at 22:50
    
No prob, that's why I asked :) — So I was thinking, maybe the PHP tool could just make a list of which files it has renamed. You could then exclude those from the rsync command. –  slhck Nov 14 '11 at 22:55
    
The thing is a didn't develop the php website. I've try to desactivate few fonctions like renaming but it doesn't work. I've also contact the author but he refuse to change anything to prevent bugs on uploading. ;) –  Benoitt Nov 15 '11 at 0:50
    
Do you know what the renaming convention is? In other words, the files get uploaded and renamed, would your script be able to predict what the new names would be? I suppose the other way to do it is keep track locally of any files that have been successfully uploaded and exclude them (or move them out of the source folder) –  Paul Nov 15 '11 at 2:22
show 3 more comments

1 Answer

up vote 0 down vote accepted

You can use the following:

#!/bin/ksh
DEST_DIR=/destination
HIST_DIR=/images_bck
SOURCE_DIR=/images

find ${SOURCE_DIR} -name "*.jpg" >${HIST_DIR}/tmp.file
exec 3<${HIST_DIR}/tmp.file

while read file <&3;do
  file_name=$(basename "${file}")
  if [ ! -f "${HIST_DIR}/${file_name}.old" ];then
    cp -p "${file}" "${HIST_DIR}/${file_name}.old"
    cp "${file}" "${DEST_DIR}"
  fi
done
exec 3<&-
[ -f ${HIST_DIR}/tmp.file ] && rm -f ${HIST_DIR}/tmp.file

For new users;

sudo apt-get install ksh

it's a kornshell, after change DEST_DIR , HIST_DIR, SOURCE_DIR and do

ksh -v ./MySuperScript.ksh

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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