Computer Science 150
Course Info
Unix Online Documentation
Misc. Information
Lab 3: Intermediate Usage of the CEE/CS Department Server
References:

The purpose of this lab is to learn more intermediate commands for the department server, such as shortcuts to move around files in vi, how to use shell wildcards, how to search files for patterns, how to transfer files to and from the department server, how to archive files, how to print files, and how to forward your Odin email to another email address.

INTERMEDIATE VI/VIM COMMANDS

The basic vi/vim commands given in Labs 1 and 2 will be enough for you to create and edit source code files, but there are many more vi/vim commands that make it faster and easier to edit your files. While these commands are more difficult to learn, they can be very handy. Refer to the resources links at the top of the page for a list of vim commands. This lab will cover just the more common and useful commands.

Computer Science majors should learn all the commands in this section. You will need to know how to use these vim commands for other Computer Science courses. Other students are encouraged, but not required, to learn the commands in this section.

Almost all command mode commands can be modified by a number. This will cause the command to repeat that many times. So if you give the number 5 followed by the delete word command, it will delete five words. This is a powerful, but sometimes accidently activated feature of vim. If you accidently hit a number before a command, hit the ESC key to cancel the number. Any changes you accidently make in command or last-line mode can also be undone with the u (undo) command.

Commands to move around the vim buffer:

  • G (uppercase G) - Goes to the last line in the file
  • :5 - Goes to line number 5 (replace 5 with the desired line number)
  • 5G - Also goes to line number 5
  • k - Move up one line (5k would jump up 5 lines)
  • j - Move down a line (10j would move down 10 lines)
  • l - Move ahead one character
  • h - Move back one character
  • w - Move ahead a word
  • b - Move back a word
  • Commands to alter/delete the contents of the vim buffer:

  • D (uppercase D) - Delete from the cursor to the end of the line
  • dd - Delete the entire current line (5dd would delete 5 lines)
  • dw - Delete from the cursor to the end of the current word (3dw would delete the next 3 words)
  • x - Delete the character under the cursor (4x would delete the next 4 characters)
  • cw - Change the current word, this will put you in insert mode to change the word, hit ESC to go back to command mode when finished changing (3cw would replace the next three words)
  • r - Replace the current character. You will return to command mode once you finish typing the replacement character.
  • yy - Copy the current line (3yy would copy 3 lines)
  • p - Paste the contents of the copy clipboard after the current line
  • P (uppercase P) - Paste the contents of the copy clipboard BEFORE the current line
  • Other commands to enter insert mode:

  • I (uppercase I) - Put cursor at the beginning of the line when entering
  • a - (append) Put cursor after the current character when entering
  • A (uppercase A) - Put cursor at end of the current line
  • o - Create a new blank line after the current line and enter there
  • O (uppercase O) - Create a new blank line BEFORE the current line and enter there
  • SHELL META CHARACTERS AND WILDCARDS

    The shell attaches special meaning to some characters, such as using & in Lab 2 to start a program in the background. There are several other useful meta characters, including filename wildcards, that can be useful to know.

    Let's say you want to create or edit a file with a space in the name. If you just typed:

    vi cs150 file.txt
    vim would actually open two files, one called cs150 and a second called file.txt, instead of one file with a space in the name. That is because the shell uses the space as a meta character to separate items (tokens) in the command line. To get around this, you must "quote" or "escape" the space character to tell the shell that it is part of the filename instead of the token separator. To "escape" a special character, you put the backslash (\) in front of it. To "quote" a filename, you put single quotes (') or double quotes (") around the filename. So any of the following commands would work to create or edit a file with a space in the filename:
    vi cs150\ file.txt
    vi 'cs150 file.txt'
    vi "cs150 file.txt"
    
    Other useful meta characters are the wildcard characters for filenames. This lets you tell the shell, editor, etc. to use all files whose filename matches a pattern. The two shell wildcards are:

  • * (asterisk) - Match anything.
    Example: ls * would list all files and ls lab* would list all files beginning with the phrase 'lab'.

  • ? (question mark) - Match exactly one character.
    Example: ls lab?.cpp would list lab1.cpp, lab2.cpp, and so on, but not lab10.cpp since there are TWO characters between 'lab' and '.cpp'.
  • USING GREP TO SEARCH FILES FOR PATTERNS

    Grep is a pattern matching utility for character strings in lines of text. You use grep to find patterns in a file or to filter a pattern from the output of other commands. Various switches in grep provide additional options as shown here:

    grep -v hello junk.txt  // display lines in junk.txt that do not contain 'hello'
    grep -i sam junk.txt    // display lines that contain 'sam', ignoring case 
    grep -c hello junk.txt  // count lines that contain hello
    
    You should single quote expressions in grep to prevent the shell from performing unwanted character substitutions. Enclosing characters in single quotes preserves the literal value of each character within the quotes. Examples:
    grep 'time.h' *.cpp      // display lines containing time.h in all .cpp files
    ls -al | grep -i '.txt'  // display files with extension .txt, ignoring case
    ps -ef | grep <username> // filter ps output to display your processes
    

    HOW TO TRANSFER FILES

    The best way to transfer files to and from Odin is to use the SSH file copy utility (scp on Linux and Mac, pscp.exe using the Putty utility suite on Windows). This section will cover the scp command. The scp command is very similar to the cp command, in that you are copying from a source file to a destination file. But scp allows either the source file or the destination file to be on another machine, so you can use it to copy files between two different machines.

    A remote filename is distinguished from a local filename by the present of the colon (:) symbol in the filename. If you forget the colon symbol, then scp will behave exactly like cp, that is it will just copy files on your current machine instead of transferring files. A remote filename has the following pattern:

    <username>@<hostname>:<filename>              // Syntax
    <username>@odin.cs.csub.edu:example_file.txt  // Example from off-campus
    <username>@odin:example_file.txt              // Example from on-campus
    
    If you are uploading a file TO Odin, the remote filename is the destination file. If you are downloading a file FROM Odin, the remote filename is a source file. Examples:
    scp stuff.txt <username>@odin:stuff.txt  // Upload stuff.txt to Odin
    scp <username>@odin:stuff.txt stuff.txt  // Download stuff.txt from Odin
    
    While Odin also supports the FTP protocol, we strongly recommend that you do NOT use FTP as it sends your password in plaintext (un-encrypted). Any eavesdroppers between you and Odin can then intercept your username and password, which would allow them to logon to Odin as you. They could even change your password and lock you out of the account. Use an SCP or SFTP program at home instead. Both SCP and SFTP encrypt your connection so eavesdroppers cannot intercept your login credentials.

    USING TAR AND INFOZIP TO ARCHIVE FILES

    The Unix tar (tape archive) utility can archive files in similar fashion to the Winzip utility under Windows. In fact, Winzip understands tar format and can untar a Unix tar file. As a student, you may be asked to email multiple files to your instructor that have been consolidated into a single tarfile. The steps below show you how to use tar.

    The two versions of tar typically used are:

    tar cvf stuff.tar *  // tar all files in current directory into stuff.tar
    tar cvf stuff.tar .  // tar all files and subdirs in current directory into stuff.tar
    tar xvf stuff.tar    // untar the files in stuff.tar
    
    NOTE: Be very careful about the order of arguments in tar. If you type tar cvf * lab5.tar instead of tar cvf lab5.tar * you will corrupt the first file in your directory. You will then need to fix that file, remove the tarfile and start over again.

    How to Use Gzip to Compress Files:
    Since the tar utility does not compress your files (it archives or packs them together for easy manipulation) a compression utility such as gzip (a GNU product) is often used.

    A typical session might go as follows:

    Pack all files including directories into file.tar

    tar cvf file.tar
    Compress file.tar into file.tar.gz
    gzip file.tar
    Uncompress file.tar.gz into file.tar
    gunzip file.tar.gz

    On Odin and other Linux systems, you can also tell tar itself to invoke gzip while it creates or unpacks files by adding the option z to the command (e.g. cvzf instead of cvf):

    tar cvzf stuff.tar *  // tar all files in current directory into stuff.tar
    tar xvzf stuff.tar    // untar the files in stuff.tar
    

    How to Create a zip File:
    Some instructors prefer a zip file instead of a tarball. Luckily, Odin can also create zip files using a command line utility. The commands to do this are:

    zip myLabs.zip lab*   // Create a zip file containing all lab files
    unzip myLabs.zip      // Extract all the files from a zip file
    
    This zip file should be compatible with Winzip and other zip utilities.

    HOW TO PRINT FILES WITH LPR

    The department classrooms and the walk-in laboratory have laser printers available for class-related printing. You can print directly from Odin to these printers using the lpr command.

    Important: Department Printing Policy
    Only CEE/CS related coursework can be printed on the classroom printers (Sci III 240, 311, 312, 313, 314, and 315). You are NOT allowed to print coursework for other classes, personal documents or screenshots not related to your CEE/CS courses on these printers. If abuse is rampant, the printers will not be stocked with paper or may be turned off entirely.

    The walk-in tutoring center (Sci III 324) has a printer, but it is not always stocked with paper. If paper is provided for this printer, it will be "recycled" paper that has already been printed on one side. You are strongly encouraged to bring your own paper for printing in the walk-in lab.

    The lpr command must be told the name of the printer and the name of the file to work. The syntax for the command is:
    lpr -P<printer> -h <filename> 
    Not all classroom printers can be accessed directly from Odin. The current printer names (case sensitive, so use any uppercase letters given in the printer name) for the classrooms are:

    Classroom Printer
    Sci III 311 lab311
    Sci III 315 lab315bw
    Walk-In Lab (Sci III 324) WalkinLab
    Sci III 240 lab240

    Be patient after using the lpr command as it can take several minutes for the file to be sent to the printers. Do NOT repeat the lpr command if the file does not print. Instead, ask your instructor, a student assistant, a tutor or the department system administrator for help.

    HOW TO FORWARD YOUR EMAIL

    By default, your Odin email is stored on Odin and you must use Alpine (or another CLI mail editor) to access your email messages. If you instead wish to use your Gmail, Yahoo, etc. email account, you have to set up a forwarding rule on Odin to do so.

    Forwarding rules are kept in a special dot (hidden) file in your home directory called the .forward file. To create the .forward file, make sure you are in your home directory (such as doing the cd command) and then give the following command:

     vi .forward 
    Then set vi to paste mode (the vi last-line command :set paste from Lab 2) and paste in the following information:
    # Exim filter - do not edit this line!
    # only works for exim... others will treat it as a plain .forward file...
    
    # if this filter generates an error, then deliver to original address
    if error_message then finish endif
    
    # If you want to forward your email to an offsite address, uncomment and modify
    # the following line to use your own email address:
    #deliver myaddress@mydomain.invalid
    
    finish
    
    You MUST change the line #deliver myaddress@mydomain.invalid to be YOUR email address on Gmail, Yahoo, etc. and to remove the # sign (called "uncommenting" the line). If you do not do this, the forward rule will be invalid and the system administrator will receive numerous errors in the logs about it (which can lead to a grumpy system administrator, and that is never a good thing).

    Download file viewer | CSUB - Computer Science Department