Lecture 18 - Unix 2 - Files and Directories


Home > Lectures > Lecture18

Objectives

Contents


The upcoming tutorial is in the Linux Lab B1.11

From now on check the subjects tutorial page to see what room you are in.

Files

Filenames

Example file names:

HelloWorld.java, readme.txt, assigment1, tut_12, "my documents", myPhoto.jpg, .login

Absolute vs Relative References

The 'Reference' corresponds to where Unix begins searching for a file. There are two possibilities:

Example: Directory tree (partial)

           /
           |
 +----+----+--------+
 |    |    |        |
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

Example 1a: Absolute paths referring to directories

/
/bin
/etc
/Users
/Users/tjwhitfort
/Users/tjwhitfort/ProgEnv
/Users/tjwhitfort/ProgEnv/assign
/Users/tjwhitfort/public_html
/Users/wkvarcoe

Example 1b: Absolute paths referring to files

/etc/passwd
/Users/tjwhitfort/geelong.txt
/Users/tjwhitfort/ProgEnv/assign/Assign1.java
/Users/tjwhitfort/public_html/index.html
/Users/wkvarcoe/x.txt

Example 2a: relative paths referring to directories

(Assume the present working directory is /Users/tjwhitfort)

ProgEnv         (refers to /Users/tjwhitfort/ProgEnv)
ProgEnv/assign  (refers to /Users/tjwhitfort/ProgEnv/assign)
public_html     (refers to /Users/tjwhitfort/public_html)
.               (refers to /Users/tjwhitfort)
..              (refers to /Users)
../wkvarcoe     (refers to /Users/wkvarcoe)
../..           (refers to /)
../../etc       (refers to /etc)

Example 2b: relative paths referring to files

(Assume the present working directory is /Users/tjwhitfort)

geelong.txt                  (refers to /Users/tjwhitfort/geelong.txt)
ProgEnv/Hello.java           (refers to /Users/tjwhitfort/ProgEnv/Hello.java)
ProgEnv/assign/Assign1.java  (refers to /Users/tjwhitfort/ProgEnv/assign/Assign1.java)
public_html/index.html       (refers to /Users/tjwhitfort/public_html/index.html)
../wkvarcoe/x.txt            (refers to /Users/wkvarcoe/x.txt)
../../etc/passwd             (refers to /etc/passwd)

Example 2c: relative paths referring to files

(Assume the present working directory is /Users/tjwhitfort/ProgEnv)

Hello.java                  (refers to /Users/tjwhitfort/ProgEnv/Hello.java)
./Hello.java                (refers to /Users/tjwhitfort/ProgEnv/Hello.java)
assign/Assign1.java         (refers to /Users/tjwhitfort/ProgEnv/assign/Assign1.java)
../geelong.txt              (refers to /Users/tjwhitfort/geelong.txt)
../public_html/index.html   (refers to /Users/tjwhitfort/public_html/index.html)

Example 2d: paths referring to directories and files using ~

(Assume the home directory is /Users/tjwhitfort; for these examples it doesn't matter what the present working directory is)

~                       (refers to /Users/tjwhitfort)
~/ProgEnv               (refers to /Users/tjwhitfort/ProgEnv)
~/ProgEnv/Hello.java    (refers to /Users/tjwhitfort/ProgEnv/Hello.java)
~/..                    (refers to /Users)
~/../..                 (refers to /)

Using Relative Addresses

Determining relative addresses:

Example (home directory /Users/tjwhitfort)

cd /Users/tjwhitfort (pwd is /Users/tjwhitfort)
.                    (pwd is /Users/tjwhitfort/ substitute /Users/tjwhitfort/ for . = 
                      /Users/tjwhitfort/)
..                   (pwd is /Users/tjwhitfort/ remove last directory for the .. =
                      /Users/
Hello.java           (pwd is /Users/tjwhitfort/ combined with Hello.java = 
                      /Users/tjwhitfort/Hello.java)
ProgEnv/assign       (pwd is /Users/tjwhitfort/ combined with ProgEnv/assign = 
                      /Users/tjwhitfort/ProgEnv/assign)
../wkvarcoe/x.txt    (pwd is /Users/tjwhitfort/, remove last directory for the .. gives
                      /Users/ combined with wkvarcoe/x.txt = /Users/wkvarcoe/x.txt)
../../etc/passwd     (pwd is /Users/tjwhitfort/, remove last two directories for the ../.. 
                      gives / combined with etc/passwd = /etc/passwd)
cd /etc
~/public_html/index.html  (substituting home directory for ~ gives:
                           /Users/tjwhitfort/ combined with public_html/index.html = 
                           /Users/tjwhitfort/public_html/index.html
~/../wkvarcoe/x.txt  (substituting home directory for ~ gives /Users/tjwhitfort/ and remove 
                      single directory for .., gives /Users/ combined with wkvarcoe/x.txt = 
                      /Users/wkvarcoe/x.txt)

Listing Files

ls

ls is used to list the files in a directory. The files are generally listed in alphabetical order in columns, from top to bottom left to right. Uppercase (capitals) come before lowercase when sorting into alphabetical order.

Format

Format (simplified)

ls [-adlR] [names]

Note: [] indicates optional

Example 1 (working directory is /Users/tjwhitfort)

ls
ProgEnv       geelong.txt  public_html

Example 2 (with some more files)

ls
Assignment2  ProgEnv      dumpster     nsmail tmp
Desktop      Test.class   geelong.txt  public_html
Documents    Test.java    mac          tim.outbox

Commonly used Options (Switches/ Flags)

-a Option

Lists all the files in the directory. This shows all files including hidden files. Hidden files have names starting with a "." e.g. .login

ls -a
.login       Progenv       geelong.txt  public_html

-l Option

Displays a listing of files using the long format.

ls -l
drwx------ 2  tjwhitfort  itstaff     9  May 4  20:24  ProgEnv
-rw-r--r-- 1  tjwhitfort  itstaff    77  Apr 30 10:00  geelong.txt
drwxr-xr-x 5  tjwhitfort  1234     4096  Apr 30 10:14  public_html

Explanation for the first line above

drwx------   The first column are the file protections: (more about these later)
2            The number of links to the file.
tjwhitfort   The username (or number) of the owner of the file.
itstaff      The group name (or number) the user belongs to.
9            The size of the file in bytes.
May 4 20:24  The date the file was last modified. If the year isn't stated then it is the current year
ProgEnv      The name of the file.

ls -l filename

Displays a long listing about a directory or file

ls -l ProgEnv
drwx------ 2  tim  itstaff     9  May 4  20:24  ProgEnv

-al Options

Combines the -a (all) and -l (long format) option. ls -al is the same as ls -a -l

ls -al
-rwx------ 2  tjwhitfort  itstaff   152  May 7  20:24  .login
drwx------ 2  tjwhitfort  itstaff     9  May 4  20:24  ProgEnv
-rw-r--r-- 1  tjwhitfort  itstaff    77  Apr 30 10:00  geelong.txt
drwxr-xr-x 5  tjwhitfort  1234     4096  Apr 30 10:14  public_html

-R Option

Display a listing of files in the specified directory and Recursively display the files in all of its subdirectories

ls -R
ProgEnv       geelong.txt  public_html
./ProgEnv:
Hello.java    assign                  
./ProgEnv/assign:
Assign1.class Assign1.Java   
./public_html:
default.gif   index.html

-d Option

Display a listing of files in the current directory without showing the contents of subdirectories. This is useful when use wildcards (* or ?) as files in subdirectories are automatically shown when using wildcards.

ls -d g*
geelong.txt

Using absolute references

ls /User/tjwhitfort
ls -l /Users/tjwhitfort/ProgEnv/assign
ls -d /Users/t*
ls -a /etc

Using relative references  (home and present working directory is /Users/tjwhitfort)

ls                    (same as ls /Users/tjwhitfort)
ls  .                 (same as ls /Users/tjwhitfort)
ls  ~                 (same as ls /Users/tjwhitfort)
ls  -a ProgEnv        (same as ls -a /Users/tjwhitfort/ProgEnv)
ls  ProgEnv/assign    (same as ls /Users/tjwhitfort/ProgEnv/assign)
ls  ..                (same as ls /Users)
ls  -l ../wkvarcoe    (same as ls -l /Users/wkvarcoe)
ls  -al ../..         (same as ls -al /)
ls  ~/ProgEnv/assign  (same as ls /Users/tjwhitfort/ProgEnv/assign)

Wildcard Characters

The shell (tcsh) also provides a pattern matching facility, (called globbing) where a pattern is typed in instead of a file name. The shell then replaces that pattern with all the pathnames that match it before executing the command.

Two main wildcharacters are:

Examples

ls a*     (Displays all filenames with 'a' as the first character)
ls *.*    (Displays all filenames containing a '.' anywhere in the name) 
ls *      (Displays all filenames (same as: ls))
ls ?h*    (Display all files with a 'h' character in the second position) 

Displaying Files

Files can be displayed using cat, less or more. cat is generally used for viewing very small files. less and more display a text file a screen at a time. more can only more forwards through a file, whereas less can more forwards and backwards. Some commands in less are q to quit, space or f for forward one screen, b for back one screen..

Format (simplified)

cat file-name

less file-name

more file-name

Examples (present working directory is /Users/tjwhitfort)

cat geelong.txt
more geelong.txt
less ProgEnv/Hello.java

less ../wkvarcoe/ProgEnv/tut1.java
less /etc/passwd

Creating Text Files

Text files consist of simple characters such as digits and letters that can be displayed on the screen. Text files can be displayed, created or modified using a text editor. Text files can also be displayed using the commands: cat, less or more. Some text editors available are: nedit, pico, ee, vi, emacs. Some of these editors will be discussed later.

Some files are binary files. Examples of binary files are executable programs and graphics files; these files contain specially encoded information that is not in a useful format for humans to read. For example a graphics file needs a special program to interpret and display the data in the file.

nedit

nedit is an editor that uses a graphical user interface (menus, mouse etc). It is easy to use and has some nice features for writing programs. This will be the recommended editor for creating Java programs (although any text editor will do).

Format

nedit [filename]

Examples

nedit&             Create a new file without specifying its name
nedit Hello.java&  Open a new or existing file called 'Hello.java'

Using nedit

nedit has many features in common with a Word Processor, but requires an <Enter> at the end of each line of text.

It can save a lot of time to learn the shortcut keys. Many of the shortcut keys are the same as for windows e.g. Cut (Ctrl+X), Copy (Ctrl+C), Paste (Ctrl+V), Undo (Ctrl+Z), Save (Ctrl+S).

Preferences

nedit provides are number of preferences that are useful for programmers. The preferences can be changed temporarily or permanently. To change preferences until you exit nedit select options from the Preferences menu (e.g. Preferences > Statistics Line). To save your preferences permanently use Preferences > Default Settings >(set the desired preferences), then Preferences > Save Defaults.

Some useful preferences settings are:

If you would like to use a different font size, set the following

To save the above settings you must:

To activate the preferences exit nedit then run it again.

Working with Directories

Special directories are:

pwd

Display the path name of the present (current) working directory

Format

pwd

Example

pwd
/Users/tjwhitfort

cd

change the present working directory. This affects commands that use relative addresses, as the addresses are relative to the current 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

makes (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

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 a file or files.

Format

cp [-r] file1 target-file

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

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

moves or renames a 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

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.

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)

Key Points

Further Reading


Previous Lecture | Lecture Index | Next Lecture | Tutorial
Last modified 02-May-2006 by Tim Whitfort.
Copyright © 2003-2006 Brian Retallick & Tim Whitfort