Say I have multiple files
file-replace-1.ext
,file-replace-2.ext
,...,file-replace-10.ext
to be copied to locations ~/tree/loc1
,~/tree/loc2
,...,~/tree/loc5
replacing the part replace
with corresponding loc*
.
I have been using
for i in *replace*; do cp "$i" ~/"tree/loc1/${i/replace/loc1}"; done
to get it done for just loc1
. How can I batch copy this way to all the locations?
Update
I used loc1
,loc2
, etc for simplicity. The code I am looking for should work for any subdirectory name in a parent directory. For example the subdirectory names could be the names of months.
Use a find command, printing only the directory within the tree structure and incorporating this within within your loop and so:
for i in *replace*
do
find ~/tree -maxdepth 1 -type d -printf "%f\n" | while read line
do
if [[ "$line" != "tree" ]]
then
cp "$i" ~/tree/"$line"/"${i/zipped/$line}"
fi
done
done