1

I'm making a bash script that should backup all files and dir structure to another dir. I made the following code to do that:

find . -type f -exec cp {} $HOME/$bdir \; -o -type d -exec mkdir -p {} $HOME/$bdir \; ;

The problem is, is that this only copies the files and not the dir structure.

NOTE: I may not use cp -r, cp -R or something like it because this code is part of an assignment.

I hope somebody can put me in the right direction. ;)

Joeri

EDIT:

I changed it to:

find . -type d -exec mkdir -p $HOME/$bdir/{} \; ;
find . -type f -exec cp {} $HOME/$bdir/{} \; ;

And it works! Ty guys ;)

1
  • if your original command had ended with ... mkdir -p $HOME/$bdir/{} I think it would have worked. Commented Jan 9, 2013 at 4:22

3 Answers 3

1

This sounds like a job for rsync.

You mention that this is an assignment. What are your restrictions? Are you limited to only using find? Does it have to be a single command?

One way to do this is to do it in two find calls. The first call only looks for directories. When a directory is found, mkdir the corresponding directory in the destination hierarchy. The second find call would look for files, and would use a cp command like you currently have.

You can also take each filename, transform the path manually, and use that with the cp command. Here's an example of how to generate the destination filename:

> find . -type f | sed -e "s|^\./|/new/dir/|"
/new/dir/file1.txt
/new/dir/file2.txt
/new/dir/dir1/file1_1.txt
/new/dir/dir1/file1_2.txt

For your purposes, you could write a short bash script that take the source file as input, uses sed to generate the destination filename, and then passes those two paths to cp. The dirname command will return the directory portion of a filename, so mkdir -p $(dirname $destination_path) will ensure that the destination directory exists before you call cp. Armed with a script like that, you can simply have find execute the script for every file it finds.

Sign up to request clarification or add additional context in comments.

Comments

1
cd olddir; tar c . | (cd newdir; tar xp)

1 Comment

came across this while searching for a way to move a directory, its structure, and all its contents, to another directory. Your options works like a charm and is quite compact. thanks
0

Can you do your find with "-type d" and exec a "mkdir -p" first, followed by your find that copies the files rather than having it all in one command? It should probably also be mkdir -p $HOME/$bdir/{}.

2 Comments

you could merge them into the same find with two different expressions for what it's worth.
just noticed that, except he's put the {} in the wrong place in the mkdir. I think the original should have worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.