Local Terminal

Meet your (local) Terminal

Get started with using the terminal and learn to navigate, create, change, and delete. Have fun!

Chapters

  1. Some Terminal Commands
  2. First Steps
  3. Print to Screen and Redirect to File
  4. Copy, Rename and Remove
  5. Wildcards
  6. Terminal History
  7. Some Biological Examples
  8. Assignments - Good Luck !

Chapter #1 : Some Terminal Commands

Some Basic Commands
pwd.................: absolute pathname of the current working direction
man <command>.......: manual page for command (exit with q)
cd <where>..........: change directory/folder
cd .. ..............: go up one directory
cd..................: go home
mkdir <dir>.........: create directory
rmdir <dir>.........: remove directory (if empty)
ls <dir>............: list content of directory
ls -alh <dir>.......: more detail list
echo "message"......: prints content or message
cat <file>..........: print and concatenate files
head -n 5 <file>....: show first n lines
tail -n 5 <file>....: show last n lines
more <file>.........: read file (exit with q)
less <file>.........: similar to more but newer
> & >>..............: re-direct output (e.g. pwd > file.txt)
cp <ori> <copy>.....: copy file
mv <old> <new>......: move and/or rename file
rm <file>...........: remove file - carefull!!i
wc <file>...........: word, line, character, and byte count
grep "query" <file>.: search input file(s)
clear...............: clear terminal 
history.............: show terminal history
date................: display date
cal.................: display calendar
No worries - Help is at your fingertip
## Access the manual (Syntax: man <command>)
man cat # example

## Help (Syntax: help <command>)
help cat # might not work for all commands

## Which copy am I using and where is it located?
which cat
BUILT-IN VARIABLES
echo ${SHELL}.......: Gives present shell
echo ${USER}........: Displays username
echo ${HOME}........: Home directory of User
echo ${RANDOM}......: To get a random number
echo ${PWD}.........: Current directory

Chapter #2 : First Steps

## First, open your terminal.

## Where are you? Get current working directory
pwd
# > structure: <folder>/<folder>/<folder>

## Create a working directory - a folder inside a folder inside another folder
mkdir ${HOME}/HS2019             # Create first folder in your home directory
mkdir ${HOME}/HS2019/EvolGen     # Create another folder inside the first folder
mkdir ${HOME}/HS2019/EvolGen/BI  # Create yet another folder inside this folder
cd ${HOME}/HS2019/EvolGen/BI     # Change your directory to the working directory

## Would it not be nicer to do this with less typing?
mkdir -p TestFolder_A/TestFolder_B
# > option -p creates any intermediate folders

## Go down and go up step-by-step 
cd TestFolder_A/TestFolder_B
pwd
cd ..
pwd
cd ..
pwd

## Go down and go up two directories at once
cd TestFolder_A/TestFolder_B
pwd
cd ../..
pwd

## Remove test folders
rmdir TestFolder_A/TestFolder_B
rmdir TestFolder_A

Chapter #3 : Print on Screen and Redirect to File

## Print message on terminal
echo "Hello Terminal"

## Print message to a file (>) and open the file
echo "Nothing in life is to be feared." > text1.txt
cat text1.txt

## Print another message to a different file and combine with the previous file
echo "It is only to be understood." > text2.txt
cat text1.txt text2.txt > text12.txt
cat text12.txt

## Add (>>) a third line to the combined file
echo "Marie Curie" >> text12.txt
cat text12.txt

## Create another text file with a different multi-line message 
echo -e "Think Like a Proton\nStay Positive" > text3.txt
# > \n stands for newline and divides the string into two lines
cat text3.txt
cat text3.txt > proton.txt

## Create an empty file and fill it
touch ZERO.txt
echo "Test File" >> ZERO.txt
echo "${HOME}"   >> ZERO.txt
echo "${PWD}"    >> ZERO.txt
echo "---------" >> ZERO.txt
date             >> ZERO.txt
echo "---------" >> ZERO.txt
cat ZERO.txt

Chapter #4 : Copy, Rename and Remove

## Copy file
cp text12.txt Marie_Curie.txt
more text12.txt Marie_Curie.txt
ls -l

## Rename file (move file)
mv ZERO.txt logfile.txt
ls -l

## Remove file(s)
rm text12.txt text.txt
ls -l

Chapter #5 : Wildcards

## List all text files
ls *.txt
ls text[123].txt
# > [123] a group - meaning 1, 2, or 3

## Remove multiple files
rm text[123].txt

Chapter #6 : Terminal History

You might be familiar with the history of your internet browser. The terminal has a history too. This is great because with command history we cannot only search the past but it also means not to retype previous commands. Use arrow up and down to travel within your history. You can also access it:

history [n]
history -c
history -d offset
history -d start-end
history [-anrw] [filename]

With no options, display the history list with line numbers. Lines prefixed with a ‘*’ have been modified. An argument of n lists only the last n lines.

-c clear history 
-d offset Delete the history entry at position offset.
-d start-end Delete the history entries between positions start and end
-a Append the new history lines to the history file.
-n Append the history lines not already read from the history file to the current history list.
-r Read the history file and append its contents to the history list.
-w Write out the current history list to the history file.

Chapter #7 : Some biological examples

# Download a sequence fasta file
pwd # make sure this is the place for the download
curl -O https://gdc-docs.ethz.ch/UniBS/HS2019/BioInf/data/RDP_16S_Archaea_Subset.fasta

# Count the number of lines in the file 
wc -l RDP_16S_Archaea_Subset.fasta

# Have a look at the fasta file
less RDP_16S_Archaea_Subset.fasta

# Have a look at the first 15 lines
head -n 15 RDP_16S_Archaea_Subset.fasta

# Count the number of sequences
grep ">" -c RDP_16S_Archaea_Subset.fasta

# Find a specific motif and highlight it
grep "cggattagatacccg" --color RDP_16S_Archaea_Subset.fasta

# Count how many time you found the motif
grep "cggattagatacccg" -c RDP_16S_Archaea_Subset.fasta

# Find alternatives step-by-step
grep "cgggaggc" -c RDP_16S_Archaea_Subset.fasta
grep "cgggtggc" -c RDP_16S_Archaea_Subset.fasta
grep "cgggcggc" -c RDP_16S_Archaea_Subset.fasta
grep "cggggggc" -c RDP_16S_Archaea_Subset.fasta

# Find alternative faster
grep "cggg[atcg]ggc" -c RDP_16S_Archaea_Subset.fasta

Chapter #7 : Assignments

Try to solve the following problems/questions. Record all the commands and add comments (#) to explain your steps.

# Example 
# 7.0 What is the meaning option -S for command ls -Sl ?  
# Lets have a look at the manual/help page for command ls
help ls
# => S: list file and sort it by size
Your Turn:
7.1 What week day was 1. August 1291?
7.2 Create a text file with the content of your home directory? 
7.3 Add a title to this text file.  
7.4 Add a date to the bottom of the file.
7.5 Extract the complete last sequence from the fasta file RDP_16S_Archaea_Subset.fasta.
7.6 Write the last 10 terminal commands into a text file. 
7.7 List and delete all created tmp files.