Linux for Bioinformatics

Lesson 3 of 14 · 10 min

Making, Moving, and Deleting Files

A well-organized project keeps every file in a predictable place, which saves you hours of confusion later on. A common habit in bioinformatics is to split a project into three folders: data for the raw inputs you receive, scripts for the code you write, and results for the outputs you produce. In this lesson you will build that layout by hand and learn to create, copy, move, and delete files directly from the command line.

Making Folders And Files

A folder is called a directory, and the command mkdir (short for make directory) creates one. The -p flag (a flag is an option that starts with a dash and changes how a command behaves), short for parents, lets you build a whole chain of nested directories in one step and never complains if a folder already exists. The command touch creates a new empty file, which is useful for setting up files you plan to fill in later.

bash
mkdir -p rnaseq/data rnaseq/scripts rnaseq/results
touch rnaseq/scripts/align.sh rnaseq/data/reads.fastq
ls -R rnaseq
rnaseq:
data  results  scripts

rnaseq/data:
reads.fastq

rnaseq/results:

rnaseq/scripts:
align.sh

The cp command copies a file, leaving the original untouched: you give it the file to copy and then the new name or location. To copy an entire directory and everything inside it, add the -r flag, which stands for recursive. The mv command moves a file into another folder, or renames it if you keep it in the same place, and unlike cp it does not leave the original behind.

bash
# cp and mv print nothing when they succeed
cp rnaseq/data/reads.fastq rnaseq/data/reads_backup.fastq
cp -r rnaseq/data rnaseq/data_raw
mv rnaseq/scripts/align.sh rnaseq/scripts/align_reads.sh

Deleting Is Permanent

The rm command (short for remove) deletes a file, and rm -r deletes a directory together with everything inside it. Unlike your desktop computer, the command line has no trash bin, so a deleted file is gone immediately and cannot be recovered. Adding the -i flag, short for interactive, makes rm pause and ask you to confirm each deletion, which is cheap insurance against a costly mistake. On Linux the prompt names the exact file; you answer y for yes or n for no.

bash
rm rnaseq/data/reads_backup.fastq
rm -r rnaseq/data_raw
rm -i rnaseq/scripts/align_reads.sh
rm: remove regular empty file 'rnaseq/scripts/align_reads.sh'? n

Try it yourself: Make a folder named practice with mkdir, add three empty files inside it using touch, copy one of them to a new name with cp, and rename another with mv. Then delete one file using rm -i and type y to confirm. Notice that the moment it disappears there is no undo and no trash bin to rescue it from, which is exactly why rm -r deserves your full attention every single time.