1

I have thousands of txt files with a .pdbqt extension in a directory named 100000cpds_ligand_00001.pdbqt, 100000cpds_ligand_00002.pdbqt, 100000cpds_ligand_*****.pdbqt and would like to rename them according to a unique identifier from their contents.

The identifier is given as REMARK Name = PV-000009607404 in a single line, where PV-000009607404 can be of variable length, text, and numbers (e.g. ZINC001248485427, etc.). I would like to only keep the identifier part in the file name and not the REMARK Name = part. So, I would e.g. change 100000cpds_ligand_00001.pdbqt to PV-000009607404.pdbqt. I can do it from the command line, but lprefarably I would like to use a small script.

Thank you very much for your help!

2 Answers 2

1

I have done an example which should only copy files instead of rename. Comments indicate some of the logic. I'm not a bash expert, but I tested this on a file.

You should create this file in the same directory as your source files, and name it something like pdbqt-mover.sh.

Either call it by typing bash pdbqt-mover.sh or you'll have to set permissions on the file so you can execute it using ./pdbqt-mover.sh.

#!/bin/bash
# process only files starting with 100000cpds_ligand_ and ending in .pdbqt
for infile in 100000cpds_ligand_*.pdbqt; do

  # get the pv-* string from lines beginning with "REMARK  Name = "
  # get the group matching PV-**** up to the end of the line
  pvnum=$(sed -n 's/^REMARK  Name = \(PV-\S*\)/\1/p' "$infile")

  # construct the output filename
  outfile="$pvnum".pdbqt

  # only copy if the output file doesn't exist
  if [ ! -f $outfile ] ; then
    # cp instead of mv
    cp "$infile" "$outfile"
    echo "Copied $infile to $outfile"
  else
    echo "File $outfile exists! Skipping"
  fi
done
1
  • Hi Stephan, you should add your command line work to your question to show what you've tried and how it is working for you. Welcome!
    – jtessier72
    Jun 2, 2020 at 10:46
0

This is very easily done using grep and also a terminal file browser called RANGER. First use grep to pull out the lines containing filenames. For example

grep REMARK * > fnlist

edit fnlist and get rid of the redundant string of "REMARK NAME = ". You now have a clean list of new file names.

Launch Ranger, navigate to the directory containing your files and do a bulkrename operation. Paste the contents of nflist when presented and you are done.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .