CHE3935 Lecture 1 Introduction to Linux 1
Logging In PuTTY is a free telnet/ssh client that can be run without installing it within Windows. It will only give you a terminal interface, but used with a PC X Window server and tunneling X11 through ssh should be able to launch graphical applications as well Open PuTTY by going to C:\tools and double clicking on putty.exe In the Host Name box type puccini.che.pitt.edu Type your pitt.edu username at the login as: prompt Your initial password is username @new12 Change your password by typing yppasswd and following the instructions Make sure to choose a secure, strong password that is not the same as your pitt.edu password. 2
Some Basic Commands (1) See http://collab.sam.pitt.edu/dev/karls-course for more detailed instructions There are many different Linux shells you will be using the bash shell (bournagain shell, free replacement of the Bourn shell, and combines, bashes together, features of sh, csh, and ksh shells) The commands you type at the command line prompt are bash commands First command: ls -la type this and see what you get This is a directory listing (many commands are like abbreviated words without vowels, ls = list in this case) The -la part adds options. Compare by typing ls What do you get? Second command: man ls The man command lists manual pages for all bash commands, so man ls gives the manual page for the ls command. Look at the manual page for ls and find out what -la does. Hint: Move forward and backward on the man page by using the f (or space ) and b keys. Hint: You can search for text within the man page by pressing the / key and then typing text to match. Note: The search is Case Sensitive! Type n for the next search match and p for the previous search match. 3
Some Basic Commands (2) Locating commands: which ls Now try ls /bin Note that /bin is a directory (folder). Changing directories: cd /bin puts you into the /bin directory, which you do not own, but you can visit. Find out where you are: pwd stand for print working directory. Now type cd followed by pwd to see where you went. Making directories can be done with the mkdir command. Type man mkdir to see how it works. Make a directory called tmp by typing mkdir tmp Type ls l to see your newly created directory then cd tmp to go into that directory. Type ls and ls -la to see what is in your directory. 4
Some Basic Commands (3) So far you don t have any files, so let s create some to play with. Type the following (in your tmp directory): echo This is my first file > file1 Now type ls -l to see your file and cat file1 to dump the contents of the file to your screen. The cat command concatenates one or more files and sends them to the standard output. Note that you can redirect the output to a file as well. For example, create another file by typing echo This is another file > file2 Now type ls -l to see that both files are there. Now you can cat the contents of both files to a third file: cat file1 file2 > file3 Now type ls followed by cat file3 to see what you have done. 5
Some Basic Commands (4) File manipulation: Moving, copying, and deleting files can be accomplished with the mv, cp, and rm commands, respectively. Try them and see how they work! Use the man pages if you need to. Note that these commands are like driving without seatbelts or airbags. E.g., if you do cp file1 file2 you will clobber file2 replacing it with file1 with no warning. We will now change this behavior. Type cd to get to your home directory then type the following: echo "alias rm='rm -i'" >.bash_aliases echo "alias cp='cp -i'" >>.bash_aliases echo "alias mv='mv -i'" >>.bash_aliases source.bash_aliases Now try these commands and see what happens. The.bash_aliases file should be read whenever you log in from now on so that you can drive with some seatbelts. 6
Text Editing With vim(1) There are many text editors available in the Linux environment, but vi (or vim=vi improved) will work on any terminal and is something everyone claiming Linux expertise should know. There are many vi tutorials on the web. E.g., http://linuxconfig.org/vim_tutorial I encourage you to try these out. The thing about vi that will take some getting used to is that it does not behave like any text editor you have used before (probably). It has two modes of operation: (1) visual mode (where you can move around in the file, issue keyboard commands to delete characters, words, etc., issue commands on the command line, etc., and (2) insert mode where you can type text into the file. This is fundamentally different from all modern text editors. The reason for this is that these modes allow you to use any keyboard to run vi as long as it has the esc key and the regular alphanumeric keys. You don t need arrow keys, a mouse, or anything else. This was very useful in the olden days when keyboards didn t have arrow keys and a computer mouse was a rodent who programmed. Let s copy a file from somewhere so we can practice editing stuff with vi (which is actually vim on puccini) cp ~webpages/web-docs/johnson_pub.html. ls l 7
Text Editing With vim(2) Edit the file: vi johnson_pub.html You are automatically in visual mode when you open any file. You can move the cursor around using the hklj keys, with h=left, k=up, l=right, j=down. Try it and see what happens. You can also use the arrow keys. Now let s quit the file without saving changes. Type :q! and you should be back at the bash command line. The : means go to the command line within vi, q means quit and! means discard any changes. Get back into the file and let s learn some more features. Edit the file: vi johnson_pub.html There are many things you can do in visual mode. Delete characters: x=delete the character under the cursor, X=delete character to the left of the cursor (note that vi commands are case sensitive) Delete words: dw Delete from cursor position to end of line: d$ or D Move to the beginning of a word: w Move to the end of a word: e Repeat a command n times: ncommand E.g., 15x will delete 15 characters, 7w will move forward 7 words, etc. Undo a command: u Try these out! 8
Text Editing With vim(3) Now you have made changes to a file you can either quit without saving or you can save the file (and quit). Try typing :q and see what happens. Now type :wq to write and quit. A shortcut for wq is x, i.e., :x does the same thing as :wq You can also save the file at any point by typing :w and then continuing with editing the file. Saving the file with a new name: :w filename Edit the file again and let s play some more. Let s enter the insert mode. You can do this in a number of ways. Insert at cursor position: i Append at end of line: A Open new line below current line: o Open new line above current line: O To get out of insert mode, hit the esc key. Try it. Note that you can move around using the arrow keys, but not the hjkl keys (for obvious reasons). Practice going in and out of insert mode to get the hang of it. It becomes very natural after some practice. You can repeat the last command by typing. in visual mode. Try it by inserting a word, hit escape, then move the cursor and type. to insert the word again in a new place. 9
Text Editing With vim(4) Let s go back to visual mode and learn some more commands Delete a line: dd Yank (copy) lines: yy Paste anything just deleted or copied: p or P Replace a single character: r or multiple characters: R Go to end or beginning of line: $ or 0 Go to end of file: G Go to beginning of file: gg or 1G Go to line n: ng Execute a shell command within vi: :!cmd, e.g., :!ls -la will list all files in the current directory Read in a file to the current file: :r filename Edit a different file: :e filename Try these commands 10
Text Editing With vim(5) Search and regex: vi uses regular-expressions (regex) in searching. This is a very powerful pattern language. See http://www.regular-expressions.info/ for details. We will only scratch the surface of regex Searches in vi are case sensitive by default. To ignore case type :set ic in visual mode. Basic search: /regex or?regex where regex is the expression to search for. E.g., search for Zhang in the file by typing /Zhang Find next or previous: n or N Examples of regex search: /[1-4] finds any number in the range 1-4. /<sub>\d finds the characters <sub> followed by any digit. \[A-Z] finds any upper case letter, etc. There are many other things you can do with regex searching. 11
Text Editing With vim(6) Search and replace Replace the first occurrence of str1 with str2 on a single line: :s/str1/str2 Replace all occurrences on a single line: :s/str1/str2/g where g=global Replace all occurrences in a file: :%s/str1/str2/g Confirm each replace: :%s/str1/str2/gc Replace all occurrences between lines n and m: :n,ms/str1/str2/g, e.g., :10,50s/Johnson/Nosnhoj/g replaces every occurrence of Johnson with Nosnhoj anywhere in the range of lines 10 to 50. Try these commands 12
More Shell Commands The shell provides scripting capabilities that are very powerful. See http://linuxconfig.org/bash_scripting_tutorial for an introductory tutorial To see some examples of scripts that are run every time you login go to your root directory and edit the.bashrc file: vi.bashrc Note the alias commands, note the case esac pairs. Try to figure out what each line does. Be careful not to change this file or you could mess up your shell environment. In order to run scripts the files containing the scripts must be executable. You can make any file executable by using the chmod command. This command controls all the file attributes or permissions. See, for example, http://en.wikipedia.org/wiki/chmod 13