Lesson 4 of 14 · 11 min
Peeking Inside Files Without Opening Them
A FASTA, FASTQ, or TSV file is just plain text, so you never need a heavy graphical editor to look inside one. This lesson uses four terminal commands for that job: cat, less, head, and tail. We begin with cat, which prints a whole file to the screen when you type cat, then a space, then the file name.
# a FASTA record is a > header line followed by the DNA sequence below it
cat sequence.fasta>seq1 example gene
ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG
>seq2 another gene
ATGCGTACGTTAGCACTGACCGGTTAA
A FASTQ file stores sequencing reads, and each read (one sequenced DNA fragment) takes four lines: an @ identifier, the DNA sequence, a + separator, and a line of quality scores with one character per base. A single file can hold millions of reads, so cat would flood the screen; use less instead. It shows the file one screen at a time: press the space bar or arrow keys to move, type / then a word and Enter to search, press n to jump to the next match, and press q to quit. Because less reads the file in chunks rather than loading it all into memory, it opens even a 10 GB FASTQ instantly.
less reads.fastqThe command head shows only the first lines of a file and tail shows only the last lines. Add -n followed by a number to pick how many lines, so head -n 8 prints the first eight. The commands below print, in the order they run, the first two reads of the FASTQ, the last record of the FASTA, and the top of a TSV file, a table whose columns are separated by tab characters and whose first row names those columns.
head -n 8 reads.fastq
tail -n 2 sequence.fasta
head -n 2 counts.tsv@read1
GATTTGGGGTTCAAAGCAGTATCGATCAAATAGTAAATCC
+
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
@read2
CCGATTGCACTCCAGCCTGGGCAACAAGAGCGAAACTCCG
+
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
>seq2 another gene
ATGCGTACGTTAGCACTGACCGGTTAA
gene_id sample1 sample2
ENSG00000141510 1024 987
A log file is a plain-text file that a running analysis keeps adding lines to as it works. Adding the -f flag to tail turns on follow mode: it prints the last lines and then stays open, showing each new line the moment the program writes it. Press Ctrl and c together to stop watching and return to your prompt.
# -f follows the file and prints new lines live as the aligner writes them
tail -f alignment.log[00:01] Loading reference genome hg38.fa
[00:03] Reference index ready
[00:08] Aligned 1000000 / 5000000 reads
[00:15] Aligned 2000000 / 5000000 reads
Try it yourself: Grab any FASTQ file and run head -n 4 to see its first read, then tail -n 4 to see its last. Open it with less, search for a short sequence motif by typing /GGATCG and Enter, then press q to quit. Finally, start any long command that writes to a log and follow it live with tail -f logfile, pressing Ctrl-c once it finishes.