How to ... in your Unix account

I will add addtional topics to this listing in response to questions you may have.


How to...

  1. Connect to Linux using ssh.
  2. Directories/files:
  3. Print a file.
  4. Secure ftp a file to/from your unix account.
  5. Compiling.
  6. Unix online manual.
    • Browse the unix manual - man - pages.
    • Print a man page.
  7. Vim editor
  8. Emacs
    1. Start and Exit emacs
    2. Abort any emacs command
    3. Create a new file or find an existing one.
    4. Compile a program in emacs
    5. Find compiler syntax errors in emacs
    6. Cursor movement
    7. Cut and paste
    8. Managing emacs Windows
    9. Unix directories in emacs
    10. Using gdb in emacs

Connect to your CDM Linux account using secure shell.

First, you must activate your ctilabs account(s) at:


  https://accountactivate.cdm.depaul.edu/


Activating your account will take at least 10-15 minutes during regular hours if you are already registered for a cti class. Otherwise, there may be a delay of up to one day after you have registered. You will need your campus connection password and id, but you must register at the beginning of every quarter to be able to access cti lab accounts.

Next, make sure you have a connection to the internet; e.g., dial in to your ISP if necessary.

Run the secure shell application. See the link below for PuTTY, a nice ssh application. There are other secure shell applications. In what follows, I'll just refer to it as 'ssh'.

You will need to know the CTI server address for you Linux account. There are 2 addresses, either of which will connect you to your account:

ctilinux1.cstcis.cti.depaul.edu
ctilinux2.cstcis.cti.depaul.edu

You will get a command window and after ssh connects to the appropriate Linux server, with the prompt:


login as: your_login

your_login@ctilinux1.cstcis.cti.depaul.edu's password:

Enter your CDM (campus connection) login name and password.

To log out type "ctrl-d".

(Currently you must use a secure shell, so telnet will not work. There are free sources as well as trial versions of secure shell. In particular: PuTTY is free, small, and convenient to use:)


PuTTY - a Free Windows Telnet/SSH Client

If this doesn't fit your needs see also www.ssh.com (commercial and free) or www.openssh.com (free with links for unix, mac and windows). Note that the lab computers have a secure shell client that you can use.


Directories/Files

List directories [back]

Command: ls

Arguments: Optional list of files and or directory names

Options: -l -F

Description: Lists the specified files and or directories. For a directory, all its files and directories are listed. If no file or directory is specified, the current directory is assumed.

Example(s):

$:glancast> ls
prog1            prog1_ver0.c   prog2            sample.c
tmp 
$:glancast> 

$:glancast> ls -lF
total 28
drwxr-xr-x   2 glancast cscfclt      512 May 16 11:38 prog1/
-rw-r--r--   1 glancast cscfclt     8177 Jun 20 15:39 prog1_ver0.c
drwx------   2 glancast cscfclt      512 Jul 20  2000 prog2/
-rw-r--r--   1 glancast cscfclt     2224 Jun 20 15:39 sample.c
drwx------   2 glancast cscfclt      512 Sep  6  2000 tmp/
$:glancast> 

Display current directory name [back]

Command: pwd

Arguments: none

Options: none

Description: Print Working Directory; i.e., display the name of the current directory.

Example(s):

$ pwd
/home/CSTCIS/glancast/373class
$ 

Delete Files/Directories [back]

Command: rm

Arguments: file list

Options: -i -f -r

Description: Removes (deletes) the specified files

-i   Ask before removing each file
-f   Remove files even if write permission is not set
     without prompting.
-r   Recursive remove directories and subdirectories in
     the list. Files will be removed in these directories
     first and then the directory itself.

Example(s):

$ rm prog1.c
$ rm -i *.c
$ rm -fr prog2.c hw1_dir

Rename a file/directory [back]

Command: mv

Arguments: existing_file new_file

Options: -i

Description: Renames existing_file to have the name new_file -i Prompts if mv would overwrite an existing file.

Example(s):

$ mv prog1.cc prog1.c

Copy a file [back]

Command: cp

Arguments: existing_file new_copy

Options: -i

Description: Copy existing_file to the file new_copy. If new_copy doesn't exist, create it. If it does exist, first delete its contents.

If new_copy is a directory, a copy of existing_file is created in directory new_copy with the same file name - existing_file.

-i   Prompt if the new_copy file already exists, before
     overwriting it.

Example(s):

$ cp sample.c prog1.c

$ cp ~glancast/373class/dot_emacs.zip .

Create a directory [back]

Command: mkdir

Arguments: new_subdirectory_name

Options:

Description: Create a subdirectory of the current directory

Example(s):

$ mkdir hw1

Change the current directory [back]

Command: cd

Arguments: target_directory

Options:

Description: Change the current directory (working directory) to the specified target_directory. If no target_directory is specified, change to the login directory.

Example(s):

$ cd hw1

$ cd ..

$ cd

Print a File[back]

There are no accessible printers for the Unix account.

So to print a file, you need to use ftp to transfer the file to a PC. You can then print the file as you would any other file on the PC.


Secure ftp a file from your Unix account.[back]

You must use a secure ftp client (sftp) to transfer files to/from your Linux account.

Since email is not enabled on the accounts, sftp is the reliable way to transfer files to a lab or home machine from your Linux account.

The terminal program Putty that implements a secure shell also comes with a secure ftp client, psftp.

Use ctilinux1.cstcis.cti.depaul.edu (or ctilinux2 through ctilinux7 instead of ctilinux1) as the target from your home or lab machine.

Commands to transfer files are put and get. The help command lists all the psftp commands.

psftp> open your_login@ctilinux1.cstcis.cti.depaul.edu
(enter password if prompted)

psftp> 

Commands:

open machine Open a conection to the machine
          (e.g. for machine use ctilinux1.cstcis.cti.depaul.edu) 

pwd               print working directory on ctilinux1
lpwd              local print current directory on the local machine
ls                list files in the current working directory on ctilinux1
!ls               list files in the current directory on the local machine
cd dir            change working directory to be the subdirectory
                  dir on ctilinux1 
lcd ldir          change the local current directory
                  to be the subdirectory ldir on the local
                  machine.
put file          (where file is in the local current directory) 
get remote_file   (where remote_file is on the remote
                  machine ctilinux1)
bye               End the connection and terminate psftp

Compiling

Compile a C program and run it. [back]

To compile a single file C program in a file named prog.c use the gcc compiler.

$ gcc prog.c -o prog

This compiles the program in prog.c, links it with any standard c++ library routines called (e.g. i/o routines) and produces the executable file named prog.

Now run prog:

$ prog

or

$ ./prog 

Note: . is an alias for the current directory. 
So ./prog means run the prog executable file that is in
the current directory. Linux shell will only search the current
PATH for programs. If the current directory alias, ., is
not one of the directories in the string value of PATH, the
first version will not work since Linux will not be able to find prog.

Simple make files.[back]

Instead of typing the command to execute the gcc compiler as:

 gcc prog.c -o prog

You can simply type:

make prog

The make program is a utility which can build executable files from source files, but it generally has to be told what to build, what the executable depends on, and how to build the executable from these dependencies. (The make program can make certain assumptions if you don't specify all of these.)

Here is a simple make file for the compilation above in which you specify the target, dependencies, and the rules for building the targets. The make utility will automatically look for a file named 'makefile' first (then for 'Makefile').

Here is a sample file (it would be named 'makefile'):

prog: prog.c
        gcc prog.c -o prog

If this file, 'makefile', has been created, to compile or recompile, you can simply type:

make

For simple cases like this where there is only one target that depends on only one file, the default rules that make uses are sufficient and no actual 'makefile' is necessary. However, in this case you still have to tell make the name of the target:

make prog
In the case the make utility doesn't find either 'makefile' or 'Makefile', it will look for a source file named prog.c (or prog.xxx for other file extents make knows about). If make finds prog.c, it will use it as the file on which the target depends and make knows a default rule to compile 'prog.c' with executable output named 'prog'.

Unix online Manual[back]

Browse Unix man pages

To view the unix online manual, you need to know the exact name of a manual topic; e.g., fork. In some cases you may not know the exact name. You can try the man command with the -k option (for keyword)

$ man -k fork
/usr/local/graphics/man/windex: No such file or directory
/usr/local/teTeX/man/windex: No such file or directory
/condor/cscfclt/glancast/local/man/windex: No such file or directory
fork		fork (2)	- create a new process
fork1		fork (2)	- create a new process
pthread_atfork	pthread_atfork (3t) - register fork handlers
vfork		vfork (2)	- spawn	new process in a virtual memory	efficient way
fork		fork (2)	- create a new process
fork1		fork (2)	- create a new process
pthread_atfork	pthread_atfork (3t) - register fork handlers
vfork		vfork (2)	- spawn	new process in a virtual memory	efficient way

The output shows all manual topic names in the first column related to the keyword entered (fork).

Display a manual page

Once you know the name of the manual page for a topic use the man command again with that specific topic:

$ man fork
System Calls						  fork(2)



NAME
     fork, fork1 - create a new	process

SYNOPSIS
     #include <sys/types.h>
     #include <unistd.h>

     pid_t fork(void);

     pid_t fork1(void);

DESCRIPTION
     The fork()	and fork1() functions create a new process.   The
     new  process (child process) is an	exact copy of the calling
     process (parent process).	The child  process  inherits  the
     following attributes from the parent process:
 
     .... (continues)

Saving/Printing a Unix manual page to a file

To put the same manual page information in a (new) file in your directory named fork.txt:

$ man fork | col -b > fork.txt

Once you have this file, you can transfer it to a PC using ftp and print it just like any other file.


Vim

For the moment see this file vim_commands.html. I'll reorganize this by editing tasks soon.


Emacs [back]

Start, Suspend, and Exit emacs [back]

Start emacs:

$ emacs

Suspend emacs. Once emacs is started, you can temporarily suspend emacs and go to the Unix prompt. Then return to the emacs session where you left off.

Suspend emacs:  ctrl-x ctrl-z
Resume emacs:   $ fg
Exit emacs: ctrl-x ctrl-c

Abort any emacs command [back]

Abort any command: ctrl-g

Create a new file or find an existing file [back]

Before any editing you need to create a new file or find an existing file with the find-file command:

find-file:  ctrl-x ctrl-f

This opens the file in the current emacs window (the one with the cursor) and associates a buffer with it. If there was a file open in the window already, the file is still associated with its emacs buffer, but is not displayed in an emacs window.


Compile a program in emacs [back]

Emacs has named commands. Many commands are bound to keys. For example, the command find-file is bound to the key combination ctrl-x ctrl-f.

Whether a command is bound to a key or not, you can execute it by "name" by typing M-x (Alt key + the 'x' or Esc key, then x). You are then prompted in the mini-prompt line at the bottom of the emacs window to enter a "named" command.

A useful named command is "compile".
Enter this name after typing the M-x combination at the M-x: prompt

M-x: compile

Find next compile error [back]

next-error:  ctrl-x `   (That's the ctrl-x and then the
                         back-quote key, which is located 
	                 just to the left of the number 1 key.)

Cursor movement[back]

previous-line:	ctrl-p or up-arrow 
next-line: ctrl-n or down-arrow
backward-char: ctrl-b or left-arrow
forward-char: ctrl-f or right-arrow
scroll-up: ctrl-v  (scroll the text up nearly a full screen)
scroll-down: ctrl-z (scroll text down nearly a full screen)
forward-word: Esc-f  (Escape key + f)
backward-word: Esc-b
beginning-of-line: ctrl-a
end-of-line: ctrl-e

Cut and paste[back]

The cut (kill-region) command works on the region between the point and the mark. The point is the location of the cursor. You set the location of a mark with the set-mark-command, which sets the mark to be the current location of the cursor (the current point).

So to cut a region of text

  1. move the cursor to the first character of the region.
  2. set the mark (ctrl-space)
  3. move the cursor to the character just after the region.
  4. kill-region (ctrl-w)

To paste a previously cut (killed) region:

  1. Move the cursor to the position where the text is to be inserted.
  2. ctrl-y ("yank" - insert the last killed text)

Summary of Cut/Paste Related keys

set-mark-command: ctrl-space
cut (kill-region): ctrl-w
paste (yank): ctrl-y
exchange-point-and-mark: ctrl-x ctrl-x

Since the region between the mark and the point is not highlighted, the exchange-point-and-mark command is useful to check where the mark (and point) are currently located since they determine the region that will be acted upon by a cut operation.


Managing emacs Windows[back]

Commands to split/delete/navigate emacs windows

Command Name Key(s) Description
split-window-horizontally ctrl-x 2 Split the current window (containing the cursor) into two horizontal windows, each visiting the same file.
split-window-vertically ctrl-x 3 Split the current window (containing the cursor) into two windows, each visiting the same file.
other-window ctrl-x o Move the cursor to the next window. Does nothing if there is only one window.
delete-window ctrl-x 0 Delete the window (but not the buffer or the file it holds) containing the cursor.
delete-other-windows ctrl-x 1 Delete all other windows (but not the buffers or the files they are visiting) except the window containing the cursor.

Unix directories in emacs[back]

Command Name Key(s) Description
dired ctrl-x d Displays a list of files in selected directory
dired-find-file f With a directory listed in dired mode, opens the file or displays the contents of the directory at the cursor position in the current window.
dired-previous-line p Move cursor to previous directory entry
dired-next-line n Move cursor to next directory entry
dired-flag-file-deletion d In dired, mark a file entry for deletion
dired-unmark u In dired, unmark a file entry
dired-do-flag--delete x In dired, delete the files flagged for deletion.
dired-do-rename R In dired, Rename the file entry at cursor position. This can be renamed to a different directory, thus moving the file.
dired-do-copy C In dired, copy the file entry at cursor position. This can be copied to a different name in the current directory or to the same or different name in a different directory.
shell-command M-! In dired, prompts for a shell command; e.g. "man fork | col -b". (Note the col -b command gets rid of unwanted backspace characters, ^H, normally produced by man for the screen display emphasis.)

Using gdb in emacs [back]

The gdb debugger can be run from the command line.

However, you need to see the code as well as enter gdb commands for stepping through execution and examining data.

With emacs, you can run gdb in one window and have the program code in another window (vertical windows or horizontal as you choose).

  1. Type the M-x combination (Alt+x or Esc, then x) to enter a named command. At the M-x: prompt in the mini-prompt window at the bottom of the emacs window type gdb:

    	M-x: gdb   
          

  2. Then the mini-prompt changes to:

    	Run gdb (like this): gdb 
          

    After the second gdb, type the name of the executable file you wish to debug. E.g.,

    	    Run gdb (like this): gdb bomb
    	  

  3. An interactive emacs window then opens with gdb running on your chosen file.

  4. Split the window and in one of the windows use ctrl-x ctrl-f (find-file) to open the text file with the C source code (or the assembler code) for your executable - e.g. bomb.s.

                ctrl-x 2        splits the window into two horizontal windows,
                                each showing the same interactive gdb session
    
                ctrl-x ctrl-f   find-file (prompt for file name appears in
                                mini-prompt window at bottom of emacs window)
    
                Find file: enter file name (e.g., bomb.s)
    	  

  5. After the find-file executes, one of the the gdb windows is replaced by the file (e.g. bomb.s) and you now have the program code (bomb.s - a text file) in one window and an interactive gdb session in the other window!

Getting the History of gdb Commands in emacs

The arrow keys don't work for history in emacs, since they are already used to move the cursor around in a file. However, you can use different key combination to go back (and forward) in the history of gdb commands you have entered.

This can save you from having to retype 8 hex digit addresses, and other commands you have already entered.

If you have entered several gdb commands in an emacs window, you can retrieve them one at a time at the gdb prompt by:

	Alt p        go back one gdb command (you can repeat this)
        Alt n        go forward in the history of gdb commands you have typed
      

When a gdb command is retrieved, you can use use arrow keys to move and backspace or delete keys to edit the command.

If you have retrieved a command and possibly edited it, the enter key will execute the command.