Issue
my bash script looks within the $dir any *.pdb filles which starts with the $mask keyword and then copy them to the $target_dir
mask1='MD_' #usually suffix
mask2='_after_equil' #usually posfix before the pdb extension
find "$dir" -maxdepth 2 -name "${mask1}*A.pdb" -exec cp {} ${target_dir} \; #
I would like to modify this script adding the IF condition in the following manner: if the pdb files with the $mask1 are not found in the $dir instead to look for the pdb files with the $mask2, then remove the $mask2 from the name of each file and add the $mask1 in the begining, and finally copy the renamed pdb files to the targer dir:
e.g this the following filles should be processed:
1rep_6nax_apo_300K_after_equil.pdb 3rep_6nax_apo_300K_after_equil.pdb
2rep_6nax_apo_300K_after_equil.pdb 4rep_6nax_apo_300K_after_equil.pdb
that should be renamed to
MD_1rep_6nax_apo_300K.pdb MD_3rep_6nax_apo_300K.pdb
MD_2rep_6nax_apo_300K.pdb MD_4rep_6nax_apo_300K.pdb
and then coppied to $target_dir
Solution
First 3 lines same as in your question (but quoting target_dir
) and then add a loop to do the rest:
mask1='MD_' #usually suffix
mask2='_after_equil' #usually posfix before the pdb extension
find "$dir" -maxdepth 2 -name "${mask1}*A.pdb" -exec cp {} "$target_dir" \;
while IFS= read -d '' -r old; do
file="${old##*/}"
new="${target_dir}/${mask1}${file%${mask2}.pdb}.pdb"
cp -- "$old" "$new"
done < <(find "$dir" -maxdepth 2 \( -name "*${mask2}.pdb" -a ! -name "${mask1}*A.pdb" \) -print0)
Answered By - Ed Morton Answer Checked By - David Marino (WPSolving Volunteer)