Lesson 5: Absolute and Relative Paths
In bioinformatics, you will often need to reference files located in various places: your home directory, a shared lab drive, or a specific project folder. Understanding how to precisely tell the computer where a file is located is critical.
There are two ways to specify a file’s location: Absolute Paths and Relative Paths.
1. The File System Tree and Root (/)
Section titled “1. The File System Tree and Root (/)”Linux organizes files in a hierarchical tree structure.
- Root (
/): The very top of the tree is called the “root” directory and is represented by a single forward slash/. - Everything on your system lives under root. There are no drive letters like
C:orD:at the top level in Linux.
2. Mounted Drives (/mnt)
Section titled “2. Mounted Drives (/mnt)”If you are using WSL (Windows Subsystem for Linux) or accessing external hard drives on a Linux machine, these drives are often “mounted” inside the /mnt directory.
- WSL Users: Your
C:drive is typically accessible at/mnt/c/. - Example: If your
Bioinformatic_Fridaysfolder is in your Windows Documents folder, the path might look like:Terminal window /mnt/c/Users/YourName/Documents/Bioinformatic_Fridays/
3. Absolute Paths
Section titled “3. Absolute Paths”An Absolute Path is the full address of a file, starting from the root (/). It is unambiguous; it points to the same location no matter where your current working directory is.
Key Rule: An absolute path always starts with a /.
Examples
Section titled “Examples”Let’s find the absolute path to the attendees.csv file in our Training folder.
- Navigate to the
Trainingfolder (wherever it is on your system). - Run the
pwd(Print Working Directory) command:Output (example for a Linux user):Terminal window pwdOutput (example for a WSL user):/home/bodeoni/Bioinformatic_Fridays/Training/mnt/c/Users/WAVECU001/Documents/Bioinformatic_Fridays/Training
The Absolute Path to the file is that full folder path plus the filename:
/home/bodeoni/Bioinformatic_Fridays/Training/attendees.csv
When to use Absolute Paths:
- In scripts/code (so they run correctly from anywhere).
- When configuring tools that need to find a reference file (e.g., a genome index).
4. Relative Paths
Section titled “4. Relative Paths”A Relative Path describes the location of a file relative to where you are right now. It never starts with a /.
Symbols:
.(dot): The current directory...(dot dot): The parent directory (one level up).
Scenarios
Section titled “Scenarios”Scenario A: You are inside Bioinformatic_Fridays/
- To list the files in
Training/long_readswithout leaving your current spot:(This is a relative path. We didn’t start withTerminal window ls Training/long_reads/, so Linux looks forTraininginside the current folder.)
Scenario B: You are inside Training/short_reads/
- You want to see the
attendees.csvfile, which is one level up (inTraining/). - Path:
../attendees.csvTerminal window ls ../attendees.csv
Scenario C: You are inside Training/short_reads/ and want to go to Training/long_reads/
- You need to go up one level (to
Training), and then down intolong_reads. - Path:
../long_readsTerminal window cd ../long_reads
5. The Home Shortcut (~)
Section titled “5. The Home Shortcut (~)”The tilde character ~ is a shortcut for your home directory.
-
On standard Linux: usually
/home/your_username -
On WSL: usually
/home/your_linux_username(Note: this is your Linux home, not your Windows User folder). -
Example:
cd ~takes you home. -
Example:
ls ~/Downloadslists your Downloads folder (if it exists in your Linux home).
Practice Exercise
Section titled “Practice Exercise”- Navigate to the
Training/long_readsdirectory. - Use
pwdto find the Absolute Path of this directory. - List the contents of the
short_readsdirectory using a Relative Path (hint: you need to go up, then down). - Try to list the
attendees.csvfile using an Absolute Path (use the output from step 2 to help you construct it).