How to ... in your Linux account
I will add addtional topics to this listing in response to questions you may have.
How to...
- Connect to Linux using ssh.
- Directories/files:
- Print a file.
- Secure ftp a file to/from your unix account.
- Compiling.
- Linux online manual.
- Vim editor
- Emacs
-
Connect to your CDM Linux account using secure shell.
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 CDM server address for you Linux account:
cdmlinux.cdm.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@cdmlinux.cdm.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 [top]
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 [top]
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 [top]
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 [top]
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 [top]
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 [top]
Command: mkdir
Arguments: new_subdirectory_name
Options:
Description: Create a subdirectory of the current directory
Example(s):
$ mkdir hw1
Change the current directory [top]
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[top]
There are no accessible printers for the Linux account.
So to print a file, you need to use sftp to transfer the file to your/another computer that can access a printer.
Secure ftp a file from your Linux account.[top]
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 cdmlinux.cdm.depaul.edu 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@cdmlinux.cdm.depaul.edu (enter password if prompted) psftp> Commands: open machine Open a conection to the machine (e.g. for machine use cdmlinux.cdm.depaul.edu) pwd print working directory on cdmlinux lpwd local print current directory on the local machine ls list files in the current working directory on cdmlinux !ls list files in the current directory on the local machine cd dir change working directory to be the subdirectory dir on cdmlinux 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 cdmlinux) bye End the connection and terminate psftp
Compiling
Compile a C program and run it. [top]
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.[top]
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
- prog is the target.
- prog.c is the file on which the target depends.
- gcc prog.c -o prog is the command needed to build the
target if it is 'out of date' with respect to the file(s) it depends
on.
Note: The line for the command must begin with a tab character! Otherwise, the make program will not correctly interpret the makefile.
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'.
Linux online Manual[top]
Browse Linux 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 Linux 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 [top]
Start, Suspend, and Exit emacs [top]
Start emacs:
$ emacs
Suspend emacs. Once emacs is started, you can temporarily suspend emacs and go to the Linux 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 [top]
Abort any command: ctrl-g
Create a new file or find an existing file [top]
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 [top]
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: promptM-x: compile
Find next compile error [top]
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[top]
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[top]
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
- move the cursor to the first character of the region.
- set the mark (ctrl-space)
- move the cursor to the character just after the region.
- kill-region (ctrl-w)
To paste a previously cut (killed) region:
- Move the cursor to the position where the text is to be inserted.
- 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[top]
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.
Linux directories in emacs[top]
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.)
Customize Shell and Emacs[top]
Here are some customizations that can be made to the shell and to emacs:
- Shell - Add the current directory to your PATH environment variable.
-
Emacs - Adds the following command bindings to keys:
- compile: ctrl-c,c
- Manual-entry: ctrl-c,ctrl-m
- goto-line: ctrl-x,ctrl-g
- Scroll up: ctrl-z (scroll-down is already ctrl-v)
- Make current cursor line the top line in the window: ctrl-c,u
To make these customizations, login to your CDM Linux account and type:
~glancast/setuplogin