The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script to search for text in a file and copy file imeadows UNIX for Dummies Questions & Answers 9 11-12-2008 09:12 PM
Noob on Unix. bobtheb UNIX for Dummies Questions & Answers 7 07-10-2008 07:56 AM
how to copy a file to a directory ,where file and dir are sent as args to a function? wrapster Shell Programming and Scripting 0 06-08-2008 08:37 AM
Script to capture new lines in a file and copy it to new file fara_aris Shell Programming and Scripting 0 05-27-2008 11:11 PM
complete noob avdrummerboy UNIX for Dummies Questions & Answers 3 12-04-2006 12:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Something like this perhaps:


Code:
#!/usr/bin/ksh
find /srcdir -type f | while read f
do
    destfile=$(echo $f | sed 's#/srcdir#/destdir#')
    if find $destfile -newer $f
    then
        print "$destfile is newer than $f, skipping"
    else
        print cp -p $f $destfile
    fi
done

Obviously take out the second print once you've confirmed that it's going to do what you want.

Last edited by Annihilannic; 07-17-2008 at 04:17 AM.. Reason: typo
  #2 (permalink)  
Old 07-17-2008
wildside wildside is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
Quote:
Originally Posted by Annihilannic View Post
Something like this perhaps:


Code:
#!/usr/bin/ksh
find /srcdir -type f | while read f
do
    destfile=$(echo $f | sed 's#/srcdir#/destdir#')
    if find $destfile -newer $f
    then
        print "$destfile is newer than $f, skipping"
    else
        print cp -p $f $destfile
    fi
done

Obviously take out the second print once you've confirmed that it's going to do what you want.

THANK YOU! this is where I have been trying to get to.
So I tried to convert it to using Bash Shell. (im on OS 10.5)

Here is my script:


Code:
#!/bin/bash
find /FolderA -type f | while read f
do
    destfile=$(echo "$f" | sed 's#/FolderA#/FolderB#')
    if find $destfile -newer $f
    then
        echo "$destfile is newer than $f, skipping"
    else
        echo cp -p $f $destfile
    fi
done

When i run it, it looks like it will do everything correctly. I threw some random files in and changed a couple to test. I am getting an error where I think it is stumbling on a directory name with a space in it. I tried puting quotes on things here and there but to no avail. here is the line form the output.


Code:
find: /FolderA/new: No such file or directory
cp -p /FolderA/new folder/CODES.txt /FolderB/new folder/CODES.txt

so it looks like it stumbles but then copies the file in the directory anyway.
Is there a way to help this spaced directory error?
  #3 (permalink)  
Old 07-17-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
#!/usr/bin/ksh
find /srcdir -type f | while read f
do
    destfile=$(echo "$f" | sed 's#/srcdir#/destdir#')
    if find "$destfile" -newer "$f"
    then
        print "[$destfile] is newer than [$f], skipping"
    else
        print cp -p "$f" "$destfile"
    fi
done

  #4 (permalink)  
Old 07-17-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
I beleive it would be:

cp -p /FolderA/new\ folder/CODES.txt /FolderB/new\ folder/CODES.txt

will have to search for a space and add a \ then space.

Or just quote it at vgersh99 says on the next post.. :P
  #5 (permalink)  
Old 07-17-2008
wildside wildside is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 5
hmmm.... So it looked like it was going to work. I put those quotes in. I made an exact copy of the files in FolderA and put them in FolderB. (if the same everything should say it will copy) and I get this:

Code:
/FolderB/.DS_Store
/FolderB/.DS_Store is newer than /FolderA/.DS_Store, skipping
/FolderB/AnimalToysIcons/.DS_Store is newer than /FolderA/AnimalToysIcons/.DS_Store, skipping
/FolderB/AnimalToysIcons/Fasticon.com.url is newer than /FolderA/AnimalToysIcons/Fasticon.com.url, skipping
/FolderB/AnimalToysIcons/Icons/.DS_Store is newer than /FolderA/AnimalToysIcons/Icons/.DS_Store, skipping
/FolderB/AnimalToysIcons/Icons/Elephant is newer than /FolderA/AnimalToysIcons/Icons/Elephant, skipping
/FolderB/AnimalToysIcons/Icons/Giraffe is newer than /FolderA/AnimalToysIcons/Icons/Giraffe, skipping
/FolderB/AnimalToysIcons/Icons/Gorilla is newer than /FolderA/AnimalToysIcons/Icons/Gorilla, skipping
/FolderB/AnimalToysIcons/Icons/Lion is newer than /FolderA/AnimalToysIcons/Icons/Lion, skipping
/FolderB/AnimalToysIcons/Icons/Zebra is newer than /FolderA/AnimalToysIcons/Icons/Zebra, skipping
/FolderB/AnimalToysIcons/License - Read Me.url is newer than /FolderA/AnimalToysIcons/License - Read Me.url, skipping
/FolderB/C_PROJECTS/conversion.cpp is newer than /FolderA/C_PROJECTS/conversion.cpp, skipping
/FolderB/CODES.txt is newer than /FolderA/CODES.txt, skipping
/FolderB/new folder/CODES.txt is newer than /FolderA/new folder/CODES.txt, skipping

  #6 (permalink)  
Old 07-17-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
When you copy them it updates the timestamps, so all of the files will appear to be newer. Use cp -p to preserve the original timestamps when you create your test copy.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0