LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scripting help copying files from a NTFS partition (http://www.linuxquestions.org/questions/linux-newbie-8/scripting-help-copying-files-from-a-ntfs-partition-784104/)

Tamaros 01-22-2010 06:17 PM

Scripting help copying files from a NTFS partition
 
I record my lectures at school using my mobile phone then send them over wifi to my laptop and use a program to do volume correction all in windows. However, I want a exact copy of that folder in my home folder on my ubuntu install on a separate partition.

I've been manually copying them over so far but I want to make a script that I can run to copy all new files over.

I know a little bit about scripting, mounting the drive and actually copying files is easy, its figuring out how to determine new files and copy only those that I don't know how to do.

antegallya 01-22-2010 08:21 PM

Hello,

there's a great tool for file synchronisation that will determine which are new or changed. It's name is rsync. There's plenty of options.
Note that it's not usually installed by default so :
Code:

sudo apt-get install rsync
This would do the trick (have a look at man rsync for details) :
Code:

rsync -a source_directory destination_directory
(It only helps for the file synchronisation, you still need to mount your partition)

Hope it helps.

Tamaros 01-23-2010 01:56 AM

I thought I had this one in the bag!

OK . . .

I set a variable for the source directory, something like

Code:

NTFS=/mnt/ntfs/Users/name/Documents/School/Spring\ 2010/
and a source directory of

Code:

/home/name/School/Spring\ 2010/
then I'm calling rsync with

Code:

rsync -ar --progress $NTFS $DEST
I added the 'r' for recursive because within the 'Spring 2010' directory I have a directory for each class and then a directory for lectures and other related documents etc., etc.

It didn't react as I expected. In the man page i read something about "--protect-args (-s)" in relation to whitespace so I added that


Code:

rsync -ars --progress $NTFS $DEST
Same exact behavior.

Code:

sending incremental file list
rsync: link_stat "/mnt/ntfs/Users/name/Documents/School/Spring" failed: No such file or directory (2)
rsync: change_dir "/home/name/Desktop/2010" failed: No such file or directory (2)
rsync: link_stat "/home/name/School/Spring" failed: No such file or directory (2)
created directory 2010

sent 18 bytes  received 12 bytes  60.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1057) [sender=3.0.6]

I have the script file located on the desktop which is the only explanation i can think of for it to show '/home/name/Desktop/' as a file path in any of the errors. I had thought that using absolute file-paths would make the location of the script itself irrelevant (or did I do that incorrectly)

the directory 2010 referenced at the end of the output was created on the Desktop as well.

Here's the whole script if that helps anyone help me.

Code:

#!/bin/bash

# lecturesync, version 1

# invoke with 'sudo'

# mount point stored in variable
MNT_DIR=/mnt/ntfs

# hard drive device identifier
HD=/dev/sda2

# ntfs source directory
NTFS=/mnt/ntfs/Users/name/Documents/School/Spring\ 2010/

# destination directory
DEST=/home/name/School/Spring\ 2010/

# mount the Win7 ntfs drive
mount -t ntfs $HD $MNT_DIR

# sync the files
rsync -ars --progress $NTFS $DEST


antegallya 01-23-2010 08:18 AM

Hello,

that's only a little problem of escapes and substitution. In fact when you set
Code:

$ DEST=/home/name/School/Spring\ 2010/
$ echo $DEST
/home/name/School/Spring 2010/

So the rsync command you are using don't use escaped spaces. Think of the subsitution the shell does, after substituting the executed command line is :
Code:

rsync -ars --progress /mnt/ntfs/Users/name/Documents/School/Spring 2010/
 /home/name/School/Spring 2010/

The -s option is protection of the spaces to pass to the remote host, it doesn't affect the parsing of rsync args.

The solution is simply to add quotes
Code:

rsync -as --progress "$NTFS" "$DEST"
Note that I removed the -r option, because it is implied by -a. Following the man page :
Quote:

-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
Hope it works now.

Tamaros 01-23-2010 10:44 AM

Worked like a charm!

Thank you very much.


All times are GMT -5. The time now is 06:59 PM.