Navigating the Filesystem
When you open a terminal, you are always “inside” a directory. Every command you run happens relative to that location. Knowing where you are and how to move around is the first skill you need.
Where am I? pwd
Section titled “Where am I? pwd”pwd stands for “print working directory.” It shows the full path of where you currently are.
$ pwd/home/userWhen you first open a terminal, you start in your home directory. This is your personal space on the machine.
What is here? ls
Section titled “What is here? ls”ls lists the contents of the current directory.
$ lsDesktop Documents Downloads projectsUseful ls options
Section titled “Useful ls options”# Long format: shows permissions, size, and modification date$ ls -ldrwxr-xr-x 2 user user 4096 Mar 28 projects/-rw-r--r-- 1 user user 240 Mar 28 samplesheet.csv
# Show hidden files (files starting with a dot)$ ls -a. .. .bashrc .ssh Desktop Documents
# Human-readable file sizes$ ls -lh-rw-r--r-- 1 user user 1.2G Mar 28 sample_R1.fastq.gz
# Combine options$ ls -lahThe -l flag is the one you will use most. It shows who owns each file, how big it is, and when it was last modified.
Moving around: cd
Section titled “Moving around: cd”cd stands for “change directory.”
# Move into a directory$ cd projects
# Check where you are now$ pwd/home/user/projectsGoing back
Section titled “Going back”# Go up one level (to the parent directory)$ cd ..
# Go up two levels$ cd ../..
# Go back to your home directory (three ways)$ cd$ cd ~$ cd /home/userThe .. shortcut means “one directory above.” The ~ shortcut means “my home directory.” You will use both constantly.
Going to a specific place
Section titled “Going to a specific place”# Absolute path: starts from the root /$ cd /home/user/projects/rnaseq
# Relative path: starts from where you are now$ cd projects/rnaseqAn absolute path always starts with / and works no matter where you are. A relative path starts from your current directory. Both get you to the same place.
The directory tree
Section titled “The directory tree”Linux organizes everything in a single tree starting from /, called the root. Here is a simplified view of what matters for bioinformatics work.
/├── home/│ └── user/ ← your home directory│ ├── projects/│ │ ├── rnaseq/│ │ │ ├── data/│ │ │ ├── results/│ │ │ └── samplesheet.csv│ │ └── chipseq/│ └── software/├── tmp/ ← temporary files, cleared on reboot├── usr/│ └── bin/ ← installed programs└── var/ └── log/ ← system logsYou will spend most of your time inside /home/user/. System directories like /usr/ and /var/ matter less for daily work.
Creating directories: mkdir
Section titled “Creating directories: mkdir”# Create a single directory$ mkdir results
# Create nested directories (all at once)$ mkdir -p projects/rnaseq/data
# Create multiple directories$ mkdir data results figuresThe -p flag is important. Without it, mkdir projects/rnaseq/data fails if projects/ or rnaseq/ do not exist yet. With -p, it creates every missing level.
Removing empty directories: rmdir
Section titled “Removing empty directories: rmdir”# Remove an empty directory$ rmdir old_resultsrmdir only works on empty directories. This is a safety feature. To remove directories with files inside, you will learn about rm -r in the next page.
Tab completion
Section titled “Tab completion”Press Tab to autocomplete file and directory names. This saves time and prevents typos.
# Type "cd pro" then press Tab$ cd pro<Tab>$ cd projects/
# If multiple matches exist, press Tab twice to see options$ cd p<Tab><Tab>projects/ papers/Tab completion works with commands, file paths, and many program options. Get in the habit of using it for everything.
Seeing the structure: tree
Section titled “Seeing the structure: tree”The tree command shows a visual overview of a directory. It is not installed by default but is very useful.
# Install tree$ sudo apt install tree
# Show the structure of the current directory$ tree.├── data│ ├── sample_R1.fastq.gz│ └── sample_R2.fastq.gz├── results└── samplesheet.csv
# Limit depth$ tree -L 2
# Show only directories$ tree -dtree is especially helpful when exploring a new project or checking the output of a pipeline.
Finding files: find
Section titled “Finding files: find”When you know a file exists but cannot remember where it is, find will search for it.
# Find all .fastq.gz files under the current directory$ find . -name "*.fastq.gz"./data/sample_R1.fastq.gz./data/sample_R2.fastq.gz
# Find all directories named "results"$ find /home/user/projects -type d -name "results"
# Find files modified in the last 24 hours$ find . -mtime -1The first argument is where to start searching. The -name flag matches file names. The -type d flag limits results to directories.
Quick reference
Section titled “Quick reference”| Command | What it does |
|---|---|
pwd |
Print current directory |
ls |
List contents |
ls -lh |
List with details and readable sizes |
ls -a |
List including hidden files |
cd dir |
Move into a directory |
cd .. |
Move up one level |
cd ~ |
Go to home directory |
mkdir -p path |
Create directories (including parents) |
rmdir dir |
Remove an empty directory |
tree -L 2 |
Show directory structure (2 levels deep) |
find . -name "*.csv" |
Search for files by name |
Next steps
Section titled “Next steps”Now that you can move around the filesystem, the next page covers working with files: creating, copying, moving, and understanding permissions.