Download Introduction to Linux: Exercises and more Summaries Family Law in PDF only on Docsity!
Introduction to Linux: Exercises
In these instructions the first character "$" in the command examples should not be typed, but it
denotes the command prompt.
Some command lines are too long to fit a line in printed form. These are indicated by a backslash “\” at
the end of line. It should not be included when typing in the command. For example
$ example command
continues
and continues
Should be typed in as:
example command continues and continues
0 Download and unpack the exercise files (do that first time only):
Go to the workshop home page (www.csc.fi -> click at the workshop name link at the right column, scroll
down and download the tar archive from the link, save as …)
Open a terminal, cd to the folder where you downloaded the archive, unzip and untar the file:
$ tar xzvf linux-exercise.tar.gz
The v (verbose) flag in the tar command shows the files with the path that are untarred. You see, that
you'll get a number of files in a subdirectory named linux-exercises. Go to that directory with the cd
command.
1 Moving around in the directory tree
Metadata: commands in this exercise: cd, mkdir, ls, mv, more, less, cat, tar.
Metadata: The aim of this exercise is very simple: learn to move around with cd, look at file contents,
create directories and move files around.
0) Unpack the dirs.tar.gz file
$ cd moving-around $ tar zxvf dirs.tar.gz
1) Find out what directories and files were created
$ ls $ ls - l
$ cd inputs $ ls - l $ cd .. Etc.
2) Which output file is about caffeine?
Go to the result subdirectory (which itself has subdirectories). The pdb-files have a line with "TITLE
somename". Below are some commands that you can try find out which file is about caffeine. What are
the other structures about?
$ grep caffeine *.pdb $ grep caffeine /.pdb $ grep title /.pdb $ grep TITLE /.pdb
You can view the complete file contents with either more, less or cat.
3) Create subfolders
There are only three result* - directories. Create new ones: result4 and result5 for outputfiles 4
and 5 and move the outputfiles (out_4.pdb, out_5.pdb) to those directories.
cd to the outputs - directory
create a new subdirectory with mkdir result
move the out_4.pdb with the move command mv result3/out_4.pdb result
repeat for result5 and out_5.pdb
4) Create a new compressed tar file of the new directory hierarchy
Go to the moving-around directory where the original tar file is (dirs.tar.gz) with cd.
$ tar zvcf dirs-new.tar.gz inputs outputs
5) Confirm the tar file contents
$ tar ztf dirs-new.tar.gz
2 Use the man command to find flags for ls
Metadata: commands in this exercise: man, ls
Metadata: learn how to find detailed info about flags, and sort ls output
1) Open the ls man page
$ man ls
This opens the man page for ls. As there are a lot of options for ls, it is useful to search the man page.
Search is triggered by pressing / and then writing a (start of) a keyword. Pressing "enter" triggers the
$ ls ???????.pdb
2) Limit listing of the output files containing a range of numbers
Go to the moving-around/outputs directory. List all pdb files in the subdirectories.
$ ls /.pdb
In this command the first "*" tells to look at all subdirectories in the current directory, and the second
"*" all strings Now limit the list to only those out-files that have a number 2-5 in their name:
$ ls */out_[2-5].pdb
What is the difference to this command?
$ ls /[2-5].pdb
4 Simple backup scripts
Metadata: commands in this exercise: cp, mkdir, date, echo
Metadata: simple script that makes backup of a sup-directory to freshly created sub-directory
Change into the sub-directory linux-exercises/backupscript. In that directory you should have
the following files that are the solutions (so don’t open) for the following exercises:
enhancedbackupscript.sh simplebackupscript.sh
1) A simple backup script
Based on the start from our befriendly.sh example, create a script that copies all files with a suffix
(e.g., test.dat) from your home directory automatically to a directory in /tmp/homebackup that is
first created by the same script. Use wildcards for that. Try to place some verbosity into the script by
using the echo-command, e.g.,
$ echo "Starting copying files"
Take also of the advantage of the possibility to preserve the date of the file by adding a – p to the cp
command. N.B.: if you also add a - u to the copy command, then you would make sure that upon
multiple runs of the script only files of same newer would replace the one in the backup directory. This
would be kind of an (on terms of whole files) incremental backup.
2) More versatile backup script
Using the possibility to store the output of a command in a local variable, create a directory-name that
includes the current date:
destination=/tmp/homebackup_$(date +%Y-%m-%d)
And rewrite the script to create dedicated backups that are distinguishable by this date. Hint: You can
then use the variable in connection with the mkdir command simply by $destination.
5 Linux command line exercises
Metadata: commands in this exercise: grep, tar, cat, cut, more, less, awk, sed, wc,
sort, gnuplot
Metadata: example files from computational chemistry
Change into the linux-exercises/chem directory. In that directory you should now have these files:
dimer.log : Gaussian quantum chemistry geometry optimization log file for a water dimer
dimer_scan.log : Gaussian log file for relaxed potential energy scan for stretching water dimer
distance
freq.log : Gaussian log file for a frequency calculation
cp2k.out : cp2k calculation ascii output file
cp2k.xyz : xyz format molecular structure file of liquid water
1) have a look at the contents of some files
$ more cp2k.out $ nano cp2k.out …
2) How did the total energy change in the water dimer optimization run?
(dimer.log)
The part in the log file where the energy has converged is shown below. The final energy is printed on
the shadowed line.
Cycle 12 Pass 1 IDiag 1: E= - 152.637052486060 Delta-E= - 0.000000000001 Rises=F Damp=F DIIS: error= 7.20D- 08 at cycle 5 NSaved= 5. NSaved= 5 IEnMin= 5 EnMin= - 152.637052486060 IErMin= 5 ErrMin= 7.20D- 08 ErrMax= 7.20D- 08 0.00D+00 EMaxC= 1.00D-01 BMatC= 2.02D-13 BMatP= 1.38D- 12 IDIUse=1 WtCom= 1.00D+00 WtEn= 0.00D+ Coeff-Com: 0.795D- 03 0.448D- 01 0.134D+00 0.411D+00 0.410D+ Coeff: 0.795D- 03 0.448D- 01 0.134D+00 0.411D+00 0.410D+ Gap= 0.182 Goal= None Shift= 0. RMSDP=8.86D- 09 MaxDP=9.24D- 08 DE=-1.08D-12 OVMax= 9.92D- 08
SCF Done: E(RPBE-PBE) = - 152.637052486 A.U. after 12 cycles NFock= 12 Conv=0.89D- 08 - V/T= 2. KE= 1.517485854820D+02 PE=-4.434279894947D+02 EE= 9.792090969701D+ Leave Link 502 at Thu Jan 29 09:51:27 2015, MaxMem= 33554432 cpu: 2. (Enter /appl/chem/G09RevD.01/g09/l601.exe) Copying SCF densities to generalized density rwf, IOpCl= 0 IROHF=0.
How to get all those lines out?
$ grep "SCF Done:" dimer.log
What is the shortest string to grep that gives only these lines?
What does wc (short for word count ) print out? Can we give it some flags? ( try man wc, or google for it )
How many atoms in total? ( we know it's only hydrogen and oxygen atoms, i.e. O and H )
$ grep - E "O|H" cp2k.xyz | wc - l
You could also count all the lines in the file and subtract the first two, which don't represent atoms.
$ wc - l cp2k.xy
What if your structure had also osmium atoms (Os). How would you change your commands?
7) How long did one iteration in cp2k.out take?
The part in the output file that shows timing is like this:
ENSEMBLE TYPE = NVE STEP NUMBER = 1 TIME [fs] = 0. CONSERVED QUANTITY [hartree] = - 0.880615921354E+
INSTANTANEOUS AVERAGES CPU TIME [s] = 290.20 290. ENERGY DRIFT PER ATOM [K] = - 0.810224161417E+02 0.000000000000E+ POTENTIAL ENERGY[hartree] = - 0.880838552803E+04 - 0.880838552803E+ KINETIC ENERGY [hartree] = 0.222631448393E+01 0.222631448393E+ TEMPERATURE [K] = 305.326 305.
You could try this to get the timing:
$ grep TIME cp2k.out
But that gives too many hits. To get only the line that has the wall clock time spent at each time step
give:
$ grep "CPU TIME" cp2k.out
Is energy drift per atom speeding up? How to get access to those lines?
8) How to plot those times?
First direct them to a file
$ grep "CPU TIME" cp2k.out > times
Confirm they are there:
$ more times
Start gnuplot with gnuplot and give
plot 'times' using 0:5 with points
or with lines
plot 'times' using 0:5 with lines
Explanation: in gnuplot the command is "plot", followed with the filename that has the data to
be plotted (as it is a string, it needs to be quoted), "using" tells gnuplot to use the following
columns in that file, "0" means the line number (first line=1, second line=2,… this will be the x-
axis), ":" means to plot the second column as the function of the first column, "5":th column will
be the y-axis, "with" is followed by what to plot at the coordinates, now it's "points" i.e. some
symbols.
Exit with quit
How to plot the energy drift per atom?
9) What is the average temperature based on cp2k.out?
The part in the output file that has this information looks like this:
ENSEMBLE TYPE = NVE STEP NUMBER = 1 TIME [fs] = 0. CONSERVED QUANTITY [hartree] = - 0.880615921354E+
INSTANTANEOUS AVERAGES CPU TIME [s] = 290.20 290. ENERGY DRIFT PER ATOM [K] = - 0.810224161417E+02 0.000000000000E+ POTENTIAL ENERGY[hartree] = - 0.880838552803E+04 - 0.880838552803E+ KINETIC ENERGY [hartree] = 0.222631448393E+01 0.222631448393E+ TEMPERATURE [K] = 305.326 305.
First we want to grep all lines with the temperature:
$ grep TEMPERATURE cp2k.out
To get the average we can process the output of the grep command with awk and create a cumulative
sum of the column with the temperature (+= adds the value in column 4 to the current value in variable
a), then print that instantaneous value (in column 4), the number of records processed NR and the
average up to that point:
$ grep TEMPERATURE cp2k.out | awk '{a+=$4;print $4,NR,a/NR}'
This could also be done without separate grep command with
$ awk '/TEMPERATURE/{a+=$4;n++;print $4,n,a/n}' cp2k.out
Explanation: here we use a counter "n", which is incremented each time the condition is met (the
line has the string TEMPERATURE. n can then be used to calculate the cumulative average. In this
case NR counts all lines in the file cp2k.out so we can't use that to calculate the average.
How to plot both the instantaneous temperature and the cumulative average? ( hint: in gnuplot if you
use replot instead of plot, the previous plot is retained )
10) What is the smallest x-coordinate in cp2k.xyz?
$ awk - F ";" '{print $1 """}' tmp_gene_id > tmp_trimmed
First we tell awk to use ; as the field separator. $1 now matches everything up to the first ; (i.e.
until the gene_id code). Getting the " in place is a bit tricky. As " has a special meaning (it is
not just a character) we need to escape it with \ to mean just-the-character- " and not the
meaning of " and finally quote that with " :s. An alternative way to do this in two steps is to use
cut to leave out everything after the first occurrence of ; and then print the trailing " with awk
(as above).
$ cut - d ";" - f 1 tmp_gene_id | awk '{print $0 """}' > tmp_trimmed
6. Sort the file by chromosome and by miRNA start position (4. column). Make sure to sort the
chromosomes in numerical order (-n), not in alphabetical ( i.e. 1,2,3... not 1,10,11..)
$ sort - k1n,1 - k4n,4 tmp_trimmed > hsa.edited
Extra task. Make another file that only has entries from chromosome 2
It’s possible to do the above in single command line, i.e. passing the output from the previous command
as input to the next, but usually it is safer to use temporary files (until you know each of the steps work).
Here is a one-liner that does steps 1-6:
$ grep - v "#" hsa.gff3 | grep - v "miRNA_primary_transcript" | cut - c 4 - |
sed s/ID=/'gene id "'/ | awk - F ";" '{print $ 1 """}' |
sort - k1n,1 - k4n,4 > hsa.edited
CSC Quick Reference to Unix commands and CSC Computing Environment 2016-06-02 1/
CSC Quick Reference
Getting help
man program (manual pages) apropos stuff (looks for stuff in all documentation) firefox (html browser) CSC services for researchers: http://research.csc.fi module load application (initialize the environment of an application ) module avail (list applications on current server, module spider on Taito) module list (list of loaded applications) module purge application (remove application environment) FAQ: https://research.csc.fi/faq-knowledge- base General Guide: http://research.csc.fi/csc-guide
Unix commands
ls (list directory) less (print a file to the screen, quit with q ) cp (copy a file) rm (delete a file) mv (move or rename a file) cd (change the current directory) cat (sends the file to standard output) pwd (print name of the current directory) mkdir (create a directory) rmdir (delete a directory) exit (quit the session) passwd (change password) history (list all commands given previously) ! stuff (executes the last command that started with "stuff") head nobel.tex (list ten first lines of the file nobel.tex ) tail -100 nobel.tex (list the last hundred lines of the file nobel.tex ) tail -f nobel.tex (keeps listing the end of file nobel.tex. Handy for following an output file when lines are appended to it.) grep stuff nobel.tex (print lines containing the word stuff from the file nobel.tex ) ls -la > file (output of a command to a file) ls -la | grep " nobel " ( Chaining (piping) multiple commands) tar cvf nob.tar nobel.* (make a tar-file nob.tar from all files whose names begin with nobel You can also tar a directory_._ ) tar xvf nob.tar (extract all files from the tar- archive nob.tar ) gzip nob.tar (compress file nob.tar to save space) gunzip nob.tar.gz (uncompress file nob.tar.gz )
File Transfer scp computer1:file1 computer2:file2 (copy files from computer1 to computer2 ) An example of scp usage: scp nobel.tex [email protected]: (copies the file nobel.tex (from current directory (see pwd above) to machine top.univ.fi ) Because the directory in the target machine was not specified the file goes to the home directory of user laureate .) You can also install a graphical file transfer program e.g. FileZilla iput file (copies file to the iRODS archive) iget file (copies file from iRODS archive to current directory) ils (shows content of your iRODS archive) Files can be copied also using the web browser via the Scientist's User Interface: https://sui.csc.fi
Networking ssh computer (open a new secure session) In Linux use ssh -X computer or ssh -Y computer to enable X-connection In Windows use NoMachine client instead as Windows does not have native support for remote graphics (much better than X- emulators like XMing) dos2unix changes Windows (DOS) format text files into Unix-format.
Paging With less less file (print a file to the screen) ls -la | less (page the output of a command) [return] (next line) [space] (next screen) b (previous screen) /stuff [enter] looks for the next occurrence of "stuff", n gives the next, n the next, ... h (list the commands of less) q (quit the less program)
Computers sisu.csc.fi (Cray XC supercomputer for massively parallel jobs) taito.csc.fi (HP supercluster for serial and parallel jobs) taito-shell.csc.fi (HP supercluster, interactive use) http://research.csc.fi/csc-s-servers nxkajaani.csc.fi NoMachine gateway to CSC's servers. Some GUIs directly available.