I have about 1000 folders that I want to extract a single file from to upload to a server but I need to preserve the directory tree.
cp */myFile.txt ../newTree
Is what I basically want to do but instead of each file being saved to ../newTree/myFile.txt
I want it to be ../newTree/*/myFile.txt
where the *
is the wildcard from the cp
command.
I couldn't find a flag in the man file for this so I'm thinking I may need another utility besides 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With