Help. noob needs help with file copy!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help. noob needs help with file copy!
# 8  
Old 07-17-2008
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
# 9  
Old 07-17-2008
Quote:
Originally Posted by Annihilannic
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?
# 10  
Old 07-17-2008
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
# 11  
Old 07-17-2008
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

# 12  
Old 07-17-2008
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

# 13  
Old 07-17-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh CSV to XML file (noob tier)

Hey all, I'm very new to shell scripting and would love some help. I have been messing around with KSH at my job, and have been tasked with generating an XML file from multiple CSV files. However, I barely even understand the syntax for for loops! Output should be something along the lines of ... (2 Replies)
Discussion started by: Parrakarry
2 Replies

2. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

3. UNIX for Dummies Questions & Answers

Noob questions.. Append output to a file in different directory

Noob question! I know almost nothing so far, and I'm trying to teach myself from books, on a typical command line without using scripts how would I append output from a sort to a file in a completely different directory? example: If I'm sorting a file in my documents directory but I... (2 Replies)
Discussion started by: Byrang
2 Replies

4. Shell Programming and Scripting

Noob's 1st...bash-script for copying one file into many

I have one file "file.a.b.c-d.r" that I would like to use to spawn 4 other files: "file.a.b.1-A.r" "file.a.b.1-B.r" "file.a.b.1-C.r" "file.a.b.1-D.r" where the field "c-d" changes into my 1 and A-D. I was doing this manually at the prompt with > cp "file.a.b.c-d.r" "file.a.b.1-A.r" >... (13 Replies)
Discussion started by: WSUToad
13 Replies

5. UNIX for Dummies Questions & Answers

I'm a noob at Lynx...

So... I'm using lynx on a Mac... I didn't know that the whole dang thing is in Terminal? I rarely use terminal. I can't even figure out how to start the thing up. I type in lynx into it and press enter... And the cursor just goes down. Nothing happens like the FAQ websites describe. I feel so... (6 Replies)
Discussion started by: yennster
6 Replies

6. UNIX for Dummies Questions & Answers

Noob on Unix.

This may seem really easy to alot of you but i am a real noob on unix. I have been set the task to make a script which will answer a query. Basically I will have made files for people and i want to be able to search for a persons file and then select certain variables from the files. e.g... (7 Replies)
Discussion started by: bobtheb
7 Replies

7. Linux

noob help needed

i'm having trouble putting together a program :( any help would be much appreciated! Write a Shell Program to automate the process of collecting assignments from the directories of students of any specified class. The person running the program should be able to pass a parameter to the... (1 Reply)
Discussion started by: ace_face
1 Replies

8. UNIX for Dummies Questions & Answers

complete noob

Hi all, This is my first post. I am a complete noobie to the UNIX OS, I have an iMac G5 with the unix shell built in and am interested in learning how to use it to do things useful with it, but have no idea where to start. I have read over the basic commands but they haven't helped me much yet.... (3 Replies)
Discussion started by: avdrummerboy
3 Replies

9. UNIX for Dummies Questions & Answers

I am a unix noob

Hello i am new to this forum. I signed up here really to ask one question. I recentaly got a old unix server from my work and i never really understood what unix is or what is does. Dont get me wrong i and very smart with computers as long as its windows, mac, or linux i can use them all but i... (4 Replies)
Discussion started by: alt+f4
4 Replies
Login or Register to Ask a Question