Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
change directory from current directory to directory with name Mail go one directory up in the directory hierarchy, e.g., from /Users/Mail/Fritz to /Users/Mail brings you back into the directory where you came from / ./ or type name of subdirectory or simply cd
absolute path names begin with a slash relative path names begin with Navigating to your home directory
cd ~
If you are Fritz and you are currently in any arbitrary directory, e.g., in /Users/Maria you can can navigate to your directory Mail in the two following ways: cd cd Mail cd ~/Mail If you are Fritz and you want to see whats on Marias Desktop then type: cd ~maria/Desktop ls
rm -r Maria
(You get information about reading, writing, executing privileges, file size and date) If you want to now what options a command comes with, e.g., ls, then inquire like this: man ls in the manual pages (or short man pages) Man pages tend to be incomprehensible, at least to me, but they can often help your memory. If you want to combine two or more options, you do not need to repeat the minus sign, e.g., the two following commands are equivalent: ls -la and ls l -a -a in the list command plots all files, even those that are hidden, i.e., those that start with a period. For example .bash or .bash_history
Some trivia
The reason why Linux is relatively safe is that regular users have no write privileges in sensitive areas, e.g., in the directories where the operational system is located. In addition, when you execute a program, the program inherits your priviledges and not those of the superuser, who is allowed to do essentially anything on a Linux box.
Exercises:
1. Create a directory tree, which you would find useful, or one that is similar to what is on your Windows PC 2. Create directories Mail/Dieter and Masters/Experiment/200912 and Masters/Experiment/201001 3. Go into directory 201001 and change your working directory directly to Dieter. 4. Repeat the previous exercise and see what happens when you type: cd ~/Mail/D and then hit the [tab] key (no space between D and tab) The [tab] key is what tends to make the command line faster than the mouse! 5. Find all files (directories) in your home that are hidden. What could their purpose be? 6. Can you say who can access your home directory?
head -7 my_first_file similar to more but only for the first seven lines tail -7 my_first_file similar to more but only for the last seven lines A difference between more on one hand and head and tail on the other hand is that head and tail do not display the text in screen quanta. The latter can be changed as follows: head -128 my_first_file | more In Linux, the symbol | is called a pipe. It means that the output of the command (here head) is not to be sent to the standard output (usually screen), but into a pipe (meaning a file somewhere on a temporary directory that is not in your directory tree). The data then awaits instructions as to what to do with its content (here display it in screen quanta with more).
Some trivia
In principle, you need nothing but the commands echo, head, and tail to edit an ascii file. The only thing you would still need to learn is how you hide quotation marks when using echo. However, it is wise to resort to editors. Some of them are mentioned in the appendix. Even so, these three commands come in handy many times.
Exercises:
1. 2. 3. 4. Copy your file my_first_file from your home into your directory ~/Mail/Dieter. Delete the file my_first_file in while being in your home. If you ever typed out my_first_file, then repeat exercise 4 from the last chapter. If you had a long data file, and you were only interested in lines 50-55, how would you display them? And append them to an existing file? Hint: The command wc my_first_file will be useful. What does it do?
Each number defines the privileges for you/your group/ the world. r = 4 , w = 2 , x = 1. 7 = 4 + 2 + 1 is equivalent to rwx 5 = 4 + 0 + 1 is equivalent to r x 4 = 4 + 0 + 0 is equivalent to r In our example, the world may read/copy but not execute the code (on your machine).
Exercises
1. Create directories Dieter and Maria and Xaver in your ~/Mail directory. Allow yourself to access Dieter, to read the file names in Maria but not to acces it, and to change into Xaver, but not to be able to list the files.
2. Create a directory ~/myprograms. There design a shell script called make_my_file that creates an empty file. 3. Include myprograms in the list of directories to be searched by Linux.
IV Links
They are similar to links under Windows. The details are not important in a 4-hour tutorial.
Prompt
It is nice to know the name of your working directory. You can see it if you include the following line in .bashrc PS1="\W\$ " PS1 is another shell variable
Exercise
1. Create an alias and an executable in your bin directory that both have the same name, e.g., myls. (They may be doing different things.) Which one is executed by the shell when you type myls?
VI Job handling
Finding job IDs
ps aux shows the users and process ID (PID) of others and myself if you skip the a, youll only see yours 6
top
Killing jobs
kill 36549 allows the job to with PID 36549 terminate certain input/output kill -9 36549 kills the job without further ado mycommand [CTRL C]
machine), you can create an alias or shell script, so that you only have to type something like my_scp_mars filename and your done. No mouse clicking and navigating through directories necessary._ scp p localfile your_user_name@foreignhost.univ.edu:Mail/. The option p preserves the file properties (date of creation, privileges, etc.) of your original file on your local computer, which copies it to your Mail directory in the home of the remote computer. If you want to copy from a remote computer to a local one, type something like: scp r your_user_name@foreignhost.univ.edu:Mail/. ~/Archive/. The option r means recursive copying, that is, the Mail directory with all its subdirectories including their contents will be copied. If the directory contains a lot of data, you may want to zip it up, which you can do on the command line. See appendix.
IV Miscellaneous
History
Each shell creates a history file, which saves the last few commands that you typed into the command line, e.g., .bash_history in your home. Assume you want to repeat a previous scp command that you used, then type history | grep scp then your history file will be dumped into a pipe (see section II). The command grep will make sure that only those lines will be displaced As a result of the history command, you will get lines such as: > 421 scp file1 fritz@zuse.de:. > 430 scp file2 maria@zuse.de:. If you want to repeat the first out of these two option, then simply type
! 421
If you want to repeat the last out of these two option, then simply type ! scp executes the last in your .bash_history that started with scp. Never ever type !r or !rm These are absolute no goes! because you want to avoid the worst case scenario, which would be that the last command starting with r was rm r * and you are currently in your home or as root in the root of the directory tree. It is actually safest to type rm some_very_strange_letter_combination after you used rm together with a wild card. The history command is another reason why you can perform tasks that repeat very often extraordinarily quickly. Even if you have to change some parts of the last scp command, you could mouse those that you want to copy.
Wildcards
Sometimes you want to list or find files that satisfy certain search criteria, for example, all files that have the name fritz or Fritz in them. Then you can use wildcards (space fillers) to substitute for all other letters. For example, you could type ls *[fF]ritz* ... lists all files that contain either fritz or Fritz While * substitutes for any number of letters, ? will replace any single letter. For example ls *.f9? ... would list all Fortran 90 and Fortran 95 source codes (plus potentially other files), as those end with .f90 and .f95. respectively.
Finding files
Want to see a listing of all word files in a directory tree (such as home): find . name *.doc print the . tells linux to search in your working directory keep in mind that you can pipe this command or redirect into a file, which would be great if you wanted to make an inventory of all mp3 files, for example.
Change ownership
chown newowner file1 file2 file3 changes ownership of said files (directories) to said user. You must have permissions to change the ownership, i.e., be owner or root.
Change Passwd
Simply type on the command line: passwd you will be asked to type the old password once and the new one twice.
Viewing files
Postscript files: PDF files: jpegs, gifs, : ghostview gv, .. with xpdf, preview, gv gimp gv myfile.ps xpdf myfile.ps gimp myfile.jpg
A: Editors
pico / gedit Easy to use. Mostly for beginners. emacs
The standard choice for many developpers. Pretty efficient many shortcuts.
vi / vim
Awful in the beginning, but extremely fast if you know a few short cuts. My absolute personal favorite. 10
Most Linux now comes with a Windows like surface. If you right click onto a file on your desktop that ends with .txt, then your Linux will offer you text editors. In most cases, you should be able to execute that text editor also from the command line. .
B: Plotting / gnuplot
Webpages : http://www.duke.edu/~hpgavin/gnuplot.html gnuplot is a great program to display and fit data quickly and also does an OK job with 3d plots. For final figures, that is, those that make it into your thesis or into a publication, I would recomment xmgrace or something else (mathematica, maple,) To invoke gnuplot simply type: gnuplot
Elementary plotting
gnuplot> help plot gnuplot> plot cos(x) check that out, it is pretty self-explanatory known special functions are, e.g., exp, log, sinh powers are indicated as in fortran with ** otherwise, syntax is closer to that of C, C++ set xr [0:2*pi] plot data.dat u 3:2 w lp u is shorthand for using u 3:2 indicates that you plot 3rd column as x and 2nd column as y w lp is shorthand for linepoints default is to plot 1:2. You can also plot 0:2 (0 indicating the row number). If your file only consists of one column, the 0:1 is default. set log y unset log y plot data.dat u ($1-0.5):($2*log($3)) w lp When you perform mathematical operations on a column, you have to proceed it with a $ sign. Here we plot column 1 shifted by 0.5 as the x-axis and the product of column 2 and log of column 3 as y-axis. plot data.datw d, cos(x) You can plot several data files set xl pressure (GPa) xl is shorthand for xlabel [page up/down] Brings back the previous/next command. Which is usually to be preferred to (unless youre done and plot into postscript or something else) replot self-explanatory quit self-explanatory
gnuplot> gnuplot>
gnuplot> gnuplot>
11
3D plots
gnuplot> splot data.dat u 1:2:3 There are many ways how you can modify 3D plots, which are learned quickly. These will be included in later iterations of this tutorial.
Defining variables
gnuplot> a=1.57 now you can simply use a as a variable. Certain letter combinations cannot be use as variable names
12
Fitting
gnuplot> fitf(x) = fp1*x**2 + fp2*x**3 gnuplot> fp1 = 1 fp2 = 1 Defines a function f(x) and defines and initializes parameters fp1 and fp2. gnuplot> fit fitf(x)data.dat u 1:2 via fp1, fp2 This will perform the fit and assign new values to the fitparameters. gnuplot> plot fitf(x),data.dat will plot the original function and the fitted values.
Exercises
1. Plot exp(sin(x)) in the range from 0,..,pi. Define a title of the plot as follows: gnuplot> set title I feel bad Write the output as postscript into a file myplot.ps. View the file with ghostview of equivalent from within gnuplot. Edit the postscript file from within gnuplot and change bad with good. 2. Download the data file datensatz.dat from http://www.lms.uni-saarland.de/Lehre/09ECMS/Week05/datensatz.dat Fit the data set to the function f(x) = a x2 + b x3. 3. Create a nice looking jpeg graph from the fit.
C: Awk
Great program to manipulate data on the command line and also to do some moderate programming. Many commands are similar to C++, but it does not need to be compiled. awk '{print $1 "\t" $2*$1^2} < input.dat > output.dat \t < > produces a tab standard input (usually keyboard) comes from file input.dat standard output (usually screen) goes to output.dat
13