Oren
Oren

Reputation: 5309

Copy recursively X folders deep linux

I want to copy a folder to another folder, but I want only 2 layers deep.. I know I can do cp -rf ... ... but this will copy all the layers...

Example ( The folders name are just an example)

I have Book/science/1/ext And I want to copy it, but only Book/science/1

I don't want the 3rd layer, Is it possible?

Thank You.

Upvotes: 0

Views: 5064

Answers (2)

genisage
genisage

Reputation: 1169

Elliot's answer works for me, but makes an intermediate archive. If you want to do it without one the commands:

find src-dir/ -maxdepth 2 -type d -exec mkdir -p dest_dir/{} \;
find src_dir/ -maxdepth 3 -type f -exec cp {} dest_dir/{} \;

Will recreate the directory structure under dest_dir, and then look for all of the files in src_dir that are less than three levels deep and copy them to the structure in dest_dir (note the /{} after dest_dir in both commands).

It will miss symbolic links and I'm really bad at the maxdepth and mindepth options, so they might be off by one.

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201399

Yes, here is one possible way using find and then tar to perform the copy in a block way and to keep the directory structure

find . -maxdepth 3 | grep -v "^.$" | xargs tar cfp - | \
    (cd /destination_folder ; tar xvvf -)

Upvotes: 1

Related Questions