bashsedbasename

Permission denied error using sed to change file name


I'm trying to take the filename of each file in a directory and 'rename' it to create a respective output file when running through a program. When running the script I get the error Permission denied, for the line that is meant to be doing the renaming.

outputname=basename $file | sed -e "s/_Aligned.sortedByCoord.out.bam/_.gtf/"

For example one file is named 92_Aligned.sortedByCoord.out.bam, I want the output to be 92_.gtf. Another file is called 10.5_rep1_Aligned.sortedByCoord.out.bam, and the output should be called 10.5_rep1_.gtf.

Am I doing it wrong? Not sure if sed is the right way as I'm technically not renaming the file, but creating another file from that name and changing it?


Solution

  • You want to force evaluation on basename-sed pipe. ( with '$(' ) and handle the quoting)

    outputname=$(basename $file | sed -e "s/_Aligned.sortedByCoord.out.bam/_.gtf/")
    

    Or using Bash built-in only, which will be much FASTER.

    file_base=${file##*/}
    outputname=${file_base/_Aligned.sortedByCoord.out.bam/_.gtf}