Search and Copy Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and Copy Script
# 1  
Old 12-13-2010
Search and Copy Script

Dear All,

I would like to create a Unix script which basically searches for files which are more than 2 days old and copy only the new files to the destination. What i mean is if the destination may have most of the files, it may not have only last 2 to 3 days file.

I am able to create the script which basically search and copy which are older than 2 days but how would i check if the destination has any of the files and how would i skip them? Also is there any other better command i can use other than "cp"?

Thanks,

RR
# 2  
Old 12-13-2010
if you have rsync available this is ideal tool - it will not re-copy the files that have been copied already
man rsync
This User Gave Thanks to migurus For This Post:
# 3  
Old 12-13-2010
I agree rsync is the tool of choice, if you don't have it the following should do what you want:

Code:
SRC=/path/to/source/files
DEST=/path/to/dest/files
cd $SRC
find . -type f -mtime -2 | while read file
do
    DDIR="$DEST/$(dirname $file)"
    if [ ! -f $DEST/$file ]
    then
        [ -d "$DDIR" ] || mkdir -p $DDIR
        cp -p $file $DDIR
    fi
done


Last edited by Chubler_XL; 12-13-2010 at 09:11 PM.. Reason: Fix typo
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 12-14-2010
Thanks to both of you for your help. I have the rsync installed, I am also looking at the options.

But how will I use the rsync to copy the files which is more than 2 days old? Can i use something like this
Code:
for i in ' find /path_to_find -type f -mtime -2 -exec ls {} \;' do 
begin 
  rsync $i /destination_path 
done

let me know. I am new to scripting and please explain if I am wrong.

Last edited by Scott; 12-14-2010 at 04:17 PM.. Reason: Code tags, please...
# 5  
Old 12-14-2010
I guess following should be enough
Code:
 
rsync -av <source_dir> <destination_dir>

# 6  
Old 12-14-2010
I suppose the thing is rsync manages all this for you, it examines all the files in the source directory and any that have changed, are missing (or optionally have been removed) are synced up with the destination directory.

So no need to worry about anything changed in the last x days anymore. If you have stuff in the source you want to exclude from being copied to dest you will need to use the --exclude=PATTERN or --exclude-from=FILE options of rsync.
This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 12-15-2010
Thanks, I will try that option in rsync
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and Copy Files

Hi All Need your help, I am looking for a script to search for files with specific extension as .log and then copy the latest one to a different folder. Here is the scenario /dev/abc/xyz/a_2_122920131.log /dev/abc/xyz/a_2_123020131.log /dev/abc/xyz/b_2_12302013.log... (2 Replies)
Discussion started by: jimmun
2 Replies

2. UNIX for Dummies Questions & Answers

Script to search and copy files

HI everyone, I been to this site before for help and found my answers on other threads now I am posting my own :). I have a list of file names with out extensions on an txt file. I need a way for the script to search on the server for each file name and copy the files over to a new directory.... (12 Replies)
Discussion started by: sergiol
12 Replies

3. Shell Programming and Scripting

recursive search and copy

Hello again. Well, I need help again sooner as I thought. Now I want to search for files with a known name within all subdirs, and copy the to differently named files in the same directory. For example if I had only one file to copy, I would just usecp fileName newFileNamebut to do this... (1 Reply)
Discussion started by: cabaciucia
1 Replies

4. Shell Programming and Scripting

sed help - search/copy from one file and search/paste to another

I am a newbie and would like some help with the following - Trying to search fileA for a string similar to - AS11000022010 30.4 31.7 43.7 53.8 60.5 71.1 75.2 74.7 66.9 56.6 42.7 32.5 53.3 I then want to replace that string with a string from fileB - ... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

5. Shell Programming and Scripting

Shell Script to Search for a particular String and copy the timestamp to a variable

Hi, We Perfrom Loads to the database through a Perl script which generates a statistics file. I need to read the statistics. the Statistics file looks something like below: Process Beginning - 08-26-2010-23.41.47 DB2 CONNECTION SUCCESSFUL! Ready to process and load file: FILENAME # of... (2 Replies)
Discussion started by: Praveenkulkarni
2 Replies

6. Shell Programming and Scripting

shell script to search and copy files

Hello Im new to this forums, I would like some help regarding a script that I need in order to copy some files. Heres the scenario: I need to search several files which have a particular code inside, lets say "test" all of them on different directories. I need to copy all of them on a new... (4 Replies)
Discussion started by: c.watson
4 Replies

7. Shell Programming and Scripting

Search, copy and paste

Can i search in a file for more than one string at a time? And copy the next string after that and paste it in column style? Is it possible? Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

8. Shell Programming and Scripting

Search and copy to a new file-Help

Hi All, server16.na.in.com UNKNOWN ftpuser "CWD" dms-imrm/Delasco_Invoices_DayForward_Scan" 250 - server16.na.in.com UNKNOWN ftpuser "PWD" 257 - server16.na.in.com UNKNOWN ftpuser "CWD Private" 250 - server16.na.in.com UNKNOWN ftpuser "PWD" 257 - server16.na.in.com UNKNOWN... (7 Replies)
Discussion started by: Tuxidow
7 Replies

9. Shell Programming and Scripting

Perl script to search a line and copy it to another line

Hi I have a log file (say log.txt). I have to search for a line which has the string ( say ERROR) in the log file and copy 15 lines after this into another file (say error.txt). Can someone give me the code and this has to be in PERL Thanks in advance Ammu (3 Replies)
Discussion started by: ammu
3 Replies

10. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies
Login or Register to Ask a Question