Basic UNIX Commands



A quick review of UNIX basics

The following is a list of the UNIX basic commands, practice by yourself, and make sure you understand all of them. This list can be roughly divided into the following categories:


pwd - print working directory name
$ pwd
/users1/ehuang/shell_demo


who, whoami - display the current username
$ who
root       pts/3        Feb  2 08:33
ehuang     pts/9        Feb  2 18:22    (acc5)
jerry      pts/8        Feb  2 11:55    (acc8)
root       pts/5        Feb  2 08:33
...        ...

$ whoami
ehuang

hostname - print name of current host system
$ hostname
acc5.its.brooklyn.cuny.edu

date - write and set the date and time
$ date
Mon Feb  2 18:26:21 EST 1998

uname - get name of current UNIX system
$ uname -a
SunOS butler1.its.brooklyn.cuny.edu 5.6 Generic sun4m sparc

ls
ls
with no arguments prints a list of the names of the files in the current working directory. With directory arguments a list of files in each directory is printed. With a file argument that filename is listed. Some options are:
  1. -a : displays all entries including those that begin with a period.
  2. -d : displays directories only.
  3. -F : marks entries in list (directory/ , symbolic link@ , executable_file*)
  4. -l : lists mode, number of links, owner, size(bytes), time of last modification. Special files have size field replaced with major and minor device numbers. (mode first character: d=directory, b=block-type special file, c=character type special file,l=symbolic link,s=socket,-=plain file) (mode permissions:user-group-other)
  5. -r : sorts entries in reverse alphabetic or time order
  6. -i : prints the inode number of each file
  7. -u : sorts by time of last access (read or executed)
  8. -t : sort (Example: Sort by last inode modification time = ls -ctl)
  9. -1 : print in a single column
$ ls
faq.tar   test1     test2

$ ls -l
-rw-------   1 ehuang   adjunct   466944 Jan 31 21:04 faq.tar
-rw-------   1 ehuang   adjunct       20 Feb  2 18:23 test1
-rw-------   1 ehuang   adjunct       20 Feb  2 18:23 test2

$ ls -a
.        ..       faq.tar  test1    test2

$ ls -lt
total 932
-rw-------   1 ehuang   adjunct       20 Feb  2 18:23 test2
-rw-------   1 ehuang   adjunct       20 Feb  2 18:23 test1
-rw-------   1 ehuang   adjunct   466944 Jan 31 21:04 faq.tar

$ ls -lratF
total 936
-rw-------   1 ehuang   adjunct   466944 Jan 31 21:04 faq.tar
drwx------  15 ehuang   adjunct     1024 Feb  2 18:22 ../
-rw-------   1 ehuang   adjunct       20 Feb  2 18:23 test1
-rw-------   1 ehuang   adjunct       20 Feb  2 18:23 test2
drwx--x--x   2 ehuang   adjunct      512 Feb  2 18:23 ./


cp - copy the file or directory
$ cp test1 test1.old


mkdir - make a directory
rm, rmdir - remove files or directories
mv - move files or directories

for rm (similarly for mv):

  1. -i : ask before removing each file listed (interactive mode).
  2. -r : remove listed directories and all their subdirectories
  3. -f : silently remove files for which one does not have write permissions.
$ rm test1.old

$ mkdir tmp

$ ls -F
faq.tar     test1       test1.old   test2       tmp/

$ rmdir tmp

$ mv test1 test1.old


cat, more, less, pg, head, tail - browse or page through a text file
$ cat test2

	"This is file test2

	UNIX shell programming demo      " 

$ more test2

$ less test2

$ pg test1

$ head test1

This is a new file called test1
and more ...

$ tail test1


nl - line numbering filter, add line number to a file
$ nl test2 > test2.nl

$ cat test2.nl
       
     1  This is file test2
       
     2  UNIX shell programming demo      


wc - word, line and byte or character count
$ wc -l test1
 3 lines   "test1"


exit - log off
$ exit


diff, comm - select or reject lines different/common to two sorted files
$ diff test1 test1.old
2,3c2
< This is a new file called test1
< and more ...
---
> This is file test1

$ comm test1 test2
                
This is a new file called test1
        This is file test2
        
        UNIX shell programming demo        
and more ...


man - manual page, very useful !!
$ man ls
Reformatting page.  Wait... done

User Commands                                               ls(1)

NAME
     ls - list contents of directory

SYNOPSIS
     /usr/bin/ls [ -aAbcCdfFgilLmnopqrRstux1 ] [ file...  ]
     /usr/xpg4/bin/ls [ -aAbcCdfFgilLmnopqrRstux1 ] [ file...  ]

DESCRIPTION
     For each file that is a directory, ls lists the contents  of
     the  directory;  for  each file that is an ordinary file, ls
     repeats its name and any other information  requested.   The
     output  is  sorted alphabetically by default.  When no argu-
     ment is  given,  the  current  directory  is  listed.   When
--More--(5%)



$ man man
Reformatting page.  Wait... done

User Commands                                              man(1)

NAME
     man - find and display reference manual pages

SYNOPSIS
     man [ - ] [ -adFlrt ] [ -M path ] [ -T macro-package ]
          [-s section ] name ...
     man [ -M path ] -k keyword ...
     man [ -M path ] -f file ...

DESCRIPTION
-More--(5%)



compress, uncompress, gzip, gunzip - compress/uncompress the files
$ compress faq.tar

$ ls -l
total 424
-rw-------   1 ehuang   adjunct   201547 Jan 31 21:04 faq.tar.Z
-rw-------   1 ehuang   adjunct       46 Feb  2 18:47 test1
-rw-------   1 ehuang   adjunct       20 Feb  2 18:33 test1.old
-rw-------   1 ehuang   adjunct       57 Feb  2 18:35 test2
drwx--x--x   2 ehuang   adjunct      512 Feb  2 18:37 tmp/

$ uncompress faq.tar.Z

$ ls -l
total 936
-rw-------   1 ehuang   adjunct   466944 Jan 31 21:04 faq.tar
-rw-------   1 ehuang   adjunct       46 Feb  2 18:47 test1
-rw-------   1 ehuang   adjunct       20 Feb  2 18:33 test1.old
-rw-------   1 ehuang   adjunct       57 Feb  2 18:35 test2
drwx--x--x   2 ehuang   adjunct      512 Feb  2 18:37 tmp/

$ gzip faq.tar

$ ls -l
total 360
-rw-------   1 ehuang   adjunct   170070 Jan 31 21:04 faq.tar.gz
-rw-------   1 ehuang   adjunct       46 Feb  2 18:47 test1
-rw-------   1 ehuang   adjunct       20 Feb  2 18:33 test1.old
-rw-------   1 ehuang   adjunct       57 Feb  2 18:35 test2
drwx--x--x   2 ehuang   adjunct      512 Feb  2 18:37 tmp/

$ gunzip faq.tar.gz 
( gzip/gunzip is better than compress/uncompress because
 it uses the better algorithm )

ps - report process status
$ ps -al
F   UID   PID  PPID %C PRI NI   SZ  RSS    WCHAN S TT        TIME COMMAND
 8     0 17111 17106  0   0       0    0                   Z  0:00 
 8     0 17112 17106  0   0       0    0                   Z  0:00 
 8     0 17117 17106  0   0       0    0                   Z  0:00 
 8     0 17143 17106  0   0       0    0                   Z  0:00 
 8     0 17170 17169  0   0       0    0                   Z  0:00 
 8     0  7117     1  0  58 20 1432  572 modlinka S console   0:00 -ksh
 8   196  1481  1478  0  48 20 1448  908 autofs_m S pts/7     0:00 -ksh


chmod - change the permission of a file/directory
$ chmod 711 test1

$ chmod a+r test1


du, df - monitor the file system disk space usage
$ du
2       ./tmp
362     .

$ df 
/                  (/dev/dsk/c0t3d0s0 ):   62700 blocks    24923 files
/usr               (/dev/dsk/c0t3d0s6 ):  181724 blocks   206193 files
/opt               (/dev/dsk/c0t3d0s7 ):  177874 blocks   154569 files
/tmp               (swap              ):  485560 blocks     9793 files

lpr, lpq, lprm - printing commands (some systems support lp, lpstat; some systems support both)

$ lpq
hp4si1_2 is ready and printing
Rank    Owner   Job     File(s)                         Total Size
active  jerry   5       asm.h                           1672 bytes

$ lpr test1

$ man lprm

cal - print calendar
$ cal 12 1999
   December 1999
 S  M Tu  W Th  F  S
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

bc - online calculator
 
$ bc
2+5
7
15*19
285
quit

write, talk - comunication tools
$ write [user]
(use with care, do NOT interrupt others who are busy..)

$ talk [user]
(use with care, do NOT interrupt others who are busy working..)


pine - one of the user friendly mail tools
$ pine

An electronic mail program which is fairly easy to use. You can read
about this in the computer lab help desk manual. We will not use too
much time on this. There is also on-line manual available on Web.


vi - very important, make sure you are very familiar with it.
vi
The standard full-screen editor available under Unix.
      Using Command mode
      ZZ        - save file and exit
      :q!       - exit without save
      :w        - write buffer to disk
      h (left)  j (down)  k (up)  l (right)
      ndw       - delete n words (default for n = 1)
      nx        - delete n characters (default for n = 1)
      ndd       - delete n lines (default for n = 1)
      -         - go to start of previous line
      ^         - go to first non-blank character of current line
      O         - go to first column of current line
      $         - go to end of current line
      nG        - go to nth line of file (default for n = 1)
      nyy       - yank and save n lines
      p         - put down lines saved

      Using INSERT MODE
      <esc>       - exit INSERT mode
      i         - enter INSERT mode ; insert to left of cursor
      a         - enter INSERT mode ; insert to right of cursor
      A         - enter INSERT mode ; add at end of current line
      o         - enter INSERT mode ; add a new line after current line
      O         - enter INSERT mode ; add a new line before current line
      /<pat>      - search from cursor down for pattern <pat>
      ?<pat>      - search from cursor up for pattern <pat>
      n         - go to next occurrence of last pattern indicated
      N         - go to previous occurrence of last pattern indicated

stty
sets I/O options on current output terminal. With no arg, reports terminal speed and settings different than defaults. Some options are :
  1. all : print all normally used option settings.
  2. everything : print all information known to stty.
  3. raw : raw mode input ( no input processing (e.g. erase, kill,interrupt ).
  4. -raw : negate the raw mode.
  5. echo : echo back each character typed.
  6. -echo : do not echo characters.
  7. erase c : set erase character to c (default='#').
  8. kill c : set kill character to c (default='@').
  9. intr c : set interrupt character to c (default=ctrl ?).
  10. start c : set start character to c (default=ctrl q).
  11. stop c : set stop character to c (default=ctrl s).

Example: stty erase '^h'
stty kill '^u'
stty intr '^c'

These are the typical settings. Note the differences from the defaults given by simply stty -a

intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = ^@; 
susp = ^Z; stop = ^S; start = ^Q;

ctrl-keys --- the I/O control commands ( use "stty -a" to see the difference between your system and the defaults given below: )
  1. ctrl-w : deletes the current command line word you are typing (word=any sequence of non-blank characters).
  2. ctrl-d : end-of-file character.
  3. ctrl-c : interrupts current foreground process (In mail two ctrl-c characters kill the mail message and save it in dead.letter).
  4. ctrl-s : suspends output being printed to screen.
  5. ctrl-q : resumes printing of suspended output.
  6. ctrl-r : moves to next line and redraws command line.
  7. ctrl-h : erases character just typed on command line.
( Hint: whenever you have any question about UNIX commands, you can always use man "command" to take a look!! )