Selectively create folders based on names in bash script

Viewed 39

I have several files called as follow:

dosulepin3D_CID_5284550.pdbqt
protriptyline3D_CID_4976.pdbqt

These are small molecules. Now I want to create a file in the Results folder for each one of these molecules, ignoring the 3D_CID_5284550.pdbqt they all have behind, and have folders called:

dosulepin
protriptyline

I want to do this with a for loop, since I'm also performing some functions with these files. This is what I have:

DIR="/home/roy/MolecularDocking/VirtualScreening/Dockings"
cd $DIR
for ligand in ligands/*; do
    echo $ligand
    mkdir "/home/roy/MolecularDocking/Results/$ligand"
done;

But this obviuosly creates folders with the full name.

1 Answers

Something like this.

DIR="/home/roy/MolecularDocking/VirtualScreening/Dockings"
cd "$DIR" || exit

for ligand in ligands/*.pdbqt; do
    echo "$ligand"
    echo mkdir -p "/home/roy/MolecularDocking/Results/${ligand%3D*}"
done

Remove the echo from the mkdir if you're satisfied with the output.

See Parameter Expansion for more details.

Related