9 min to read
Managing Files and Directories in Unix
Essential Commands for Copying, Moving, and Organizing Your Bioinformatics Data
In this lesson we will learn how to copy the files, move them around, and rename them. We we will also learn how to use standard output to write to files.
Why File Management Matters
In bioinformatics, you’ll work with hundreds or thousands of files—raw data, analysis results, scripts, and references. Knowing how to efficiently copy, move, and organize files is essential for:
- Creating organized project structures
- Making data backups before processing
- Reorganizing analysis outputs
- Cleaning up intermediate files to save space
Let’s explore the core commands that make this possible.
Copying Files: The cp Command
Basic File Copying
The cp (copy) command creates a duplicate of a file. The basic syntax is:
cp <original_filename> <new_filename>
Example: Copying in the same directory
cp tiny_n_L001_R1_xxx.fastq.gz tiny_n_L001_R1_xxx.fastq.gz.copy
ls
This creates a duplicate with a new name. Both files now exist independently—changes to one won’t affect the other.
Copying to Different Directories
You can copy files to other locations while keeping or changing the filename:
# Create a new directory for copies
mkdir fastq_copies
# Copy with a new filename
cp yeast_01_R2.fastq.gz fastq_copies/yeast_01_R2.fastq.gz.copy
# Copy with the same filename
cp yeast_02_R2.fastq.gz fastq_copies/
# Verify the copies
ls fastq_copies/
You have noticed that even if you do not mention the name of the orignal name of the file is used and is placed in the new directory.
What happened here?
yeast_02_R2.fastq.gzwas copied and renamed toyeast_01_R2.fastq.gz.copyyeast_01_R2.fastq.gzwas copied tofastq_copies/with the same name
When to Use cp
- Creating backups before running destructive analyses
- Duplicating reference files for different projects
- Testing workflows on copies before processing originals
- Preserving originals while experimenting with modifications
Moving and Renaming: The mv Command
The Dual Purpose of mv
Unlike cp, the mv (move) command doesn’t create a copy—it relocates or renames the file. The original disappears from its previous location.
Syntax:
mv <original_filename> <new_filename_or_location>
Moving Files Between Directories
mv tiny_n_L001_R1_xxx.fastq.gz.copy fastq_copies/
cd fastq_copies
ls
The file is now only in fastq_copies/—it no longer exists in the original directory.
Renaming Files
mv yeast_01_R2.fastq.gz copy-yeast_01_R2.fastq.gz
ls
When both arguments are in the same directory, mv renames the file instead of moving it.
When to Use mv
- Reorganizing project structures after initial data exploration
- Renaming files to follow consistent naming conventions
- Moving processed data to results directories
- Cleaning up workflows by relocating intermediate files
⚠️ Caution: mv is destructive—the original location/name is gone. Double-check your commands!
Removing Files: The rm Command
Deleting Individual Files
The rm (remove) command permanently deletes files. There is no recycle bin or undo!
| Warning: There is no recycle bin or undo! |
You can try the following command to delete the following file.
rm tiny_n_L001_R1_xxx.fastq.gz.copy
💡 Safety tip: Always double-check filenames before pressing Enter. Consider using ls first to verify the file exists.
Removing Directories
To delete a directory and all its contents, use the -r (recursive) flag:
cd ..
rm -r fastq_copies/
The -r flag tells rm to:
- Enter the directory
- Delete all files inside
- Delete all subdirectories and their contents
- Delete the directory itself
Removing Empty Directories
For empty directories, you have two options:
rm -r empty_directory/
# OR
rmdir empty_directory/
The rmdir command only works on empty directories—it’s a safer option when you want to ensure nothing important gets deleted.
Critical Safety Warning
🚨 The rm command is permanent!
- Deleted files cannot be recovered
- No confirmation prompt by default
- Especially dangerous with
-rflag and wildcards
Best practices:
- Use
lsto preview files beforerm - Consider
rm -ifor interactive confirmation (asks before each deletion) - Never use
rm -rfcarelessly (especially not as root user!) - Keep backups of important data
Understanding Standard Output
The echo Command
echo prints text to the screen (standard output or “stdout”):
echo 'Hello, World.'
Output:
Hello, World.
Quotes: When Are They Needed?
Let’s experiment:
echo 'hello'
echo hello
Both produce:
hello
So far, quotes seem optional. But watch what happens with special characters:
echo hello!!
Output:
echo helloecho hello!
helloecho hello!
What went wrong? The !! has special meaning in bash (history expansion). Now with quotes:
echo 'hello!!'
Output:
hello!!
When to use quotes:
- Text contains special characters:
! $ * ? [ ] ( ) { } ; | & < > - Text has spaces:
echo 'Hello, World!' - You want exact literal text
- Best practice: Use quotes by default to avoid surprises
Output Redirection: Writing Files
The > Operator (Overwrite)
Instead of printing to the screen, we can redirect output to a file using >:
echo hello > hello.txt
This creates hello.txt with the content “hello”. If hello.txt already exists, it will be overwritten!
echo hello > hello.txt
echo hello > hello.txt # Still just one "hello"
cat hello.txt
Output:
hello
Each > overwrites the previous content.
The >> Operator (Append)
To add content to the end of a file without overwriting, use >>:
echo hello >> hellohello.txt
echo hello >> hellohello.txt
cat hellohello.txt
Output:
hello
hello
Each >> adds a new line to the file.
Practical Applications
Creating sample files:
echo ">sequence1" > sample.fasta
echo "ATCGATCGATCG" >> sample.fasta
echo ">sequence2" >> sample.fasta
echo "GCTAGCTAGCTA" >> sample.fasta
Logging analysis results:
echo "Analysis started: $(date)" > analysis_log.txt
echo "Processing sample1..." >> analysis_log.txt
echo "Completed successfully" >> analysis_log.txt
Creating metadata files:
echo "sample_id\tcondition\treplicate" > metadata.tsv
echo "sample1\tcontrol\t1" >> metadata.tsv
echo "sample2\ttreated\t1" >> metadata.tsv
Quick Reference Table
| Command | Purpose | Example | Caution |
|---|---|---|---|
cp |
Copy files | cp data.txt backup.txt |
Creates duplicate |
mv |
Move/rename | mv old.txt new.txt |
Original removed |
rm |
Delete files | rm temp.txt |
Permanent! |
rm -r |
Delete directories | rm -r temp_folder/ |
Very dangerous! |
> |
Write/overwrite | echo "text" > file.txt |
Overwrites existing |
>> |
Append | echo "text" >> file.txt |
Adds to end |
Practice Exercise
Try these commands to reinforce your learning:
- Create a directory called
practice - Copy a FASTQ file into it with a new name
- Create a text file using
echo >with your name - Append your favorite organism to that file using
>> - Rename the text file using
mv - Move the FASTQ file back to the parent directory
- Remove the
practicedirectory
Solution:
mkdir practice
cp yeast_01_R1.fastq.gz practice/practice_file.fastq.gz
echo "Your Name" > practice/info.txt
echo "Favorite organism: Yeast" >> practice/info.txt
cd practice
mv info.txt myinfo.txt
mv practice_file.fastq.gz ../
cd ..
rm -r practice/
Common Mistakes to Avoid
❌ Using rm -r without double-checking
rm -r important_data/ # Oops, meant to delete temp_data/
❌ Overwriting important files with >
echo "test" > critical_results.txt # Just erased everything!
❌ Forgetting -r when removing directories
rm data_folder/ # Error: cannot remove directory
✅ Safe practices:
ls data_folder/ # Check contents first
rm -r data_folder/ # Then remove
What’s Next?
In Lesson 07, we’ll explore:
- Wildcards and pattern matching for batch operations
- Combining commands with pipes (
|) - Using
grepto search file contents - Advanced file manipulation techniques
Key Takeaways
✓ cp creates copies, keeping the original intact
✓ mv moves or renames files—the original location/name disappears
✓ rm permanently deletes files with no recovery option
✓ Use -r flag to remove directories and their contents
✓ > overwrites files, >> appends to files
✓ Always double-check commands before deleting files
✓ Use quotes with echo to avoid issues with special characters.

Comments