Lecture 19 - UNIX 3


Home


Objectives

Contents


The upcoming tutorial is in the Unix Lab B1.11

Example: Directory tree (partial) used in examples

           /
           |
 +----+----+--------+
 |    |    |        |
bin   dev etc    Users
           |        |
           +        +----------------------------------+
           |        |                                  |
         passwd  tjwhitfort                         wkvarcoe
                    |                                  |
   +-------+--------+--+----------+                    +-------+
   |       |           |          |                    |       |
.login  ProgEnv  geelong.txt  public_html           ProgEnv  x.txt 
           |                      |                    |
    +------+-----+                +----------+         +
    |            |                |          |         |
 Hello.java  assign       default.gif  index.html  Tut1.java
                 | 
          +------+-------+
          |              |
      Assign1.class  Assign1.java

Working with Directories

 

Special directories are:

pwd

present working directory. Display the full path of the present (current) working directory

Format

pwd

Example

pwd
/Users/tjwhitfort

cd

change directory. Change the present working directory.This affects commands that use relative addresses/paths, as the addresses are relative to the present working directory.

Format

cd [directory]

Examples (absolute references)

cd /       (Changes directory to the root directory)
cd /etc    (Changes to the etc directory /etc)
cd /Users  (Changes to the Users directory /Users)
cd /Users/tjwhitfort  (Changes to the directory /Users/tjwhitfort)
cd /Users/tjwhitfort/ProgEnv  (Changes to the directory /Users/tjwhitfort/ProgEnv)
cd /Users/wkvarcoe    (Changes to the directory /Users/wkvarcoe)

Examples (relative references, present working directory is /Users/tjwhitfort)

cd ProgEnv         (down one directory level to /Users/tjwhitfort/ProgEnv)
cd ProgEnv/assign  (down two directory levels to /Users/tjwhitfort/ProgEnv/assign)
cd ./ProgEnv       (the same as cd ProgEnv above)
cd ..              (up one directory level to /Users)
cd ../wkvarcoe     (up one then down one directory level to /Users/wkvarcoe)
cd ../../etc       (up two directory levels then down to etc to /etc)

Examples (present working directory is /etc, home directory is /Users/tjwhitfort)

cd                   (Changes to the users home directory e.g. /Users/tjwhitfort)
cd ~                 (Change directory to /Users/tjwhitfort)
cd ~/ProgEnv         (Change directory to /Users/tjwhitfort/ProgEnv)
cd ~/ProgEnv/assign  (Change directory to /Users/tjwhitfort/ProgEnv/assign)
cd ~/..              (Change directory to /Users)

mkdir

make directory. Creates a new directory.

Format

mkdir directory

Examples (for relative references, present working directory is /Users/tjwhitfort)

mkdir ITFun          (create subdirectory called ITFun: /Users/tjwhitfort/ITFun)
mkdir ProgEnv/tutes  (create subdirectory called tutes: /Users/tjwhitfort/ProgEnv/tutes)
mkdir /Users/tjwhitfort/photos  (create subdirectory called photos: /Users/tjwhitfort/photos)

Example (for relative references, present working directory is /Users/tjwhitfort/public_html)

mkdir test  (create subdirectory called test: /Users/tjwhitfort/public_html/test)

rmdir

remove directory. Removes (deletes) a directory. If the directory is not empty then the command fails. Also see rm.

Format

rmdir directory

Examples (for relative references, present working directory is /Users/tjwhitfort)

rmdir ITFun          (remove subdirectory called ITFun: /Users/tjwhitfort/ITFun)
rmdir ProgEnv/tutes  (remove subdirectory called tutes: /Users/tjwhitfort/ProgEnv/tutes)
rmdir /Users/tjwhitfort/photos  (remove subdirectory called photos: /Users/tjwhitfort/photos)

File Maintenance

Commonly used  file maintenance commands are cp (copy), mv (move/rename) and rm (remove/delete).

cp

copy. Copies a file or files.

Format

cp [-r] file1 target-file

Warning: if the target file is an existing file it will be overwritten.

Options

-r     recursive. Copies the files specified, including those in subdirectories.

Examples (for relative references, pwd is /Users/tjwhitfort)

cp Test.java Test2.txt  (copies /Users/tjwhitfortTest.java to /Users/tjwhitfortTest2.java)
cp Test.java ProgEnv    (copies /Users/tjwhitfort/Test.java to 
                         /Users/tjwhitfort/ProgEnv/Test.java)
cp public_html/index.html .  (copies /Users/tjwhitfort/public_html/index.html to 
                                     /Users/tjwhitfort/index.html)
cp assign/* Backup      (copies the files in /Users/tjwhitfort/assign into the directory 
                         /Users/tjwhitfort/Backup)
cp -r assign/* Backup   (copies the files and subdirectories in /Users/tjwhitfort/assign 
                         into the directory /Users/tjwhitfort/Backup)
cp -r assign Backup     (copies the directory /Users/tjwhitfort/assign and all its files 
                         and subdirectories into the directory /Users/tjwhitfort/Backup/assign

mv

move. Moves or renames a file or files. If the file is being moved from another directory to the current working directory then use "." as the new-file-name.

Format

mv old-file-name new-file-name

Warning: if the new-file-name is an existing file it will be overwritten.

Examples (for relative references, present working directory is /Users/tjwhitfort)

mv Test.java Test3.java  (renames Test.java as Test3.java)
mv Test.java ProgEnv     (moves Test.java to /Users/tjwhitfort/ProgEnv/Test.java)
mv Test.java ProgEnv/assign   (moves /Users/tjwhitfort/Test.java to 
                               /Users/tjwhitfort/ProgEnv/assign/Test.java)
mv public_html/index.html .   (moves /Users/tjwhitfort/public_html/index.html to 
                               /Users/tjwhitfort/index.html)
mv ProgEnv/* PE  (moves the files and subdirectories in the directory 
                  /Users/tjwhitfort/ProgEnv to /Users/tjwhitfort/PE)
mv ProgEnv PE    (renames the directory /Users/tjwhitfort/ProgEnv to 
                  /Users/tjwhitfort/PE)

rm

remove. Removes (deletes) files or directories. The -r or -R switch removes all matching files in subdirectories as well. Use wildcards (*, ?) and -r with extreme care. Do an ls first to see what files would be affected.

Formats

rm file
rm [-r] directory

Warning: use with extreme care.

Options

-r     recursive. Moves the files specified, including those in subdirectories.

Examples (for relative references, present working directory is /Users/tjwhitfort)

rm Test.java   (removes Test.java)
rm *.java      (removes all files that end in ".java" from the current directory)

Examples (use with extreme care)

ls -a           (List the files to check what would be deleted by the following command)
rm *            (Removes all files in the current directory)

ls -aR ProgEnv  (List the files to check what would be deleted by the following command)
rm -r ProgEnv   (Removes the directory ProgEnv including all files and subdirectories in it)

ls -aR          (List the files to check what would be deleted by the following command)
rm -r *         (Removes all files and subdirectories in the current directory)

Printing

lpr

Remote prints to a named printer. The printers are itlab3 (Linux lab B1.11 ) and novlab5 (PC lab B1.54).

At the moment it is not possible to print from the Linux lab. Files to be printed need to be transferred to the PC network or printed at home.

Format

lpr [-#numcopies] -Pprintername filename1 [filename2 filename3...]
Options

-#numcopies.      Number of copies to print.
-Pprintername.    Printer. Name of the printer to print to.

Examples

lpr -Pitlab3 geelong.txt   Prints the file geelong.txt to the itlab3 printer)
lpr -Pnovlab5 ProgEnv/Hello.java  (Prints the file Hello.java to the novlab5 printer)
lpr -#3 -Pitlab3 Tut1.java   (Prints 3 copies of the file Tut1.java to the itlab3 printer)
lpr -Pitlab3 ex1.html Ex2.c  (Prints the files ex1.html Ex2.c to the itlab3 printer)

lpq

Displays print queue information - what is waiting to print, who it belongs to etc. The job number is used to identify a print job, and is used in the lprm command  to remove a job from the print queue.

Format

lpq [al] -Pprinter 
Options

-a    all. Reports on all queues..
-l     long. Long reporing format (more information)
-Pprintername.    Printer. Name of the printer to view the print queue.

Example

lpq -Pitlab3  (Obtain a listing of the print queue for the printer itlab3)

Rank Owner Job  Files        Total Size
1st  tim   497  Hello.java   4538 bytes
2nd  tim   368  geelong.txt    77 bytes

lprm

Removes (deletes) a job from a print queue. The job number can be obtained using the lpq command.

Format

lprm -Pprinter job#
Options

-Pprintername.    Printer. Name of the printer to remove the print job from.

Example

lprm -Pitlab3 497  (Remove job# 497 (Hello.java) from itlab3 printer queue)

Printing inside applications

How to print from some applications is outlined below. Other applications generally work in a similar manner to tjhe following.

gedit

Either print the file as above or from within nedit:

firefox

Disk Space

du

disk used. Displays the amount of disk space used in the current directory and its subdirectories. The size of each directory is displayed.

Format

du [-hkms]
Options

Only use one of the options -h, -k or -m.

-h    human readable. Displays K (for kilobytes) or M (for megabytes) after the file sizes.
-k    kilobytes. File sizes in kilobytes
-m   megabytes. File sizes in megabytes
-s    summarize. Only the total space used is displayed.

Examples

du -h
8.0K ./Temp
0 ./Temp2/Temp
0 ./Temp2/Tutorials
44K ./Temp2
8.0K ./Tutorials
120K  .    (120 kilobytes used in the current directory)

cd
du -hs
8.0M  .    (8.0 megabytes used in the users account)

df

disk free. Displays the amount of disk space free (and used, total) on the system.

Format

df [-h]
Options

-h    human readable. Displays K (for kilobytes) or M or G after the sizes.

Example

df -h
Filesystem              Type Size  use   avail %use Mounted on
/dev/root                xfs  8.3G  1.4G 7.0G  17% /
redgum:/var/mail         nfs 16.0G 16.0G 661M  97% /var/mail
redgum:/usr/home/student nfs 16.0G 16.0G 661M  97% /usr/home/student
ironbark:/usr/local/     nfs  8.5G  8.4G 115M  99% /usr/local
ironbark:/usr/freeware   nfs  8.5G  8.4G 115M  99% /usr/freeware
snowgum:/usr/src         nfs  1.9G  1.9G  58M  98% /usr/src
ironbark:/usr/gnu        nfs  4.1G  3.7G 400M  91% /usr/gnu

Explanation for the line commencing redgum:/usr/home/student (student accounts)

redgum:/usr/home/student  The name of the device
nfs                       Type of file system (nfs = Network File System)  
16.0G                     total space (G = gigabytes)
16.0G                     space used (G = gigabytes)
661M                      space available (M = megabytes)
97                        % of space used. Keep an eye on this!
/usr/home/student         the place in the system where the device is mounted

Further Reading


Written by Tim Whitfort.