0

I have hundreds of files in a directory with the same suffix and unique alphanumeric prefixes like this:

ABC01234.sorted_dup.bam

ABC04271.sorted_dup.bam

ABC09287.sorted_dup.bam

I have a file with new unique alphanumeric prefixes in one column spefic to each of the original prefixes like this:

GBH03987 ABC01234

GBH05430 ABC04271

GBH07651 ABC09287

I would like to replace the original prefixes with the correct prefixes from the file i.e. replace the prefixes in the filenames in the directory with the prefixes in column 1. The resulting filenames will have prefixes that are based on column 1 like this:

GBH03987.sorted_dup.bam

GBH05430.sorted_dup.bam

GBH07651.sorted_dup.bam

What is the easiest solution for someone new to unix? Preferably an awk or sed solution would be greatly appreciated.

2
  • So you're renaming files based on the contents of this one file? If you show the resulting filenames, it would make it crystal clear.
    – Jeff Schaller
    Dec 11 '18 at 14:03
  • Yes. The renaming is based on the contents of the file. I will update the post to show the resulting filenames.
    – newbieIP15
    Dec 11 '18 at 14:12
2

For this case, a simple shell loop should suffice:

while read -r new old; do 
  [ -f "$old.sorted_dup.bam" ] && echo mv -- "$old.sorted_dup.bam" "$new.sorted_dup.bam"
done < filenames

where filenames is the name of the file containing the mappings. Remove the echo after testing.

2
  • my comment posted before I completed it. this worked for a trial run in which I had only one line in the file with the mappings. When I extended it to cover all the mappings, the renaming didn't work and I am not sure what I am missing. From the manual I gather that read -r splits stdin line input into fields, which facilitates the mapping of the prefixes (in this case my colomns input). -f restricts the loop to be effective so long as I still have the old file in the directory so that the process doesn't re-write completed jobs. what do the dashes after mv achieve?
    – newbieIP15
    Dec 11 '18 at 18:29
  • @newbieIP15 the double dash indicates the end of command options - it's useful when filenames might start with a hyphen (not the case for the names you've shown, but good practice to allow for). See for example What is -- called? Dec 11 '18 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.