in this other answer, Aaron Quinlan stated:
bamtools has a "split" command for exactly this purpose
I can only add that I've just tried it with this simple command
bamtools split -in file.bam -reference
samtools view -?
A region should be presented in one of the following formats: `chr1',`chr2:1,000' and `chr3:1000-2,000'. When a region is specified, the input alignment file must be an indexed BAM file.
samtools view in.bam chr1 > chr1.bam should work
I wrote a java tool to split a BAM per chromosome see http://code.google.com/p/jvarkit/wiki/SplitBam
It also creates an empty BAM (filled with a pair of mock SAMRecords) for each chromosome in the Reference, if no SAMRecord was found for the chromosome.
You can use the following pipeline to extract chrY reads from the raw bam files and with the header
samtools sort A.bam -o A.sort.bam samtools index A.sort.bam samtools view -H A.sort.bam > output.extraction.sam samtools view A.sort.bam chrY >> output.extraction.sam samtools view -hb output.extraction.sam > output.extraction.bam samtools view -H output.extraction.bam
output.extraction.bam is the bam file which extracted chrY reads.