$ Python - Version: Hint: You Can Exit An Interactive Python Environment by Entering

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Series 12

12.1
date
which
sleep
time
history
clear
echo
man
python
python3

1. Enter python at the UNIX command prompt. At the level of the Python command prompt:

a) Find out what version of Python is currently used. Give the Python instructions to do so.
$ python -V
$ python –version

b) Print the message “Hello world!” on your screen; copy and paste all output obtained in your
answer sheet.
$ python
>>> print(“hello world”)
Hint: You can exit an interactive Python environment by entering exit(), making it possible to go
back to the UNIX shell.

2. What command can be used to find out where Python is installed? Write down this command in
your answer sheet. Also, for what purpose is the directory containing Python used? Use man hier
to find out what the directory in question is used for and subsequently explain this in your own
words.
$ which python
$ man hier
/usr/bin This is the primary directory for executable programs. Most
programs that can be executed by regular users should be placed in this
directory. Furthermore, these programs are not needed for booting or for
repairing the system, and where these programs are also not installed
locally.

3. Using the UNIX command line interface, print the Python version with python -V, wait (sleep)
for five seconds, and print your name and surname (using a single line).

$ python -V; sleep 3; echo Gildong Hong


Hint: As explained in the introduction, you can chain commands by putting ‘;’ in between the
different commands, so to have them independently executed by the shell one after another.

4. Measure how long it effectively takes to execute the chain of commands put together for the
previous question. Make sure the total execution time is measured (that is, the time as needed by all
commands).

Wrong input => $ time python -V; sleep 3; echo Gildong Hong Output
=> $ 0m0.000s or 0m0.001s Correct input => $ time (python -V; sleep
3; echo Gildong Hong) Output => $ 0m3.0XXs
Observation: If round brackets are not used, then the time command
only
measures the execution time of the first command right after it.

5. What command can be used to show all of the previously executed commands? Is there a way to
show only the five previously executed commands? How can that be done?
$ history
$ history 5

12.2
wget
which
who
write
nslookup
ping
zip
pwd
ls
man

1. Find the IP address of the server www.ghent.ac.kr by making use of a name server lookup.2
$ nslookup www.ghent.ac.kr

2. Find the IP address of ugent.be by making use of the ping command. To that end, ping
ugent.be using (1) its human-readable name and (2) its IP address. Can you observe a difference?
Explain in your own words.
$ nslookup ugent.be # 157.193.43.50
$ ping ghent.ac.kr
$ ping 157.193.43.50
Observation: You can ping a server using both its human-readable hostname and its numerical IP
address. The result is essentially the same.

3. Give a UNIX command to find the location of the ping executable. Use man hier to find out
what the specified directory is used for; copy and paste this explanation from the man pages.
$ which ping
/bin
This directory contains executable programs which are needed in
single user mode and to bring the system up or repair it.

4. What is the meaning of localhost? What is the IP address traditionally associated with localhost?
What is the meaning of localhost? What is the IP address traditionally
associated with localhost?
Answer: Localhost is the computer that the command line is connected to. The IP address of
localhost is 127.0.0.1.

5. Give a UNIX command to download the following FASTA file to your home directory:
https://users.ugent.be/~wmdeneve/informatics/SARS-CoV-2.fasta
Note that your home directory is the directory you arrive in just after logging in to the server.
After having downloaded SARS-CoV-2.fasta, you can have a look at the content of this text file
by means of the cat command (more or less can also be used).
Download the FASTA file SARS-CoV-2.fasta to your home directory:

$ wget http://users.ugent.be/~wmdeneve/informatics/SARS-CoV-2.fasta

6. Where is the aforementioned FASTA file downloaded? Give a UNIX command that can be used to
figure out the location of the directory containing the downloaded FASTA file in the overall file
system. Give a UNIX command that can be used to inspect the content of this directory.

$ pwd → shows the location of the current directory


$ ls → lists the files present in the current directory

7. Compress the text file SARS-CoV-2.fasta using the zip command, hereby producing a
compressed file that is binary in nature (you can verify the binary nature of this file through the cat
command). To what extent (in terms of percentage) can the file size of the text file SARS-CoV-
2.fasta be reduced?

Compress the text file SARS-CoV-2.fasta using the zip command.


$ zip out.zip SARS-CoV-2.fasta # (deflated x%)
Answer: The file size is reduced by approximately 1 – x%. The file size can be checked using the
following command: $ ls -l

8. Type who at the level of the command prompt. How many people are online at the moment?
Experiment a bit with communicating with your favorite teaching assistant through the usage of the
write hopark command. Send your name and student ID to the teaching assistant. In your
answer sheet, write down the list of commands you made use of to accomplish this task.

Chat with other users by means of the who and write commands.
$ who # find someone to talk to
$ write <login name> # start a conversation with some user
<CTRL-D> # end the conversation

12.3
1. How to make use of pip to install a new Python package? Write down the command used for
installing numpy and the command used for installing pandas in your answer sheet (numpy and
pandas are two packages often used in the field of data science). Describe the output obtained in
your own words.

$ pip install numpy


$ pip install pandas

2. How many external libraries are already installed in your active Python environment? Write down
the pip command for listing all library names, and copy and paste the output obtained in your
answer sheet.

$ pip list

3. After installation, where can the library numpy be found? Write down the pip command for
finding the location of this library within the file system in your answer sheet. Moreover, move to
that location by making use of the change directory command: cd [library location]. In
your answer sheet, write down the complete cd command used.

$ pip show numpy


$ cd /usr/lib/python2.7/dist-packages

12.4

arch
top/ htop
df
du
ps

1. Find the architecture of the server using arch. Explain the output obtained.
$ arch # x86_64
Answer: This computer system has a 64-bit architecture. This for instance means that 64-bit
memory addresses can be used by the underlying CPU(s), making it possible to address a
memory space with a total size of 2**64 bytes.

2. Using the command top, determine the CPU and RAM availability of the server.
$ top
a- How many tasks (processes) are running?
b- How much RAM (memory space) is used?
c- Difference in output between the htop command and the top command?

3. Use a UNIX command to display disk space information.

a- Write down the UNIX command to display human-readable disk space


information:
$df -h
b- How many gigabytes of disk space are available, starting from the root
(‘/’) of the system?

Series 13
13.1
wget
unzip
tar
diff
mv
smbclient

1. Give a UNIX command to download the following three files, with each file taking the form of a
compressed archive:
$ wget

2. Give the UNIX commands needed to decompress the three downloaded files.

$ unzip sample_A.zip
$ tar -xvf sample_B.tar
$ tar -zxvf sample_C.tar.gz

3. Give a UNIX command to open one of the decompressed files with a text editor. You can observe
that the text file obtained follows the HTML (HyperText Markup Language) formatting rules for
structuring and styling web pages, in which the way you use white space is not important. More
information about the use of HTML can be found at http://www.w3schools.com/html/.
What is the content of the text file obtained? Explain in your own words.

$ vi sample_A.txt
# You can see HTML tags that are describing the structure and the
layout of # a curriculum vitae.

3. Choose two decompressed text files and make use of a UNIX command to see the difference
between these two files. In addition, copy and paste the output obtained to your answer sheet.

$ diff sample_A.txt sample_B.txt

4. After comparing the content of all three decompressed text files, select a text file of your liking
and give a UNIX command to rename this text file to index.html.

$ mv sample_A.txt index.html

6. Personalize the content of index.html. To that end, you can for instance change the information
that can be found in-between the square brackets in index.html.

$ vi index.html # change some of the content # Other text editors


that are available: nano and pico

7. Now that you have adjusted the HTML file index.html, give a UNIX command to transfer
index.html from helios.ugent.be to the network drive //webhost/username. (webhost refers to the
hostname of the Ghent University server used to serve web pages, namely webhost.ugent.be,
whereas username refers to your login name).
To upload a toy file hello.txt in your current directory on helios.ugent.be to a personal web space on
a network drive, you can for instance make use of smbclient:
$ smbclient //webhost/username -U username -c'put hello.txt hello.txt'
To list all files available in the network drive //webhost/username, you can make use of the
following commands:
$ smbclient //webhost/username
smb: \> ls

$ smbclient //webhost/username -U username -c'put index.html


index.html'

13.2

cat

ls

rm

wget

rmdir

unzip

mkdir

curl

pwd

mutt

mv

touch

2. In the current directory used by Task 1, create a directory with the following name:
$ mkdir hong_gildong_1234567.git

3. Create two files in the directory create by Task 2: 1) .gitignore and 2) readme.txt. These two files
need to be created from within the current directory used by Task 1 (i.e., do not create these files
from within the directory that you created in Task 2). Also, do not ignore the dot in front of gitignore,
given that the use of this dot results in the creation of a hidden file.
$ touch readme.txt; touch .gitignore
$ mv readme.txt hong_gildong_1234567.git
$ mv .gitignore hong_gildong_1234567.git

4. Download the file experiments.zip using the command below:


$ wget http://users.ugent.be/~wmdeneve/informatics/experiments.zip
$ mv experiments.zip hong_gildong_1234567.git

5. Change your working directory to the directory created in Task 2.


$ cd hong_gildong_1234567.git

6. Decompress the file experiments.zip and subsequently remove this zip file. You are asked to do so
using a single line of commands.
$ unzip experiments.zip; rm experiments.zip

7. Upon successful completion of Task 6, you should be able to see numerous files in your working
directory. In particular, your working directory should now contain files with the following
extensions, all related to experiments on machine learning: .png, .py, .md, and .csv.
$ mkdir images input experimental_code others
8. Using only four command line statements, move all files with the .png extension to the images
directory, move all files with the .py extension to the experimental_code directory, move all files with
the .md extension to the others directory, and move all .csv files to the input directory.
$ mv *.png images
$ mv *.py experimental_code
$ mv *.md others
$ mv *.csv input

11. Provide the command line statements needed to accomplish the tasks below, as well the output
obtained (by copy and pasting this output in your answer sheet). Execute all these command line
statements in your current working directory, and where your current working directory should be
the same as the directory you would be at if you executed all tasks up to and including Task 10
correctly.

a. Given the directory <surname>_<name>_<student_id>.git, display all the files and directories in
this folder such that each file or directory appears on a separate line in stdout. Make sure that you
can see the hidden file that you created earlier.

$ ls -al

b. Display all the files and folders in the images directory such that each file or directory appears on a
separate line in stdout.

$ cd images; ls -al

12. Finally, let us learn how to send an email through your account on helios.ugent.be. In this
context, select one of the images available in the images directory, and modify the example
command line statement shown below.

Check your inbox. Who sent the email? Copy and paste the content of the email received in your
answer sheet.
$ echo "Results in attachment." | mutt -a "./cabin_count.png" -s "Results"
-– gildong.hong@ghent.ac.kr
# A user with email address gildong.hong@ghent.ac.kr sent an email with an
# image in attachment..

13.3
ls
tee
find
tty

1. The directory /etc is an important directory that contains all system-related configuration files. List
all the directories in /etc in a non-recursive manner and in such a way that they are shown with a
trailing forward slash.

$ ls -d /etc/*/

2. Find all files (including directories) in /etc in a recursive manner and redirect the obtained list of
files to a text file named etc_files.txt. While doing so, ensure that no error messages are displayed on
the standard output.

$ find /etc 2> /dev/null | tee etc_files.txt

3. What is the main difference between the files in your home directory, or any files you create and
work with, and the files that are found when executing the command ls /dev/std*?

# The files in your home directory, or any files you create and work
# with, are regular files, while the files in /dev/std* are so-called
# special files.

4. Using the long listing format, list all information about all files in the directory /dev that begin with
the prefix std.

$ ls -l /dev/std*

5. Display the file name of the terminal connected to standard input. Also, in your own words,
explain what the special files /dev/stdin, /dev/stdout, and /dev/stderr are used for.

$ tty

13.4

find directory [directory ...] criterion [criterion ...] actions


The first thing to specify is which directories (and their underlying subdirectories) should be
searched. This can consist of more than one path description. Next, you must specify what exactly
should be searched for, using so-called selection criteria. Some example selection criteria are as
follows:

• -name: look for files with a certain name

• -mtime: look for files that have not been changed since a certain point in time

• -type: look for files of a certain type (e.g., d for a directory, f for a regular file)

• -inum: look for files with a certain inode number

Other criteria can be found in the course handbook, or in the manual pages. Finally, you must specify
what should happen with the obtained files. Examples of such actions are:

• -print: print file name

• -exec: execute a command

• -ok: execute a command, but only after having been given explicit permission by the user

1. Decompress the file thapbi-pict-v0.6.15.zip and make the directory peterjc-thapbi-pict-72d004e


your working directory.
$ unzip thapbi-pict-v0.6.15.zip; cd peterjc-thapbi-pict-72d004e

2. Assuming that peterjc-thapbi-pict-72d004e is your current directory, count all regular files in this
directory that end with .sh in a recursive manner.
$ find . -type f -name "*.sh" | wc -l

3. Find all regular files with a name containing the string dump and ending with .sh. The string dump
is allowed to occur anywhere in the name of a regular file.
$ find . -type f -name "*dump*.sh"

4. Count all files with a size greater than 512 kilobytes.


$ find . -type f -size +512k | wc –l

5. Find all files that end with .tsv and rename them so they end with .csv.
$ find . -type f -name "*.tsv" -exec rename 's/\.tsv$/.csv/' '{}' \;
Or

$ find . -name "*.tsv" -exec sh -c 'f="{}"; mv -- "$f" "${f%.tsv}.csv"' \;

Series 14

14.1

pr
cat - more - less
cut - paste
tar - unzip
tr
sort
grep
head – tail
sed

1. Use a UNIX command to download the compressed file dna.zip:

Using one of the previously listed UNIX commands, unzip the compressed file dna.zip.
$ wget
$ unzip dna_fasta.zip

2. Unzipping dna.zip should result in the creation of three files, namely dna1.fasta, dna2.fasta, and
README. In order to view the content of dna1.fasta, use the following three commands: more, less,
and cat. What is the difference between these three commands? Explain.

# cat: shows the content of a file

# more: shows the content of a file, stopping at the end of the first page,

# with the possibility of scrolling down

# less: shows the content of a file, stopping at the end of the first page, # with the possibility of
scrolling up and down

3. Use I/O redirection in combination with the nl, echo, head, and date commands to perform the
tasks described below. You must complete these tasks in the order specified.

 Using the echo command, add your name to a new file called DNA_combined.txt.
$ echo "Gildong Hong" > DNA_combined.txt

 Add the FASTA descriptions of the first three specimens (including specimen 3) from dna1.fasta to
DNA_combined.txt.
$ head -33 dna1.fasta >> DNA_combined.txt

 Add the FASTA descriptions of specimen 4 to 6 (including specimen 4 and specimen 6) from
dna2.fasta to DNA_combined.txt.
$ tail -44 dna2.fasta >> DNA_combined.txt

 Number each line and redirect the output to DNA_combined_numbered.txt.


$ nl DNA_combined.txt > DNA_combined_numbered.txt

4. Find out the permissions of the file DNA_combined_numbered.txt for the user, group, and others
domain. Submit both the UNIX command used and its output. Explain the output obtained.
$ ls -l DNA_combined_numbered.txt -rw-r--r-- 1 wmdeneve users 7246
Apr 19 08:03 DNA_combined_numbered.txt

# The user domain has both read and write access, whereas the group domain # and the other
domain only have read access

5. Create a hard link to DNA_combined_numbered.txt and name it hard_link_DNA.txt. Observe the


permissions set for DNA_combined_numbered.txt and hard_link_DNA.txt, including their inode
numbers. Are they the same or different? Explain.

# create a hard link

$ ln DNA_combined_numbered.txt hard_link_DNA.txt

$ ls -li

# Observation: The permissions and the inode numbers are the same

6. Delete some content from hard_link_DNA.txt and subsequently inspect the content of
DNA_combined_numbered.txt. Explain what happened when you deleted some content from the
hard link hard_link_DNA.txt.

# Observation: When content is deleted from the hard link, it is also # deleted from the original file

7. Create a soft link for DNA_combined_numbered.txt and name it soft_link_DNA.txt. After that,
show a list with detailed information (inode number, owner, ...) about the files residing in your home
directory (similar to Question 5 of this problem statement). Does the soft link look different from the
other files? Are they the same or different? Explain.

# Create a soft link

$ ln -s DNA_combined_numbered.txt soft_link_DNA.txt

$ ls -li

# Observation: The soft link properties look different, for instance showing # which file it is linked to

14.2

1. Find out where the string ">Specimen3" is located in DNA_combined_numbered.txt and write this
information to the terminal.
# locate >Specimen3
$ grep -n '>Specimen3' DNA_combined_numbered.txt

2. Given the text file DNA_combined_numbered.txt, find out the starting locations of the even
numbered specimens and redirect this output to the text file even_specimens.txt. You can do this
one by one.
$ grep -n '>Specimen2' dna.fasta > even_specimens.txt
$ grep -n '>Specimen4' dna.fasta >> even_specimens.txt
$ grep -n '>Specimen6' dna.fasta >> even_specimens.txt

3. Using one or more UNIX commands, extract the DNA of Specimen2 (including the line that starts
with >Specimen2) from the text file DNA_combined_numbered.txt and write the result to the FASTA
file specimen2.fasta.
$ head -n88 DNA_combined_numbered.txt | tail -n11 > specimen2.fasta

14.3

1. How many output types are there in a UNIX system? Investigate and explain the difference(s)
between the output types in a UNIX system.
# In UNIX-based operating systems, there are two types of outputs
# Output 1 means standard output (stdout)
# Output 2 means error output (stderr)

2. Explain what /dev/null is. Is it possible to redirect output to /dev/null? If that is possible, what
happens when output is redirected to /dev/null? Investigate and explain.
# /dev/null is the null device, discarding all data written to it, and thus
# acting, to some extent, as a trashbin (i.e., as the proverbial black
hole)

3. What is the meaning of 2>/dev/null? Investigate and explain.


Erronous output is redirected to the null device

14.4

1. Using UNIX command(s), perform DNA to RNA mapping for the nucleotide symbols available in the
text file DNA_combined_numbered.txt. Store the output obtained into a text file that is named
RNA_combined_numbered.txt.
$ tr 'AGCT' 'UCGA' < DNA_combined_numbered.txt > RNA_temp.txt

2. Remove the symbols > in RNA_combined_numbered.txt and replace them with @.

3. Remove unnecessary - characters on the last line.


$ sed -e 's/^>/^@/p;s/-//g' RNA_temp.txt > RNA_temp2.txt

4. For each specimen, append a new line after its RNA sequence, and where this new line consists of
++++++++++++++.
$ sed '12~11 s/$/\n++++++++++++++++++/g' < RNA_temp2.txt > final.txt

14.5

1. Read dna1.fasta.

2. Extract the DNA of Specimen3.

3. Get rid of non-DNA characters (e.g., -------).

4. Remove newlines and represent the whole DNA sequence using a single line. In this context, you
may assume the use of a UNIX-formatted text file (that is, the end of a line of text is represented
using the UNIX convention, as explained in the theory).

5. Find the complement of the DNA sequence, replacing A with T, C with G, T with A, and G with C.

6. Calculate the reverse of the outcome.

7. Write the output to the text file reverse_comp_specimen3.txt.

$ cat dna1.fasta | head -33 | tail -10 | tr --delete '\n' | tr


--delete '-' | tr 'ACTG' 'TGAC' | rev > reverse_comp_specimen3.txt

Series 15

1. List only the sequence (or line) that contains the start codon. Show the corresponding line number.

$ grep -nm1 "ATG" pfizer_vaccine.txt

2. List only the sequence (or line) that contains the stop codon. Show the corresponding line number.
$ grep -n '^TGATGA' pfizer_vaccine.txt

3. List all the sequences (or lines) that have any three identical nucleotides at the beginning of the
line. Show their corresponding line number as well.

$ grep -nE '^(.)\1{2}' pfizer_vaccine.txt

4. List all the sequences (or lines) that have any two identical nucleotides at the beginning of the line,
and where the same lines have any two identical nucleotides at the end of the line.

$ grep -nE '^(.)\1{1}' pfizer_vaccine.txt | grep -E '(.)\1{1}$'

Or

$ cat pfizer_vaccine.txt | grep -n '^\(.\)\1' | grep -n '\(.\)\1$'

1. List only the sequences (or lines) that contain the spike-encoding region.
$ sed -n '2, 42p' pfizer_vaccine.txt

2. Select only the sequences (or lines) that contain the start codon and the stop codon, and save
them into a new text file that is named start_stop_codon.txt.
$ sed -n -e '1p' -e '43p' pfizer_vaccine.txt > start_stop_codon.txt

3. Given the text file start_stop_codon.txt, only change the start codon into UUU.
$ sed '0,/ATG/s//UUU/' start_stop_codon.txt
Or
$ sed ‘1s/ATG/UUU/g’ start_stop_codon.txt

15.2
# 1: $ grep ':PROFESSOR-1' rc4_data.txt -A 7
# 2: $ grep '^[a-zA-Z]' rc4_data.txt -A 6
# 3: $ grep '^[a-zA-Z]' rc4_data.txt
# 4: $ grep '^[0-9]' rc4_data.txt | sed 's/^.\{3\}//g'
# 5: $ sed '13i 5. FirstName LastName' rc4_data.txt

$ awk '{ print $1 }' icd.txt | awk -F '.' '{ print $2 }' | grep '[^0-9]'

15.3

1. Download the FASTA file NC_003070.fna to your home directory on Helios, using the URL provided
below:
$ wget https://users.ugent.be/~wmdeneve/informatics/NC_003070.fna
2. Compress the FASTA file NC_003070.fna using a command running in the background. Also, upon
logging out, this command needs to continue its execution.
$ nohup gzip NC_003070.fna &

3. Count the number of lines and characters available in the compressed FASTA file, without
decompressing this FASTA file in your home directory.
$ zcat NC_003070.fna.gz | wc -lc

You might also like