0

I'm not very good in shell scripting and would like to ask you some question about looping of files big dataset: in my example I have alot of files with the common .pdb extension in the work dir. I need to loop all of them and i) to print name (w.o pdb extension) of each looped file and make some operation after this. E.g I need to make new dir for EACH file outside of the workdir with the name of each file and copy this file to that dir. Below you can see example of my code which are not worked- it's didn't show me the name of the file and didn't create folder for each of them. Please correct it and show me where I was wrong

#!/bin/bash

# set the work dir 
receptors=./Receptors
for pdb in $receptors
do
  filename=$(basename "$pdb")
  echo "Processing of $filename file"
  cd ..
  mkdir ./docking_$filename
done

Many thanks for help,

Gleb

3 Answers3

1

If all your files are contained within the .Repectors folder, you can loop each of them like so:

#!/bin/bash
for pdb in ./Receptors/*.pdb ; do
    filename=$(basename "$pdb")
    filenamenoextention=${filename/.pdb/}
    mkdir "../docking_${filenamenoextention}"
done

Btw:

filenamenoextention=${filename/.pdb/}

Does a search replace in the variable $pdb. The syntax is ${myvariable/FOO/BAR}, and replaces all "FOO" substrings in $myvariable with "BAR". In your case it replaces ".pdb" with nothing, effectively removing it.

Alternatively, and safer (in case $filename contains multiple ".pdb"-substrings) is to remove the last four characters, like so: filenamenoextention=${filename:0:-4}

The syntax here is ${myvariable:s:e} where s and e correspond to numbers for the start and end index (not inclusive). It also let's you use negative numbers, which are offsets from the end. In other words: ${filename:0:-4} says: extract the substring from $filename starting from index 0, until you reach fourth-to-the-last character.


A few problems you have had with your script:

  • for pdb in ./Receptors loops only "./Receptors", and not each of the files within the folder.

  • When you change to parent directory (cd ..), you do so for the current shell session. This means that you keep going to the parent directory each time. Instead, you can specify the parent directory in the mkdir call. E.g mkdir ../thedir

swalog
  • 4,403
  • 3
  • 32
  • 60
  • In this case the `$filenamenoextention` will contain `./Receptors/filename` - what isn't correct. – clt60 Sep 05 '14 at 09:17
  • 1
    :) nearly... use double quotes `$(basename "$pdb")` because the filename can contain `` ;) - similarly, in the `mkdir`... – clt60 Sep 05 '14 at 09:24
  • Thank you very much! BTW I'm not an expert in SHELL scripting. Will it any difference to write the same code in csh instead of bach? What language will be better for person who is completely not a programmer (like me)? –  Sep 05 '14 at 09:38
  • You're welcome :) According to http://stackoverflow.com/questions/199661/what-linux-shell-should-i-use the most accepted opinion is to stick with `bash` unless you have a good reason not to. Seems that `csh` in particular should be avoided. – swalog Sep 05 '14 at 09:44
0

You're looping over a one-item list, I think what you wanted to get is the list of the content of ./Receptors:

...
for pdb in $receptors/*
...
0

to list only file with .pdb extension use $receptors/*.pdb

So instead of just giving the path in for loop, give this:

for pdb in  $receptors/*.pdb

To remove the extension :

set the variable ext to the extension you want to remove and using shell expansion operator "%" remove the extension from your filename eg:

ext=.pdb

filename=${filename%${ext}}

You can create the new directory without changing your current directory:

So to create a directory outside your current directory use the following command

mkdir ../docking_$filename

And to copy the file in the new directory use cp command

After correction Your script should look like:

receptors=./Receptors
ext=.pdb
for pdb in $receptors/*.pdb
do
  filename=$(basename "$pdb")
  filename=${filename%${ext}}
  echo "Processing of $filename file"
  mkdir ../docking_$filename
  cp $pdb ../docking_$filename

done
RBH
  • 572
  • 4
  • 11