Search code examples
bashdirectorypositionrenamefilenames

delete the characters of a filename from a specific position


i have 100 files in a folder with filenames as below

Example1: GCA_000696115.1_SoWt_assembly_genomic.fna 
          (0 to 15 characters are GCA_000696115.1)

Question1: remove characters after 15th character
          (i.e from, _SoWt_assembly_genomic.fna)
Example 2 : GCA_123456789.1xx_NgalHAMBI1146Draft_genomic.fna 
            (0 to 17 characters are GCA_123456789.1xx) 

Question2 : remove characters after 17th character
          (i.e from, _NgalHAMBI1146Draft_genomic.fna) 

in above examples i want to remove characters 
of the filename (after/from specific positions:
(15, 17 th positions) of the files in a folder 

Thank you in advance


Solution

  • Using rename (which you can install for your distro if you don't have it already), you can do

    rename -v -n -e 's/(^.{15}).*/$1.txt/' GCA*
    

    remove the -n to actually do the renaming.