Before you start
- Finish the earlier command line lessons first, ideally Command Line: Level 2 (Intermediate).
- Any term new? The Glossary has it.
Learning objectives
By the end of this lesson you will be able to: download a real FASTA file; count and inspect its sequences; measure the total sequence length; and compute GC content, all from the command line.
Bioinformaticians spend a surprising amount of time just interrogating files from the command line before any heavy analysis - how big is it, how many records, does it contain what I expect? That's exactly this project. You'll grab a real FASTA file of three human mRNA sequences and answer real questions about it.
Practise right here
From here on, each code box has a Run button that sends it to a real Linux terminal in your browser. The practice terminal cannot reach NCBI, so start with the box that creates genes.fasta by hand, then run the rest in order.
1Get the data
Make a workspace and download three real human gene sequences (insulin, beta-globin, and APP) straight from NCBI:
mkdir fasta-project cd fasta-project curl "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&id=NM_000207,NM_000518,NM_000484&rettype=fasta&retmode=text" -f -o genes.fasta head -3 genes.fasta # check you got sequences, not an error page
(If curl isn't found, try wget with the same URL and -O genes.fasta.)
cat > genes.fasta <<'EOF' >gene1 insulin ATGGCCCTGTGGATGCGCCTCCTGCCCCTGCTGGCGCTGCTG >gene2 hemoglobin_beta ATGGTGCATCTGACTCCTGAGGAGAAGTCTGCCGTTACTGCC >gene3 APP ATGCTGCCCGGTTTGGCACTGCTCCTGCTGGCCGCCTGGACG EOF
2First look
head genes.fasta # peek at the top - don't dump the whole thing wc -l genes.fasta # how many lines total?
3How many sequences?
Every FASTA record starts with a > header line. So counting headers counts sequences:
grep -c ">" genes.fastaIt's one of the most common FASTA sanity checks, and you can now do it instantly. (Pros often write grep -c "^>", where ^ means "only at the start of a line".)
4What are they?
grep ">" genes.fasta # list just the header lines grep ">" genes.fasta > headers.txt # save them to a file
5How long is the sequence, in total?
Here's a pipeline: strip out the header lines, remove the line breaks, and count the remaining characters - the total number of bases.
grep -v ">" genes.fasta | tr -d '\n' | wc -c
Reading the pipeline
grep -v ">" keeps every line that is not a header (the -v inverts the match). tr -d '\n' deletes the newlines so the bases form one continuous string. wc -c counts the characters. Three tiny tools, one real answer.
6GC content - from the command line
Count the G and C bases, then the total, then compute the percentage:
# Count just G and C characters in the sequence grep -v ">" genes.fasta | tr -d '\n' | tr -cd 'GC' | wc -c # Do the whole calculation in one go gc=$(grep -v ">" genes.fasta | tr -d '\n' | tr -cd 'GC' | wc -c) total=$(grep -v ">" genes.fasta | tr -d '\n' | wc -c) awk "BEGIN{printf \"GC%% = %.1f\n\", $gc/$total*100}"
You just computed a real biological metric on real data with a few command-line tools - no programming language required. That's the power (and the surprise) of Bash for bioinformatics.
🚀 Make it your own
- Download a different set of genes by editing the
id=list in the URL (any NCBI nucleotide accessions). - Count ambiguous bases (Ns) with
grep -v "^>" genes.fasta | tr -cd 'Nn' | wc -c- a quick quality check. (A plaingrep -c "N"would count lines containing an N, including headers like NM_000207.) - Count how many headers mention a keyword:
grep ">" genes.fasta | grep -c "insulin". - Save your whole session as a shell script (
analyze.sh) and run it withbash analyze.sh- your first automation. - Portfolio move: put the script and a short README on GitHub.
What you just did
You downloaded real biological data and answered concrete questions about it - count, identity, length, composition - using only the command line. This is not a toy version of bioinformatics; it's the real daily reality of inspecting files before and after every analysis. The aligners and variant callers in Track 2 are just bigger commands that fit exactly this pattern.
Why tr -cd 'GC' and not tr -d?
tr -cd 'GC' means "delete every character that is not G or C" (the -c complements the set). What's left is only the G and C characters, so wc -c counts exactly those.
Check your understanding
Sources & further reading
- Pearson WR, Lipman DJ. Improved tools for biological sequence comparison. PNAS 85: 2444-2448, 1988. (where the FASTA format comes from) doi:10.1073/pnas.85.8.2444
Official documentation:
NCBI E-utilities in depth (EFetch): www.ncbi.nlm.nih.gov.
GNU Coreutils manual (wc, tr): www.gnu.org.
Last reviewed: October 2026.
Back to the roadmap - pick your next track →
Practice terminal powered by sandbox.bio (runs in your browser; your commands stay on your computer).
