Preserve directory tree while copying files with cp Preserve directory tree while copying files with cp shell shell

Preserve directory tree while copying files with cp


With rsync:

find ./ -name myFile.txt -print0|rsync -0adv --files-from=- ./ ../newTree/

Without rsync:

You can find all files, for each file you create the directory in the newTree, and copy the file to it.

for file in */myFile.txt; do    dir=$(dirname "$file")    mkdir -p "../newTree/$dir"    cp "$file" "../newTree/$dir"done


Store all the files in a tar archive , then extract it on the server.