2

Please, I need help with backup... I am trying to do something and I have NO IDEA how to start. I want to make a script in Ubuntu that would search through all folders of my W7 partition, and wherever there are .cdr files it would ( select and ) copy all files from that folder ( .jpg, .png, .pdf, ...) but not other folders in that folder. Then it would paste those files onto my external drive in folder named exactly as the one those files were copied from.

I KNOW IT'S complicated, and my English is not very good.

I hope you can understand what I am struggling with and help me.

Any help or even advice is very much appreciated. THANK YOU !

2

Script

Not too terrible with some find, rsync and bash script magic. Put this into a file (backup.sh):

#!/bin/bash

source_dir=${1:-<source directory>}
dest_dir=${2:-<destination directory>}
tmp_file=/tmp/$USER/bkp_files

find "$source_dir" -name '*.cdr' -print | while read file; do
    find "$(dirname $file)" -maxdepth 1 -xtype f -print
    done > "$tmp_file"

rsync --files-from "$tmp_file" / "$dest_dir"

Then you have to make it executable with

$ chmod +x backup.sh

And you can run the script with

$ ./backup.sh

Step by step

Let me explain

find "$source_dir" -name '*.cdr' -print

finds all the .cdr files in $source_dir This is then piped into a while loop:

| while read file; do ...  done > "$tmp_file"

This takes every file from the previous find command, executes some commands and writes the result to $tmp_file. Note: This only works if the filename don't contain newlines, which should be given with a windows partition. The second find is

find "$(dirname $file)" -maxdepth 1 -xtype f -print

Here $(dirname $file) gives us the directory name of the .cdr file. It is the start point for our next find command. There we don't descend into subdirs with -maxdepth 1 and are only interested in files -xtype f

Lastly we use rsync to copy the files we saved in $tmp_file:

rsync --files-from "$tmp_file" / "$dest_dir"

rsync will keep the directory tree structure as is, which is what you want, right?

For more infos see the man pages:

$ man find
$ man rsync
| improve this answer | |
  • Thank you very much ! but can I use it the same way to manage files between two USB drives ? – user167302 Aug 26 '13 at 0:12
  • Sure, just change the source_dir and ``dest_dir` or pass them to the script. – ahilsend Aug 27 '13 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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