© 2004 Joseph L. Pearce HomeMailFTP SitePersonalUserUnix Help • Unix Commands • HTML CodesForumsPorts

Quick Reference of Basic Unix Commands



Workstation Info:

Hostname
Uname -a = Displays extended system info
arp = Displays hostname, ip addy, and  MAC addy
domainname  = Displays domain systems is in
prtconf | grep memory = Displays mem on unix system, not linux
User = users logged in

Unresponsive Terminal:

Ctrl Q - Enables screen scroll
"  " S - Disables "  "   "  :
"  " C - Interupts process returns to shell
"  " D - Enters EOF

DOS/UNIX Reference/Comparison Table:

Dir = ls -a = all, -l = long, ld = display detailed information on a directory, but not is contents, -R = recursive,  
ls * = will give a files starting with that character, ls -F dir1 = gives the file option to show info             on the files. Symbols: / Directory: none is ascii: * is exe: @ is symbolic link: | is a named pipe: = is a socket
clear ; date = has seperator so it clears screen first then posts date
ls ? = gives files mactching the wildcard question. Ex: ls dir? will give
all files matching dir and a wild card character but must be 4 characters long.
ls [] = lists files or directories matching the characters in brackets
ls ; = allows the use of multiple commands. Ex: ls ; cd ;

MD = mkdir make directory
CD = pwd print working directory
CD = cd change directory
RD = rmdir (only for empty directories) or rm -r (remove directory and all underlying directories).
DEL = rm remove files rm -i(interactive) warns user
COPY = cp copy files, -i(interactive), -r(recursive)
REN = mv move or rename files(no move in dos)
VER = uname -rs displays os and release number
SORT = sort sorts files sort [options] [input_filename] +n, -n, -r(reverse the order), -f(ignore case                               of sort)-d(dictionary order), -o (all output to filename listed)
MORE = more displays file contents one screen at a time
CLS = clear
EDIT = vi or pico
PRINT = lp
HELP = man


Unix command examples:

cal = Current calendar
cal 1987 = Calender for that year
date = Current date
date - u = Universal time
banner "hi there" = Scrolling banner that says hi there

Control Characters

Ctrl-s Stops screen output
 "  -q Resumes "  "  " "
ctrl-c Interupts current activity
 "" -d Indicates EOF or exit
 "" -u Erases the command line
 "" -w Erase the last word on the line
 "" -h Erase the last character on the command line

 :KeyBoard Scroll Commands

  Spacebar = Scroll to next screen
  Return = Scroll one line at a time
  b = Move back one screen
  f = Move forward one screen
  h = Display a help menu of more features
  q = Quit and retrun to the shell prompt
  /string = Search forward for a string
  N = Find next concurence of a string

File Management Commands

File = Shows file type
Strings= Used to determine what program created a file
Cat = Displays contents fo a text file on the screen
head = First -n number of lines in a file Ex: head -10 dande
Tail = Last  ""  "  "  "    "    "   "  "  "  Last -10 """"
       (Note) If the +n option is used you can start displaying lines at a specific point. This is used with the         tail command
wc = Word count options = -l counts lines, -w counts words, -c counts bytes, -m counts characters
diff = Compares two text files, then finds and displays differences between them. Options: -i = ignore case, -c =         detailed comparison
cmp = Quick compare. Displays no output if the files are identical, or it provides the character position and line        number of the first occurrence of a difference

File Naming Conventions


Maximum length: The maximum length of files and directories combined is 255 alphanumeric characters.
Nonalphanumeric characters and allowable alphanumeric characters: Underscores (_), hyphens (-), and periods (.) can  be used multiple times in a file or directory name. An example is: Feb.Reports.Sales. Although the shell will allow  asterisks (*), ampersands (&), pipes (|), quotes (" "), dollar signs ($), and spaces to be used in a filename, it is  not recommended. Those characters have special meaning to the shell. When naming files and directories, it is   recommended that choice of characters be limited to letters, numbers, dots, underscores and hyphens (-).

Filename extensions: Filenames can contain one or more extensions. Extensions usually are appended to files by an  application. Extensions are usually one to three characters that are appended to the end of a filename and are   preceded by a period (.). The student might choose to use this convention when naming files, but it is not a   necessary part of a filename.

Directory name extensions: Directory names generally do not contain extensions, but there are no rules against it.

Note: To help distinguish between files and directories, some UNIX administrators prefer their users to start   filenames with lowercase letters and directory names with capital letters. They also might recommend including an  uppercase letter D_ in the first position or including .dir on the end of directory names to distinguish them from  filenames. If no convention is used to distinguish files from directories, the student can always use the ls -F  command line to visibly show which is which. The ls -l and ls -al command, which shows a long listing, also indicates  files and directories by the first character of the display.

Creating Files and Directories

touch [filename] or multiple filenames = Used to create files, multiples files, or update date time archive bit

mkdir [dir name] = Used to make directories. User must have correct permissions for this.
ln = Link, used to make links (known as shortcuts to Windows users)

I/O Redirection

Several metacharacters are used as redirection symbols. Input redirection uses the left angle bracket (< or 0<),  which is referred to as the less than symbol. Output redirection uses the right angle bracket (> or 1>), or the  greater than symbol. Error output redirection uses the right angle bracket, preceded by the number 2 (2>). The  general format for using redirection is shown next. The File after the redirection symbol can be a text file or a  device file. All devices in UNIX are defined by filenames. An example for the screen or monitor device file is  /dev/console. It is not necessary to specify this file because it is the default.

$Command Redirection-Symbol File (text file or device file)

Note: Spaces between the command and a redirect symbol are optional.

Redirecting Standard Input

Standard input is not often redirected. However, it is possible to substitute a text file instead of the normal  keyboard input. In Figure , notice that the left angle bracket, or less than symbol is used. This allows the command  to receive input no matter what file is present. The default input is the keyboard device.

Figure  illustrates redirection of input using the mailx command. The dante file is sent to the e-mail address using  the mailx program. The mailx command invokes the standard command line mail program. Input redirection is used with  more advanced UNIX commands and functions not covered in this course.

command < file

This command uses the file as input to the command.

Redirecting Standard Output

Standard output is redirected more frequently than standard input or standard error. Many commands, such as ls, cat,  head, and tail, generate standard output to the screen. It is desirable to redirect this output to a file for future  viewing or manipulation.

By substituting a filename, the user can capture the output of a command rather than letting it go to the default  monitor. As an example, if the student wanted to get a listing of all files in a directory, they could redirect the  output from the ls command. This would capture it in a file that then could be edited using a word processor. The  output from commands shown in this course was captured in this way.

Creating a New Output File

The right angle bracket (>) allows the command to send output to a file. Using the single right angle bracket creates  a new file if the filename specified does not exist. If the filename exists, it will be overwritten.

command > file

This command creates a new file or overwrites an existing one.

Tip: To prevent the accidental overwriting of a file during redirection, the student can use the noclobber shell  feature. To enable noclobber in the Korn or Bash shell, enter at the command line set -o noclobber. To disable   clobbering, use set +o noclobber. The student can override the noclobber feature by using the >|, greater than symbol  followed by the pipe symbol. For example, ls -l >| myfiles would overwrite the existing myfiles file as if noclobber  was disabled.

The first example in Figure  illustrates redirection of the output of the ls command. The listing of the contents of  the /etc directory would be captured in a file called etc.list. This would be placed in the current directory. The  second example captures the calendar for October 2000 to a file named mon2000. The third example captures the   calendar for January 2001 and appends it to the October calendar in the mon00 file.

The student can also use the > redirect symbol with the cat command to combine multiple files into one. For example,  $cat file1 file2 file3 > file4 would combine file1, file2, and file3 into one new file named file4.

The double right angle brackets (>>) can be used if the user wants to append, add to the end, to an existing file  instead of overwriting it:

command >> file

This command creates a new file or appends to an existing file.

Redirecting Standard Error

Standard error is redirected when the user wants to capture the output of a command that might generate an error. It  is useful when developing script files, and is normally used by system administrators and programmers. Standard  output usually is redirected to a text file. Standard errors are redirected to an error file to assist in  troubleshooting if the script or program does not execute properly. To redirect standard error, use the right angle  bracket with the number 2 in front of it (2>). If a command executes with no problems, it will not generate an error.

command 2> file

This creates new file or overwrites an existing one.

Command piping

command | command

File system statistics

df = Disk free, displays disk information, -k displays it in kilobytes
du = Disk usage, displays disk usage information, -k displays in kilobytes

File searching

find = finds files based on specific criteria. Format can be "find path expression action"
grep = (global regular expression print) used to search for a string in a file or in the output of another UNIX         command. Format can be "grep [option(s)] string filename"
egrep = extended grep uses metacharacters
fgrep = fast grep uses no metacharacters

File Editing

sed = stream editor, reads lines of a text file, one by one. -n option tells sed that the user does not want to print  any lines unless directly told to do so

VI

Command Mode to Entry - i : input, o : open new file, a : append to existing line
Entry to Command - Press Esc
Command to lastline - Enter a Colon (:)
Last line to Command - Press enter
Entry to Lastline -Press Esc to return to Command mode, then enter a colon
Last line to entry - Press enter or ESC to return to command mode then enter an  insert command
 
 
 Command Ex: vi [option(s)] [filename] Use vi or vedit  (This is only available on Solaris

Input commands:

a - append text after cursor, A -  append text at the end of a  line, i - inserttext before cursor,o -  open a new  line below cursor, :w - write  buffer, :w new_filename - write buffer to new_filename, wq - write buffer (save  changes) and  quit vi, ZZ(upper case) save changes and quit vi, :q! quite without saving changes (also known as  quit damnit), :wq! - write buffer (save changes), quit vi(the ! will over ride any read only changes if you are the  owner of the file)

Positioning commands(command mode):

j(or down arrow)- Move down one line, K(or up arrow)- move up one line, Space bar-
move right(forward) one character, w - move forward one word including puncuation, b - move back one character  including punctuation, $ - Move to end of line, 0(zero) or ^ - Move down to the beginning of next line, Return - Move  down to the beginning of next line, h(or left arrow) - Move back one character, l(or right arrow) Move forward one  character For the other commands:The man pages for vi may or may not contain any descriptions of commands such as :q,  :w, :[line #], or :set depending on the UNIX version. The user may need to read the ex man pages (man ex) to learn  more about the last line mode commands. With Red Hat Linux, entering the command man vi or man ex will take the  user to the vim man pages.

Using Emacs
 
The most important first steps in learning any editing tool are:
Starting up
Opening a file for edit
Inserting and changing text
Undoing mistakes
Moving around
Starting again
Saving work
Exiting
Exiting without saving
The steps for performing these operations are identical in GNU Emacs and XEmacs. Everything may be accomplished through the menus. The student will learn how to perform these operations just by exploring the menus. We will demonstrate here how to perform the operations in the traditional Emacs way, using keyboard commands. This operation, once learned, is much faster.

The student will need to understand a little about Emacs key-binding notation in order to use the keyboard commands:

Keys Meaning
C- Hold down the CONTROL key while typing the key. For instance, C-a means Control+A.
C-x
C-f  
C-x s Some control keys, such as Control+X, are prefixes to be followed by one or more other keys. This can be another control character or a plain character.
M- Hold the META key while typing . If there is no META key, or the student does not know where it is located, press ESC (Escape) followed by the key.
M-x This is an important key that prompts the student for a command to execute by name. This is useful when the student does not remember the key combination.

Starting Emacs

Start Emacs either by simply launching the program, or with one or more file name arguments:

emacs

emacs myfile

Note: emacs starts GNU Emacs, which we use in this explanation. XEmacs is started with the command xemacs.

Aborting Keyboard Entry

The single most important key-binding combination in Emacs is

C-g

C-g runs a command called keyboard quit. This enables the student to back out of any operation when it becomes difficult or complicated for the user. If in a situation that causes trouble, C-g will almost always get the user out of it.

Opening a File

To open a file for edit, press the combination

C-x C-f

A prompt at the bottom of the screen asks for the name of the file. Respond with:

junkfile

or the student may choose any name. The file does not have to exist.

In Emacs the user is always in insert mode. This is unlike vi, where the user toggles between insert and command modes. Type in any text the student would like to use.

Undoing Mistakes

To undo a mistake, press

C-/

Repeat C-/ to undo more changes.

Cursor Movement

To find a reference for all cursor-motion command, go to http://www.gnu.org/manual/emacs/ html_mono/emacs.html#IDX66 .

Movement of the cursor can be accomplished with these keys:

C-a beginning of line

C-e end of line

C-n next line

C-p previous line

C-f forward one character

C-b backward one character

Restarting Editing

To restart editing the file from the last time the student saved it, press

C-x C-v

Enter the same filename that is being edited in response to the prompt. Then the student will be asked for verification and will reload if the answer is yes.

Killing Editing

To stop or kill the edit, leaving Emacs running, press

C-x C-k

It will prompt for the file buffer to kill, with the current file as default. Press Return. If Emacs knows the user has made changes since the last save, it will prompt for verification.

Saving Work

To save the current file at any time, type

C-x C-s

The student may still type C-x u (undo) anytime. Emacs will continue to undo until it runs out of things that it remembers to undo.

Exiting Emacs

To exit Emacs smoothly, type

C-x C-c

If Emacs sees there are unsaved file buffers, it will prompt the user whether to save each one in return.

Aborting Emacs

If the operation gets difficult and the student wants to abort, type

M-x kill-emacs

After typing M-x, the cursor occurs in what is called the echo area at the bottom of the window. Type kill-emacs there. Emacs will quit unconditionally, without prompting the student about saving work and so on. The figure shows an XEmacs session about to be killed.

To access the TUTORIAL at any time, press

C-h t

Help is available in Emacs at every keystroke. The prefix key reserved for Help, logically, is:  
C-h

Type just C-hand the program will prompt the student for further options. To see the options available, type:

C-h ?

The chmod Command

The chmod command is used by a file's owner, or superuser, to change file permissions. The two modes of operation  with the chmod command are symbolic (or relative) and octal (or absolute):

Symbolic Mode uses combinations of letters and symbols to add or remove permissions for selected categories of users.  Symbolic mode also is referred to as relative mode.
Octal Mode uses octal numbers to represent file permissions and is used to set permissions for all three categories  of users. Octal mode also is referred to as absolute or numeric mode.

Ex: chmod mode filename

The mode portion of the command format is made up of three parts

Who , is the category of users the user is working with (u, g, o, or a).  
Op , is the operator or what the user is going to do. For example: set (=), remove (-), or give (+).  
Permissions are permissions or permission assigned to the file or directory. For example: read (r), write (w), or  execute (x).  

chmod octal_mode filename

or

chmod -R octal_mode directoryname

Use the umask command to display and set the umask:

umask (displays current umask)

umask umask _value (sets umask)

Tip: To return the umask to the original value, either use the umask command or log out and log back in again. The  default umask on UNIX systems is 022. Changes to the umask are lost when the user logs out of the system unless it is  placed in the login initialization file.

chown command

When a file or directory is created, the owner or user is automatically assigned. In Linux, only the superuser or  root can change ownership. In Linux, using the chown (change owner) command transfers ownership from one user to  another. In Solaris both the current file owner and root can use the chown command.
When the ownership is changed for a file, the user is no longer the owner. The user may or may not be able to access  the file even though it is in the user's directory. Because the user is no longer the owner, group or other  permissions will determine the access. The following shows the syntax for the chown command for a file and then for a  directory:

chown new_owner filename

or

chown -R new_owner directoryname

Like the chmod command, the -R option recursively changes ownership on directories and all files. This is a quick way  to change ownership on all files in a directory at once.

Changing Group Ownership

When a file or directory is created, the user's primary group automatically is assigned. In Linux, only the superuser (root) can change group ownership using the chgrp (change group) command to transfer group ownership from one group to another. In Solaris both the current file owner and root can use the chgrp command. The format for the chgrp command is shown here:

chgrp new_group filename

or

chgrp -R new_group directoryname

Like the chmod command, the -R option recursively changes group ownership on directories and all files. This is a quick way to change the group ownership on all files in a directory at once.

The ps command

The ps (process status) command is used to list the processes currently running on the system.

ps [-options ]

ps -e: The ps command with the -e (every) option displays PID, TTY, TIME, and CMD for all processes running on the  system.  

ps -f: The ps command with the -f (full) option shows a listing of all information: UID, PID, PPID, C, STIME, TTY,  TIME, and CMD. However, only for those processes running under the current shell or Terminal window.

ps -u: The ps command with the -u (user ID) option displays PID, TTY TIME, and CMD for every process running on the  system for a particular user.

ps --forest Shows hierarchial struture of parent/child processes.

Signals and the kill command

$man -s 3C signal or man signal Shows signal listing

kill [-signal ] process-id or %job-id

killall [-i] CMD name



© 2004 Joseph L. Pearce HomeMailFTP SitePersonalUserUnix Help • Unix Commands • HTML CodesForumsPorts