The bedtools command bedtools genomecov
(aka genomeCoverageBed) will do this for you. (The commands below are for version 2.15.0 and above).
By default, you will get a histogram of coverage for each chromosome, and the last group of results will be a histogram of coverage for the genome:
bedtools genomecov -ibam aln.sorted.bam | grep genome
It will also report genome-wide coverage in BEDGRAPH format (only covered intervals):
bedtools genomecov -ibam aln.sorted.bam -bg
BEDGRAPH format with both covered and uncovered intervals:
bedtools genomecov -ibam aln.sorted.bam -bga
Only coverage on the positive strand:
bedtools genomecov -ibam aln.sorted.bam -s +
Scaling coverage by a constant for things like RPM:
bedtools genomecov -ibam aln.sorted.bam -scale 0.73
Taking into account spliced BAM alignments:
bedtools genomecov -ibam aln.sorted.bam -split
Only counting MAPQ >= 30:
samtools view -q 30 -u aln.bam | bedtools genomecov -ibam -
If you want per-base coverage at each base in the genome, you can use the -d option:
bedtools genomecov -ibam aln.sorted.bam -d
samtools will also do this much more quickly, but I believe it will only report positions with coverage > 0:
samtools depth aln.bam
Does samtools mpileup not give you what you need?
I think Qualimap can help you. It is easy to use and give you breadth of coverage in each deep of coverage.