Command Line Introduction: Excercises
Excercise 1.1: Directory structure
- Tasks:
Navigate to your home directory
Create a new directory called “testdir” inside your home directory
navigate into the “testdir” directory
Create a new file called “example.txt” inside the “testdir” directory
Solution:
cd mkdir testdir cd testdir touch example.txt
Excercise 1.2: Directory structure
- Tasks:
Use the ls command to display the file “example.txt” file using an absolute path
Use the ls command to display the file “example.txt” file using a relative path
Go one directory up (to your home directory) Use the ls command to display the file “example.txt” using a relative path
Solution:
ls /home/$USER/testdir/example.txt cd testdir ls example.txt cd .. ls testdir/example.txt
Excercise 2: Tab completion
- Tasks:
Navigate to your home directory
Create a new folder “testfolder” and a file “example.txt” in it
go to the root folder
Use tab completion to cd to the folder “testfolder” inside you home directory
Use tab completion in the testfolder to ls -l the file “example.txt”
Solution:
cd mkdir testfolder cd testfolder touch example.txt cd / use tab completion...
Excercise 3: Basic File Operations
- Tasks:
Change to your home directory
Create a file named ‘test.txt’ (and check if it is there)
Create a directory named ‘tutorial’
Copy ‘test.txt’ into the directory ‘tutorial’
Delete ‘test.txt’ (in your home)
Change to ‘tutorial’ and rename ‘test.txt’ to ‘file.txt’ and verify
Remove the directory ‘tutorial’ and its contents
Remove the directories testfolder and testdir and its contents
Solution:
cd touch test.txt ls -lrt mkdir tutorial cp test.txt tutorial/ rm test.txt cd tutorial mv test.txt file.txt ls rm file.txt cd .. rmdir tutorial rmdir testfolder testdir
Excercise 4: Links
- Tasks:
change to /mnt/
Create a directory with the name “linux_intro” and give it to the user ubuntu
Go back to your home directory
Create a soft link called ‘linux_intro’ to /mnt/linux_intro
Note: this cannot be done using normal permission. Use sudo for operating with root privileges
Solution:
cd /mnt sudo mkdir linux_intro sudo chown ubuntu:ubuntu linux_intro cd ln -s /mnt/linux_intro
Excercise 5: Display File Content
Before you can do the next excercise, you need to donwload the sequencing data:
cd ~/linux_intro
wget https://openstack.cebitec.uni-bielefeld.de:8080/swift/v1/linuxcourse/seqs.fasta
- Tasks:
Use head and tail to inspect the file
Print the first and last entry of the fasta file to the command line
Browse the file using less, search for start codons
Solution:
head seqs.fasta tail seqs.fasta
head -n 2 seqs.fasta tail -n 2 seqs.fasta
less seqs.fasta
Excercise 6: Wildcards
For the next excercise, we will donwload more sequencing data:
wget https://openstack.cebitec.uni-bielefeld.de:8080/swift/v1/linuxcourse/linuxdata.tar.gz
tar -zxvf linuxdata.tar.gz
- Tasks:
List all tools in /usr/local/bin/ starting with ‘blast’
List all tools in /usr/local/bin/ starting with ‘blast’ followed by one additional character
List all tools in /usr/local/bin/ starting with ‘a’ or ‘b’ and ending with ‘c’ or ‘d’
Copy all sequence files from the directory linuxdata to the linux_intro directory (except seqs.fasta)
Solution:
ls /usr/local/bin/blast*
ls /usr/local/bin/blast?
ls /usr/local/bin/[ab]*[cd]
cd ~/linux_intro cp ~/linuxdata/sequences* ~/linux_intro/ cp ~/linuxdata/sequences_?.fasta ~/linux_intro/ cp ~/linuxdata/sequences_[1-4].fasta ~/linux_intro/ cp ~/linuxdata/sequences_{1..4}.fasta ~/linux_intro/
Excercise 7: grep and wc
- Tasks:
Copy the Araport11_genes.gff from the previously uncompressed ‘linuxdata.tar.gz’-archive into your linux_intro
Inspect the file using less
How many lines does the file contain?
How many entries are there for Chromosome 1?
Find all entries related to ‘Auxin’
Use the command “grep” to find a file inside the “linuxdata” directory that contains the words “Romeo and Juliet”
Solutoin:
cd ~/linux_intro cp ~/linuxdata/Araport11_genes.gff . less Araport11_genes.gff wc -l Araport11_genes.gff grep -c "^Chr1" Araport11_genes.gff
grep Auxin Araport11_genes.gff
grep -r "Romeo und Juliet" ~/linuxdata/
Excercise 8: Streams
- Tasks:
Use cat and wildcards to combine all sequence-files into a new file “sequences.fasta”
Use head and tail to get the second sequence from sequences.fasta
Use grep to store the sequence headers of sequences.fasta in a file
Use grep, head and tail to store headers 11-20 in a file
Append the headers 41-50 to the same (!) file
Also store the first 50 headers in a separate file. Do this in one command by using “tee” !
Use grep and wc to find out the number of bases in sequences.fasta
Solutoin:
cat sequences_[1-4].fasta > sequences.fasta
head -n 4 | tail -n 2 sequences.fasta
grep ">" sequences.fasta > headers.txt grep ">" sequences.fasta | head -n 20 | tail -n 10 > headers_2.txt grep ">" sequences.fasta | head -n 50 | tail -n 10 >> headers_2.txt grep ">" sequences.fasta | head -n 50 | tee headers50.txt | tail -n 10 >> headers_2.txt
grep -v ">" sequences.fasta | wc
Excercise 9: Tabular Data
- Tasks:
How many features (CDS/mRNA/UTR…) are there for each type?
Hint: features are in row 3, sort and uniq might be useful
Create the same statistic for each chromosome
Hint: cut can select multiple columns
How many genes with a ‘kinase’ annotation are there per chromosome?
Solution:
cut -f 3 Araport11_genes.gff | sort | uniq -c | grep -v "#"
cut -f 1,3 Araport11_genes.gff | sort | uniq -c | grep -v "##"
grep kinase Araport11_genes.gff | cut -f 1,3 | grep gene | cut -f 1 | sort | uniq -c
Excercise 10: Shell scripts
- Tasks:
Write a shell script for the following task: Count the number of lines in file that is given as a parameter
Write a shell script for the following task: Search for a keyword given as the first parameter in a file that is given as the second parameter
Solution:
#!/bin/sh wc –l $1 #!/bin/sh grep $1 $2