Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I recently had my second hard-drive of the past semester begin to fail on me and become unable to ghost to a new drive. I still had the first hd that failed so we ghosted that drive but now I need to get all the files and things that have changed since the date of the first ghosting (first failed hd -> second failed hd) onto the new drive (all files on second failed hd since a date -> third working hd). Is there an easy way to script this in the linux terminal or use another program?

I've looked into using the following commands but they haven't been successful and fail after the first few folders have been archived:

find . -type f -mtime -58 |xargs tar --no-recursion -czvf allmodified.tgz

tar czvf allmodified.tgz `find . -type f -mtime -58`

The find code works and locates all the files on the drive I need but I still can't find a way to get them to archive with file structure so that I can unpack the files on the new drive (third working hd) and they will be preserved where they were in the past 2 months.

I'm not the best at describing my problems so if you have confusion please ask questions and I'll do my best to answer them.

Any hints help, thank you so much!

share|improve this question

1 Answer 1

They probably fail because you haven't safeguarded against whitespace in filenames in your second example. The invocation is a bit different:

find . -type f -mtime -58 -print0 | tar -czvf allmodified.tar.gz --null -T -

Here, find outputs file paths terminated by NUL characters. They are piped to tar, which reads input from stdin (-T -) delimited by null (--null). The --no-recursion isn't necessary since you will not include directories themselves in your tarball.

See also: GNU tar 1.27: 6.3 Reading Names from a File

You could then extract the archive from the same directory you took it, and it'd place the files in the directories they belong to. Note that tar will overwrite existing files upon extraction. To prevent that, use the --skip-old-files option.


As for your second example, it will fail if a file or path contains whitespace, because tar sees the two files foo bar and baz (which find outputs) as three, i.e. tar foo bar baz. The proper way to deal with this is to pipe the output of find with -print0 to the next command, if that command has a switch to read from NUL-delimited input (which most GNU utilities do).

share|improve this answer
    
Using this method would I then be able to 'unzip' the tar on my new drive and have it place files accordingly? –  Samuel Kane Nov 15 '13 at 18:48
    
Yes. Just tested it on a set of sample files. Note that your tarball has to be extracted from the same root directory as it was tarred in. Also, tar will overwrite existing files. To prevent that, use --skip-old-files when extracting. –  slhck Nov 15 '13 at 18:53
    
So if I tarred it in the /media/ubuntu/OS directory of one, then I would need to move the tar to the /media/unbuntu/OS1 folder for the other drive? Could you also elaborate on how the --skip-old-files works when extracting (I'm really a novice at this), I'm not 100% confident I'm going to do this correctly and I'm going to ruin a lot of things. Thank you so much for your help –  Samuel Kane Nov 15 '13 at 18:59
    
If you ran find … | tar … while you were in /media/ubuntu/OS/, then cd /media/ubuntu/OS1/ and tar -xcvf allmodified.tar.gz --skip-old-files, then only the missing files in OS1 are created from your OS backup. If you're unsure what will happen, try creating a smaller test archive first. Or list the contents of the archive with tar -tvf allmodified.tar.gz and imagine that tar creates each of those with the path it spits out (e.g. if tar -tvf writes ./foo/bar, then the file /media/ubuntu/OS1/foo/bar will be created). In any case, I'd recommend a small test first. –  slhck Nov 15 '13 at 19:13
    
fantastic! thank you so much! I'll let you know the results once I've done the test and the whole project –  Samuel Kane Nov 15 '13 at 19:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.