Start here, even if you have never opened a terminal
This lesson assumes nothing. We will explain what the command line even is, then build up moving around, looking at files, searching, and the one idea (the pipe) that makes it powerful, with pictures. Type each command yourself; muscle memory is the whole game here. You cannot break anything by looking.
What is the command line?
The command line (also called the terminal or shell) is a place where you type commands as text instead of clicking buttons. You type a command, press Enter, and the computer does it and prints the result. It feels old-fashioned, but for data it is faster and far more powerful than a mouse.
The parts of a command
Most commands have the same three parts. Knowing the names makes every example readable.
Anything after a # is a comment; commands are case sensitive (ls works, LS does not).
1. Knowing where you are and moving around
The terminal always sits "inside" one folder. Three commands are your eyes and feet.
pwd # print working directory: where am I? ls # list what is in this folder ls -l # list with details (size, date) cd Downloads # change directory into Downloads cd .. # go up one level cd ~ # go to your home folder
Files live in a tree of folders. A path is the address of a file in that tree.
~ is your home folder, . means "here", and .. means "the folder above". Master these three and you will never get lost.2. Creating and handling files and folders
mkdir project # make a new folder cd project touch notes.txt # create an empty file cp notes.txt copy.txt # copy mv copy.txt final.txt # move OR rename rm final.txt # remove (careful: no recycle bin!)
rm is forever
The command line has no trash can. rm deletes immediately and permanently. Read the line twice before you run it, especially before rm -r (which deletes whole folders).
3. Looking inside files
cat file.txt # dump the whole file to the screen head file.txt # first 10 lines head -n 3 file.txt # first 3 lines tail file.txt # last 10 lines less file.txt # scroll a big file (press q to quit)
head and less matter hugely in bioinformatics, where a single file can be gigabytes. You never want to cat a whole genome to your screen.
4. Searching and counting
grep "TP53" genes.txt # print lines containing "TP53" grep -c ">" seqs.fasta # COUNT lines containing ">" wc -l file.txt # count lines in a file sort names.txt # sort lines alphabetically sort names.txt | uniq # sort, then remove duplicates
Why grep is a bioinformatics superpower
FASTA files mark each sequence with a header line starting >. So grep -c ">" instantly counts how many sequences are in a file of any size. Tiny command, huge time saver.
5. The pipe: the big idea
This is the concept that makes the command line powerful. The pipe | takes the output of one command and feeds it straight into the next, so small tools combine into something bigger.
# "Take the file, keep header lines, then count them" cat seqs.fasta | grep ">" | wc -l
6. Saving output to a file
grep ">" seqs.fasta > headers.txt # write results to a NEW file grep ">" seqs.fasta >> headers.txt # ADD to an existing file
echo "ATGC" > dna.txt, then read it back with cat dna.txt. You just created and read a file entirely from the keyboard.Every key word, in plain English
- command line / terminal / shell
- the place where you type text commands.
- command
- the word that says what to do (
ls,cd,grep). - option (flag)
- a tweak starting with
-, like-lor-c. - argument
- the thing a command acts on, usually a file or folder.
- directory
- another word for a folder.
- path
- the address of a file in the folder tree.
- ~ . ..
- home folder, here, the folder above.
- pipe (|)
- feeds one command's output into the next.
- > >>
- save output to a new file / add to a file.
- comment
- a note after
#that the shell ignores.
Your starter cheat sheet
| pwd | Where am I? |
| ls / ls -l | List files (with details) |
| cd folder | Move into a folder (cd .. = up) |
| mkdir name | Make a folder |
| cp / mv / rm | Copy / move-rename / delete |
| cat / head / tail / less | View a file's contents |
| grep "x" file | Find lines containing "x" |
| wc -l file | Count lines |
| cmd1 | cmd2 | Pipe: feed output into the next command |
| cmd > file | Save output to a file |
Check yourself
R and the terminal do not run in this page, so here is what correct output looks like. Type each one and compare.
| You type | You should see |
|---|---|
| grep -c ">" genes.fasta | the number of sequences |
| echo "ATGC" > dna.txt && cat dna.txt | ATGC |
| wc -l < reads.fastq | the number of lines |
Check your understanding
~ mean in a path?~ is home, . is here, and .. is the folder above.> and >> when saving output?> replaces the file's contents; >> appends, leaving what was already there.grep ">" reads.fastq | wc -l and gets 0. Why is this wrong for FASTQ?> header is a FASTA thing. FASTQ uses four lines per read, so the read count is $(( $(wc -l < reads.fastq) / 4 )).ls -l Downloads, what is Downloads?ls is the command, -l is the option, and Downloads is the argument (what to act on).grep ">" seqs.fasta | wc -l tell you?>, so counting those lines counts the sequences.| do?head or less over cat for big files?cat floods your screen; head shows the top and less lets you scroll.Project: wrangle a real data file from the command line →
Ready for more? Command Line Level 2 (Intermediate) builds straight on this.
