Copy files from one drive to another, keeping most recently modified files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copy files from one drive to another, keeping most recently modified files
# 1  
Old 04-22-2014
Question Copy files from one drive to another, keeping most recently modified files

Hi all, I am a bit of a beginner with shell scripting..

What I want to do is merge two drives, for example moving all data from X to Y.

If a file in X doesn't exist in Y, it will be moved there.
If a file in X also exists in Y, the most recently modified file will be moved to (or kept) in Y.

I have done some reading on the forums and it seems rsync could be helpful, but the most important factor in this exercise is to only keep the most recently modified file, which I am not sure rsync does...

Do any of you have any recommendations? Thanks for the help!
# 2  
Old 04-22-2014
Replace /x/path with the value of X and
/y/path with value of Y
Code:
for i in /x/path/*
do
  if [[ ! -f /y/path/${i##*/} || ${i} -nt /y/path/${i##*/} ]]; then
    cp ${i} /y/path/${i##*/}
  fi
done

---------- Post updated at 06:08 AM ---------- Previous update was at 06:07 AM ----------

Here I have used cp to copy the file.
If you want to move, you could use 'mv' instead of 'cp'
These 2 Users Gave Thanks to SriniShoo For This Post:
# 3  
Old 04-22-2014
Hi SriniShoo, thanks for the solution. Just so I can try to understand and assist with my scripting learning, is this what your script does?:

Code:
for i in /x/path/*    //for file i within /x/path/example/example1
do
  if [[ ! -f /y/path/${i##*/} || ${i} -nt /y/path/${i##*/} ]]; then   //if file i in /x/path/example/example1 exists then see if file i from X drive is less than file i from Y drive
    cp ${i} /y/path/${i##*/}     //if file i from X drive is less than file i from Y drive (or it doesn't exist in Y drive) copy file i from X drive to same location in Y drive.
  fi        // end if
done     // end do loop

I have a quick question, what does ${i##*/} give? And how does the script know to use date modified? Is there a command for that, or is it used by default?

Thanks!
# 4  
Old 04-22-2014
${i##*/} this is used to extract base file name from the entire name including path
If $i is /x/path/ADC.DAT
${i##*/} represents ADC DAT

And -nt in the if condition is checking for newer file
# 5  
Old 04-22-2014
Great thanks for the explanation! I ran the script and it works for the base folder, but omits the directories under.
e.g. X/path/1
Files in /path are copied correctly
cp: omitting directory `./path/1'

How can I make the script recursive?
# 6  
Old 04-22-2014
Replace the for line with below
Code:
for i in $(find /x/path -type f)

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Modified or latest files copy from windows to Linux

To copy the file from windows to linux i use pscp command(pscp source user@destination). Know i want to copy the latest modified or created files from windows to linux. could any one please help me out with it. Thanks and Regards, Sourabh (2 Replies)
Discussion started by: SourabhChavan
2 Replies

2. Shell Programming and Scripting

How to copy the two most recently added files to another directory - HP-UX?

Hello, I am attempting to find and copy the two most recently added files to a specific directory, that fit a specific format. I was able to find the command to list the two most recently added files in directory: ls -1t | head -n 2 The command lists the two files names in a vertical list,... (11 Replies)
Discussion started by: mattkoz
11 Replies

3. Shell Programming and Scripting

I want to copy all files of a said type on my external hard drive

My code is this, what I'm trying to accomplish is to make a list of all pdf documents in my computer and then copy all of those documents to my external hard drive in a directory mkdir /Volumes/Hardrive-1/allpdf echo "File Locations" > /Volumes/Hardrive-1/allpdf/FileLocations.txt mdfind pdf... (2 Replies)
Discussion started by: darpness
2 Replies

4. Shell Programming and Scripting

subtring and copy recently files and another folder

Hi I have the following files A320_ZONAL_v24_-_AMM_Suffix_jess_2011-05-31.xls Jun 10 17:21 A320_ZONAL_v24_-_AMM_Suffix_jess_.xls Nov 10 17:21 A320_ZONAL_v24_-_AMM_Suffix_jess_2011-03-31.xls Nov 23 20:21 And I need only the more recent one using UNIX timestamp... (2 Replies)
Discussion started by: javeiregh
2 Replies

5. Shell Programming and Scripting

Using RCP to copy and delete the files from Windows Drive

Hi, I have a requirement to copy the files from C drive on Windows to UNIX, once the files are copied I need to delete them from that drive (C:). A drive is also on same network as my unix, so I was asked to use RCP for copying the files. Can any one have the syntax to copy the files from... (0 Replies)
Discussion started by: Raamc
0 Replies

6. UNIX for Advanced & Expert Users

command for recently modified files - "find" command not working

I have three files a.txt , b.txt , c.txt in a directory called my_dir1 .These files were created before two or three months . I have a tar file called my_tar1.tar which contains three files a.txt , b.txt , d.txt . Somebody untarred the my_tar1.tar into my_dir1 directory. So existing two files were... (1 Reply)
Discussion started by: joe.mani
1 Replies

7. Shell Programming and Scripting

Trying to Copy Files Changed Recently

I have been toying around with a script that will copy all files altered in a development directory over to a testing directory and have been trying to construct the command to meet my needs. Basically I am using find in a directory to see what files have changed over the past 24 hours. Then if... (4 Replies)
Discussion started by: scotbuff
4 Replies

8. UNIX for Dummies Questions & Answers

chmod command for recently modified files

hello! is there a way for me to use the chmod command to change permissions for several files all at once -based on the fact that these files were all most recently modified TODAY ? I can't use a wildcard on their filenames because the filenames are varied. But I was hoping I could somehow do... (2 Replies)
Discussion started by: polka_friend
2 Replies

9. UNIX for Dummies Questions & Answers

How do copy certain files and directories from one hard drive to another?

I have two drives (scsi) mounted on one server and I need to copy certain files and directories from one drive to the other. Any suggestions :confused: (4 Replies)
Discussion started by: shorty
4 Replies

10. UNIX for Dummies Questions & Answers

copy files from local drive to telnet unix machine.

i want to run some solaris executable program (text file). i telnet to solaris machine. the text file is on the net drive h: or my local drive on windows system. how can i copy the text file from local drive in windows to remote machine in solaris system? thanks. (4 Replies)
Discussion started by: gary
4 Replies
Login or Register to Ask a Question