Bioinformatics Bit 2: Command Line Fundamentals
Updated: Jan 28, 2025
Introduction
The command line interface (CLI) is a text-based way to interact with your computer. While it might seem intimidating at first, it's an essential tool in bioinformatics that offers powerful ways to handle data and automate tasks.
Getting Started
Mac/Unix
- Open Terminal (Mac: Press Cmd + Space, type "Terminal")
- Terminal comes pre-installed on all Mac/Unix systems
Windows
You have several options:
WSL (Windows Subsystem for Linux) - RECOMMENDED
- Provides a full Linux environment on Windows
- Uses EXACTLY the same commands as Mac/Unix
- Install through Windows Store (Ubuntu recommended)
- Most compatible with bioinformatics tools
Git Bash
- Download here
- Provides a Unix-like environment
- Uses same commands as Mac/Unix
- Good alternative if you can't install WSL
PowerShell via Windows Terminal
- Install Windows Terminal from Microsoft Store
- Different command syntax
- Not recommended for bioinformatics work
Basic Navigation
Purpose: Moving around your file system
| Command | Description |
|---|---|
pwd |
Show current directory |
cd myDir |
Change directory to myDir |
cd .. |
Go up one directory |
ls |
List files and folders |
cd ~ |
Go to home directory |
| Command | Description |
|---|---|
pwd or Get-Location |
Show current directory |
ls or Get-ChildItem |
List files and folders |
cd myDir |
Change directory to myDir |
cd .. |
Go up one directory |
cd ~ |
Go to home directory |
Working with Files and Directories
Purpose: Creating, moving, and manipulating files
| Command | Description |
|---|---|
mkdir my_project |
Create new directory |
touch file.txt |
Create empty file |
cp file.txt backup.txt |
Copy file |
mv file.txt newname.txt |
Rename/move file |
rm file.txt |
Delete file |
rm -r directory |
Delete directory |
| Command | Description |
|---|---|
mkdir my_project or New-Item -ItemType Directory -Name my_project |
Create new directory |
New-Item file.txt |
Create empty file |
Copy-Item file.txt backup.txt |
Copy file |
Move-Item file.txt newname.txt |
Rename/move file |
Remove-Item file.txt |
Delete file |
Remove-Item directory -Recurse |
Delete directory |
Viewing File Contents
Purpose: Reading and examining files
| Command | Description |
|---|---|
cat file.txt |
Display entire file |
less file.txt |
View file page by page |
head -n 10 file.txt |
View first 10 lines |
tail -n 10 file.txt |
View last 10 lines |
| Command | Description |
|---|---|
Get-Content file.txt or cat file.txt |
Display entire file |
Get-Content file.txt | more |
View file page by page |
Get-Content file.txt -Head 10 |
View first 10 lines |
Get-Content file.txt -Tail 10 |
View last 10 lines |
Basic Text Manipulation
Purpose: Finding and filtering content
| Command | Description |
|---|---|
grep "sequence" file.txt |
Search for "sequence" in file |
wc -l file.txt |
Count lines in file |
sort file.txt |
Sort file contents |
uniq file.txt |
Show unique lines |
| Command | Description |
|---|---|
Select-String "sequence" file.txt |
Search for "sequence" in file |
(Get-Content file.txt | Measure-Object -Line).Lines |
Count lines in file |
Get-Content file.txt | Sort-Object |
Sort file contents |
Get-Content file.txt | Sort-Object | Get-Unique |
Show unique lines |
Practical Tips
- Use Tab for auto-completion (works in all shells)
- Use up/down arrows to cycle through command history
- Use
clear(clsin PowerShell) to clear the screen - Use
ctrl + cto stop a running command - Use
man(Mac/Unix/WSL/Git Bash) orGet-Help(PowerShell) for documentation
Common Mistakes to Avoid
- Mind the case - commands are case-sensitive (except in PowerShell)
- Watch for spaces in file names (and try to avoid them)
- Be careful with delete commands
- Always verify your current directory before operations