Synchronize pictures & resize at the same time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Synchronize pictures & resize at the same time
# 1  
Old 08-15-2010
Synchronize pictures & resize at the same time

Hi,

I"m trying to achieve the following:

I have a NAS which holds all my pictures, and have it mounted on my xbmc as a network share. I want to automatically synchronize my pictures (NAS -> xbmc, one direction).
But, during the synchronization I want to resize the pictures to make them smaller on my XBMC.

so in short I would think the script should look like:
- diff of the 2 directories (recursivly), with output being only the files existing in the NAS and not on XBMC
- For each entry:
- copy the file
- apply the convert command

Can anyone help me with this?

thanks
# 2  
Old 08-15-2010
Code:
NAS=/XXX/NAS
XBMC=/XXX/XBMC

for file in $(find $NAS -type f)
do
  STR=${file##$NAS/}
  if [ -f $XBMC/$STR ]; then
     cp $file $XBMC/$STR
     Convert $XBMC/$STR                 # replace by the real convert command
  fi
done

# 3  
Old 08-15-2010
thanks for the reply!
but this doens't deal with if source contains directories with subdirectories, correct?
so ideally the script should check what folder the file is in, and recreate that same folder structure in the target ...
Think this is the tricky part Smilie

any ideas on this one?

thanks!
# 4  
Old 08-15-2010
Quote:
Originally Posted by Joeba
but this doens't deal with if source contains directories with subdirectories, correct?
thanks!
It's tricky, but it does deal with subdirectories.

Assume, that NAS was set to /usr4/nas, and the first file returned by find was /usr4/nas/2010/pic1.jpg. The result of this line of the script:

Code:
  STR=${file##$NAS/}

sets STR to 2010/pic1.jpg. The meaning of ${variable##pattern} is to remove the longest occurance of pattern from the front of the contents of variable. The variable file would have been /usr4/nas/2010/pic1.jpg and the value of $NAS would have been /usr4/nas, so the result is everything after /usr4/nas/, or 2010/pic1.jpg which includes the subdirectory.

Assuming XBMC was set to /xbmc, and taking it further

Code:
if [ -f $XBMC/$STR ];

would test to see if /xbmc/2010/pic1.jpg existed -- properly assuming the directory structure from the source filesystem exists on the target filesystem. Likewise, the copy command assumes the same directory structure on the target filesystem. (It does assume that the directory structure exists on the target.)

There are two things that I would tweek:

First, use find piped to while rather than assuming that the output of the find command can be handled as a list to for.

Code:
find $NAS -type f | while read file
do
 ....
done

Secondly, assuming Kshell or Bash, use [[ ]] to supply the test expression rather than the single bracket. The double bracketed expressions are more efficient because the evaluation is done by the shell and not by an external command (test).

Code:
if [[ ! -f $XBMC/$STR ]]

In addition, if the script should make the directories on the target that don't exist, the following needs to be added:

Code:
targetd=$XBMC/${STR%/*}   # get the target dir by chopping off the basename
if [[ ! -d $targetd ]]
then
    mkdir -p $targetd
fi

This should go immediately before the copy.

Last edited by agama; 08-16-2010 at 08:37 PM.. Reason: corrected silly typos
# 5  
Old 08-15-2010
Thanks agama, here is the update code.

Code:
#! /usr/bin/bash

NAS=/XXX/NAS
XBMC=/XXX/XBMC

find $NAS -type f | while read file
do
  STR=${file##$NAS/}
  targetd=$XBMC/${STR%/*}               # get the target dir by chopping off the basename

  if [[ -d $targetd ]]
  then
    mkdir -p $targetd
  fi

  if [[ -f $XBMC/$STR ]]
  then
     cp $file $XBMC/$STR
     Convert $XBMC/$STR                 # replace by the real convert command
  fi
done


Last edited by rdcwayx; 08-16-2010 at 04:19 AM..
# 6  
Old 08-16-2010
thanks you both! Smilie

some questions:
- I don't know the mk command, is mkdir also good?
- shouldn't it be: if [[ ! -d $targetd ]] (exclamation mark) because needs to create dir when it doesn't exist
- same for the file check, shouldn't it be with exclamation mark?
- added quotes around mk -p "$targetd" because had issue with spaces in dir name

when I run the script with a test directory:
test (dir)
_sub test (dir)
___testfile
_testfileroot

I get the following in the target dir
test2 (dir)
_sub test (dir)
___empty!
_testfileroot (dir)
___testfileroot

so think I've made a small error somewhere?
# 7  
Old 08-16-2010
Quote:
Originally Posted by Joeba
thanks you both! Smilie

some questions:
- I don't know the mk command, is mkdir also good?
- shouldn't it be: if [[ ! -d $targetd ]] (exclamation mark) because needs to create dir when it doesn't exist
- same for the file check, shouldn't it be with exclamation mark?
- added quotes around mk -p "$targetd" because had issue with spaces in dir name
How embarassing. Yes, mkdir. I use plan 9's make which is mk, so that is what my fingers like to type.

Also, yes ! -d; another typo I hit $ instead of ! from the looks of my original post.

I cut/pasted the original script posted by rdcwayx and didn't even look to find the -f mistake. Yes, a bang in front of it to copy only the files that don't exist on the target (otherwise you'll overlay them with the smaller images).

I abhor filenames with spaces for just that reason. Quoting is the right thing to do if you don't have a choice about the filenames.

Quote:
when I run the script with a test directory:
test (dir)
_sub test (dir)
___testfile
_testfileroot

I get the following in the target dir
test2 (dir)
_sub test (dir)
___empty!
_testfileroot (dir)
___testfileroot

so think I've made a small error somewhere?
Not sure at the moment. I'll copy out the script you posted and try it here and add something if I see what is wrong.

---------- Post updated at 19:54 ---------- Previous update was at 19:35 ----------

I think the problem stems from the assumption by $(STR%/*} that STR contains a path. It doesn't for files that sit in the top level directory. Here's the whole script that I ran which created the right directories, and copied the things that weren't in the target:

Code:
#!/usr/bin/env ksh

NAS=$PWD/2000     # my test directories
XBMC=$PWD/2001

find $NAS -type f | while read file
do
  STR=${file##$NAS/}
        if [[ $STR == */* ]]        # strip trailing component only if STR has a path rather than just a filename
        then
                targetd=$XBMC/${STR%/*}               # get the target dir by chopping off the basename
        else
                targetd=$XBMC    # if STR is just a filename, the target is just XBMC
        fi

  if [[ ! -d $targetd ]]
  then
    mkdir -p $targetd
  fi

  if [[ ! -f $XBMC/$STR ]]
  then
     cp $file $XBMC/$STR
     echo Convert $XBMC/$STR                 # replace by the real convert command
  else
        echo "target existed, not copied: $file"  # positive confirmation during testing that it didn't try to copy a file
  fi
done

Have a go with the changes to test $STR for a slant and see how that works.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find & auto resize images?

I need a command or mini scripts that can find certain type of images file recursively from the current dir with files of minimum 736 width and resize them by "736 max width, relative height" and replace the source files. I currently have GD installed but can also install Imagemagick if needed, I'm... (5 Replies)
Discussion started by: Frozen77
5 Replies

2. Programming

DATE & TIme

Can we select the datetime from oracle database in “hhmmssnnnccyymmdd” format ? please help to solve this..... (2 Replies)
Discussion started by: Sanal
2 Replies

3. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies

4. AIX

Synchronize time on several AIX servers ?

Hi, I want to synchronize time on several AIX servers - I don't want to set very precise time value, but I want all the servers to have same time value. How do I do that ? thanks Vilius (2 Replies)
Discussion started by: vilius
2 Replies

5. Shell Programming and Scripting

HP-UX & need help with time

I use HP-UX & need help with time. I use a script to create a date stamp: /bin/date "+%m%d%H%M" to get: 12080826. I need a way to use the "touch -t" command to create a file with a timstamp 30 minutes prior to that stamp. (12080756) Can anyone help? (9 Replies)
Discussion started by: obrien2003
9 Replies

6. UNIX for Advanced & Expert Users

Solaris 9: How to set time/Synchronize

Hi, I have 4 solaris 9(32-Bit) Sparc machines on the same subnet. All 4 of them have different times( off by 10-15 mins). I need to synchronize all 4 of them. Please advise what I should do to sync them to the proper time, and with each other. Thanks (3 Replies)
Discussion started by: 0ktalmagik
3 Replies

7. Programming

curses & window resize issues

I am writing a program to display a database to the screen(xterm) and want to allow the window resize signal to increase/decrease the amount data that is displayed. I have a signal handler function for catching the SIGWINCH event and calling a function to reallocate the space for the windows... (0 Replies)
Discussion started by: kwaz
0 Replies

8. UNIX for Dummies Questions & Answers

How do I send pictures in email?

I know how send a html page through mailx, but how do I show the pictures(gif,jpeg,etc.) in the email? (4 Replies)
Discussion started by: Thomas
4 Replies
Login or Register to Ask a Question