BASH SCRIPT: copy many files (one name) contained in a folder with different folders

I tried turning a Jaypal response ( see here ) into a bash script this way:

#!/bin/bash pwd mkdir -v ../g shopt -s globstar for file in ./**/file.txt do echo "will copy $file" cp -v --parents "$file" ../g done 

But that will not work! (It only creates an empty g folder)
What is the problem?
note: script is in f folder

Thanks!:)

+1
source share
2 answers

Answering a title question:

 source_dir=/some/dir target_dir=/some/other/dir file_name=myfile.txt cd $target_dir while read path; do mkdir -p $(dirname $path) cp $source_dir/$path $target_dir/$path done < <(cd $source_dir; find . -name $file_name -type f) 

this duplicates all $file_name found in $source_dir in $tagert_dir

As for your second question (why is your code not working), maybe you do not have bash 4+, and therefore globstar is not working? Or maybe it returns something else, which is the relative path to the file (I thought, but I can’t check, since I have bash 3.2)

+1
source

Since feature requests mark a comment as an answer , remain valid, I copy the above solution here.

OK. I decided to use only 1 * in for file in. /*/file.txt. Thus, it works correctly. Thank you! :) - fibon82

0
source

Source: https://habr.com/ru/post/1393216/


All Articles