Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am an infrequent user of UNIX, I'm sure this will be fairly trivial for any regular user so I appologize for that. I have the following code:

 for file in /home/sub1/samples/aaa*/*_novoalign.bam
      do
           samtools depth -r chr9:218026635-21994999 *_novoalign.bam < $file > /home/sub2/sub3/${file}.out 
      done

I was hoping the output would be sent to a file in sub2/sub3/ with its name like the input folder. It says 'no file or directory'. I would ideally like to send it here with the '_novoalign.bam' removed and a new ending eg '_output.txt' added. Any tips?

p.s. I don't have permission to write to the directory in which the input file is found.

share|improve this question
    
I guess the *_novoalign.bam in the samtools call is wrong. –  Hauke Laging Feb 10 at 18:37

2 Answers 2

cd /home/sub1/samples
for file in aaa*/*_novoalign.bam; do
    target_file="${file%_novoalign.bam}_output.txt"
    samtools depth -r chr9:218026635-21994999 <"$file" >"$target_file"
done
share|improve this answer

You need to remove the path and then the extension you don't want:

base=${file##*/}
newfile=${base%_novoalign.bam}_output.txt

Then use:

... > "/home/sub2/sub3/$newfile"

Check out Shell Parameter Expansion here for other options:

GNU Bash Documentation

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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