0

I have 2 independent folders A and B. B has many files with the extension .build. Across A there are a fe subdirectories that have the same structure as subdirectories of B.

For example A has some_path/Tools/Camera/ and B has different_path/Tools/Camera. Say I manually identified 2 subdirectories one in A one in B that have the same structure, I need to copy all .build files from the subdir of B into the one in A.

How would I do this?

3 Answers 3

1

Enable the globstar Bash shell option: shopt -s globstar

Now change directory into B and run:

for path in **/; do
    [ -d "<A-dir>/$path" ] && cp -n "$path/"*.build "<A-dir>/$path"
done

This will recursively check each subdirectory in B and see if there is an equivalent subdirectory in A. If there is then it will copy all .build files from the B subdirectory over to A.

1

There are many ways to do that.

Here's one that comes to mind.

cd different_path
find . -iname '*.build' | while read filename ; do 
    cp -n "$filename" "some_path/${filename#./}" 
done

If there's any chance you have some poorly named files with linebreaks in their names or similar, you might instead use a null separator:

cd different_path
find . -iname '*.build' -print0 | while IFS= read -r -d $'\0' filename ; do 
    cp -n "$filename" "some_path/${filename#./}" 
done
2
0

With zsh:

autoload -Uz zmv # best in ~/.zshrc
zmv -C 'A/(**/)(*.build)' 'B/$1$2'

If the corresponding subdirectories are not guaranteed to exist in B, you can change it to:

mkdcp() { mkdir -p -- $2:h && cp -- "$@"; }
zmv -P mkdcp 'A/(**/)(*.build)' 'B/$1$2'

Where zmv uses our mkdcp function instead of cp (with -C) to copy the files.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.