Sei sulla pagina 1di 13

Topic2

Workingin
UNIX
Directories
andFiles

LEARNING OUTCOMES
By the end of this topic, you should be able to:
1.

Describe files and directory system in UNIX

2.

Describe UNIX commands used for handling files and directories

INTRODUCTION

After you explore essential background information about UNIX in the previous
topic. This topic presents the UNIX file system: We look at its partitions and
directories and how to navigate the file system.

2.1

WORKING WITH UNIX DIRECTORIES AND


FILES

This section focuses on the UNIX hierarchical system. You also learn the basics of
UNIX file permissions and UNIX file manipulation commands.
UNIX has a hierarchical tree-like filestore. The filestore contains files and
directories. The top-level directory is known as the root. Beneath the root are
several system directories. The root is designated by the/character. Figure 2.1
shows a typical UNIX file system hierarchy.

12 X

TOPIC 2

WORKING IN UNIX DIRECTORIES AND FILES

(root)
bin

home
sbin

var
boot

dey

log

lib

usr

bin

jones

messages

man

sbin
etc

lib

local
bin

share

lib

Figure 2.1: UNIX file system hierarchy

/bin: Contains many of the programs that will be executed by users.


/sbin: Contains applications and services that support the system

/boot: Contains the UNIX kernel and other system initialization files.
/dev: Contains device driver files.
/usr: A read-only hierarchy containing files that can be shared among

initialization process.

machines.

/lib: Libraries, shared libraries, and kernel


modules.
/var: Holds variable data (such as log files and print jobs waiting to be
printed). The file /var/log/messages, for example, stores system log
messages.

/home: Users login directories appear in /home. When the


administrator creates a new user account, the system assigns a directory in
/home to that user. The login (or home) directory matches the accounts
user name. Thus /home/jones is the login (or home) directory for the
user jones. Jones can have his own directory in which he can create and
delete files, and create their own sub-directories.

2.1.1

The Current Directory

This is the actual location in the filestore hierarchy. When students log in, the
current directory is set to the home directory. Students can then change the
current directory, effectively moving around the filestore tree structure. The
current directory is also called the working directory. The current directory can
be referred to in pathnames by the . character (a full stop).

TOPIC 2

2.1.2

WORKING IN UNIX DIRECTORIES AND FILES

13

Creating a Directory

The mkdir command is used to create directories. The format of this command is:

$ mkdir directory_name
For example, you store your UNIX scripts in a directory called scripts beneath
their home directory. In order to create this directory, you can use the command:

$ mkdir scripts

2.1.3

Changing Current Directory

The command cd is used to change your current directory. For example:

$ cd scripts
will move you from your current directory, down one branch to the directory
scripts, if such a directory exists. Typing cd with no arguments takes you to your
home directory.

2.1.4

Displaying Current Directory

The command pwd is used to display your current directory. For example:

$ pwd
/home/s1234567/scripts
To practise navigating directories, try the next activity.
ACTIVITY 2.1
Log into the UNIX system with your account. Try the following items:
1.

Check which directory you are currently in. If necessary, move to


your home directory. (Remember: cd will do this from anywhere.)

2.

Create a subdirectory named scripts.

3.

Change your current directory to scripts directory by using the cd


command.

4.

Display your current directory by using the pwd command.

5.

Go back to your home directory by using the cd command without


argument.

14 X

2.1.5

TOPIC 2

WORKING IN UNIX DIRECTORIES AND FILES

The Parent Directory

The parent directory is the directory above the current directory. The parent
directory can be referred to by the .. characters (two full stops). For example to
refer to the file test.dat in the parent directory:

../test.dat

2.1.6

Deleting a Directory

The rmdir command is used to delete directories. The format of this command
is:

$ rmdir directory_name
For example,

$ rmdir scripts
Note that the directory must be empty before it can be deleted.

2.1.7

Listing Contents of a Directory

The command ls is used to list the contents of a directory. For example:

$ ls
. ..
Notice that both directories and files are listed. To list all files, including hidden
files, give the command:

$ ls -a
. .. .bash_history
At least two new files have appeared, with the names . (dot) and .. (dot dot). A
single . is a special notation for the working directory. A .. is a special notation for
the parent directory. Other hidden files begin with . (a full stop) and are normally
system files.
To get more information about each file, you can use the l option (thats a
lowercase letter L) associated with ls command. This option can be used alone,
or in combination with the a option.

TOPIC 2

$ ls -al
total 23
drwxr-xr-x
drwxr-xr-x
-rw-------

3
1146
1

type access
code

# of
links

WORKING IN UNIX DIRECTORIES AND FILES

15

s1234567
root
s1234567

ct212
root
ct212

1024
2048
279

Feb 17 10:09
Dec 3 17:44
Feb 17 10:09

.
..
.bash_history

owner

group

size

modification
date and time

name

The long format provides the following information about each file:
Total n

n amount of storage used by the files in this directory.

Type

tells whether the file is a directory (d) or a plain file (-).

Access modes

specifies three types of user (you, your group, all others)


who are allowed to read (r), write (w), or execute (x) your
files.

Links

the number of files and directories linked to the file.

Owner

who created or owns the file.

Group

the group that owns the file.

Size (in bytes)

the size of the file.

Modification date

when the file was last modified.

Name

name of the file or directory

2.1.8

Deleting Files

Files can be deleted using the rm command. For example:

$ rm test.f

2.1.9

Displaying Files

The command cat is used to display the contents of a file on the screen. For
example:

$ cat file1
Cat is short for concatenate, which is also used to put files together in order to
create a bigger file. For example:

$ cat file1

file2 > file3

16 X

TOPIC 2

WORKING IN UNIX DIRECTORIES AND FILES

You can also create a file by using the cat utility. For example, you can create a
new file by typing:

$ cat > practice

2.1.10

Copying Files

The command cp is used to copy a file. It takes the format:

$ cp old_file new_file
For example:

$ cp file1 file2

2.1.11

Renaming Files

The command mv is used to rename a file. For example:

$ mv file2 temp
changes the name of file2 to temp.

2.1.12

Moving Files

The command mv is also used to move a file to a new location in the filestore
hierarchy. For example:

$ mv file2 bin
moves the file file2 into the subdirectory bin.

2.1.13

Overwriting Files

Commands such as rm and cp can be dangerous if not used with care. The
command:

$ cp file1 file2
will delete file2 if a file of that name already exists. If you have spelled the name
of the new file incorrectly, you may accidentally overwrite the contents of a file.
Using the wildcard symbol * with the command rm can also be very dangerous.
The command:

$ rm test*
will delete all files starting with test.

TOPIC 2

WORKING IN UNIX DIRECTORIES AND FILES

17

To prevent accidental deletion of files, you can use the -i option with
commands such as rm. The format of the command is:

$ rm -i file
You will be asked to confirm that files are to be deleted. You may find that this is
set as the default on your system.

2.1.14

Wildcards

Wildcard characters can be used to identify directory and file names. The
wildcard character * is used to refer to any combination of characters. For
example:

$ ls *

refers to all files

refers to all files starting with test, e.g. test, testing,


test.c, etc.
The wildcard character ? is used to refer to a single character. For example:

$ cat test*

$ ls test?

refers to files starting with test followed by a single


character, e.g. test1, test2, etc.

$ cat test.?

refers to all files starting with test with a single character


after the full

stop, e.g. test.c, test.f

This reading presents a review of the UNIX system. If you have had some
exposure to UNIX, it will refresh your prior knowledge. If have never seen
UNIX, it will serve as a quick introduction to the system. After this reading, you
will become familiar with the basic file and directory commands such as ls, rm,
cp, mv, pwd and cd.
Matloff, N (2001) UNIX the bare minimum:

http://heather.cs.ucdavis.edu/~matloff/UNIXAndC/UNIX/UN
IXBareMn.html

18 X

TOPIC 2

WORKING IN UNIX DIRECTORIES AND FILES

SELF-CHECK 2.1
1.

Which command would you use to create a new directory on a


UNIX system?

2.

Which command would you use to remove a directory on a UNIX


system?

3.

How would you find out more information about the mkdir
command if you didnt know how to use it?

The following activity encourages you to explore the file display in UNIX.

ACTIVITY 2.2
Log into the UNIX system with your account. Try the following items:
1.

Display your current working directory using the pwd command.

2.

Make a directory called exercises.

3.

Change your directory to the directory exercises. Display the


current working directory.

4.

Return to your home directory.

5.

List the contents of your directory. Use the l, a and F


options. Compare the output.

6.

Change your directory to the directory exercises.

7.

List the contents of your directory. Use the l option to obtain a


long listing.

TOPIC 2

WORKING IN UNIX DIRECTORIES AND FILES

19

Now try creating and copying a file in UNIX.


ACTIVITY 2.3
Log into the UNIX system with your account. Try the following items:
1.

Type cd and press Enter to make sure you are in your home
directory.

2.

Display your current working directory using the pwd command.

3.

Start the process of creating a new file by typing : a. cat >

4.

file1
Press Enter.

5.

Type in the following line:

This practice file will be used for demonstration.


6.

Press Enter and press Ctrl-Z to end the input.

7.

Type cat file1 to show the content of this file.

8.

Type cp file1 file2 at the command prompt to copy file1 to


file2.

9.

Type cat file2 to verify the content of this file same as file1.

10.

Type mv file2 temp to rename the file file2 to the file temp.

11.

Type cat temp to verify the content of this file.

12.

Type rm temp to delete the file temp.

13.

List the contents of your home directory. Files file2 and temp
should be eliminated. Use the -l option to obtain a long listing.

2.1.15

UNIX File Permissions

The UNIX file security system can prevent unauthorized users from reading or
altering files. Every file and directory has specific permissions associated with it,
giving different categories of user certain permissions to look at or change a file,
and to run executable files.
The file permissions can be displayed using the command:

$ ls -l [filename]
For example, to display the permissions on the file file1, type the command:

$ ls -l file1
-rw-r--r-- 1 s1234567 ct212

3 Mar 2 13:42 file1

20 X

TOPIC 2

WORKING IN UNIX DIRECTORIES AND FILES

The first set of characters in the output from the command (-rw-r--r--) gives the
permissions. The username in the middle of the line (s1234567) is the owner of
the file. This is user who created the file. The following fields tell you the number
of characters (i.e. three characters) in the file, the date it was created and the
name of the file.
We further elaborate the permission level of this file. Note that the first character
specifies the file type. This is normally one of the following:
- indicates a file
d indicates a directory.
The following nine characters represent permissions for different classes of user.
Users on a UNIX system are assigned to a group or groups, which might
correspond to a particular department, or research group in the real world.
Members of a particular group can be allowed access to files belonging to other
members of the group.
The second, third and fourth characters in the permissions string represent
permissions that apply to the owner of the file. The next three characters apply to
members of the owners group. The last three apply to all other users. The file in
this example therefore has rw- for the owner, r-- for the group and r-- for others.
The three characters corresponding to each class of user each represent a
different type of permission. The first character represents read permission. This
means that a user has permission to open a file and view the contents. An r in
this position means that the class of user has read permission. In this example, all
users have read permission. In this, and in every situation, a horizontal bar
character (-) means that permission is denied.
The second position represents write permission (the right to make changes to a
file). In the example, only the owner has write permission. Normally, you do not
want others to be allowed to make changes to your files, so write permission is
only allowed to the owner.
The third position represents execute permission. This means permission to
execute, or run, a file that works like a command. In this example, no one has
execute permission for the file file1.txt (it is a text program, so execute permission
would be useless). To summarize the above, this is how the permissions string is
divided up:

type of file

rwowner

r-group

r-others

TOPIC 2

WORKING IN UNIX DIRECTORIES AND FILES

21

SELF-CHECK 2.2
Describe the file permission level of the following executable file:

-rwxr-x--x 1 s1234567 ct212

2.1.16

3 Mar

2 13:42 hello

Changing File Permissions

The command chmod is used to change the permissions on a file. The format of
this command is:

$ chmod mode filename


For example, to add read permission for the group to the file file1, give the
command:

$ chmod g+r file1

2.1.17

Chmod Modes

In the command:

$ chmod mode filename


the mode consists of three elements:
(i)

who

(ii)

operator

(iii) permissions.
The following options are possible:
(i)

who:
u user (owner)
g group
o other
a all

(ii)

operators:
- remove permission
+ add permission
= assign permission

22 X

TOPIC 2

WORKING IN UNIX DIRECTORIES AND FILES

(iii) permissions:
r read
w write
x execute

SELF-CHECK 2.3
Describe the change of file permission by executing following command:
1.
2.

2.1.18

chmod
chmod

o-rw
u+x

file1
test

Permissions for Directories

Read, write and execute permissions are set for directories as well as files. Read
permission means that the user may see the contents of a directory (e.g. use ls for
this directory.) Write permission means that a user may create files in the
directory. Execute permission means that the user may enter the directory (i.e.
make it his or her current directory.)
This reading presents a summary of UNIX commands:
http://www.math.utah.edu/lab/unix/unix-commands.html
Refer to this summary sheet and practise the use of various UNIX commands in
the following case study.

2.1.19

Case Study

Your director at company Toby Chan Engineering Ltd asks you to create a
centralized telephone database called corp_phones stored in a central
directory of corp_db. In the current organization structure, there are two
departments: Human Resources (HR) and Engineering (ENG).
Your manager, Philip, suggests you create separate telephone databases for HR
and ENG. Those telephone record files would contain fields for area code, phone
prefix, phone number, last name and first name. A colon (:) would be used to
separate each field. Those files of department phone numbers would be stored in
the departmental directories. The following are the records for HR and ENG:

TOPIC 2

WORKING IN UNIX DIRECTORIES AND FILES

23

HR
219:432:4567:Harrison:Joel
219:432:4587:Mitchell:Barbara
219:432:4589:Olson:Timothy
ENG
219:432:4591:Moore:Sarah
219:432:4567:Polk:John
219:432:4501:Robinson:Lisa
After the creation of the two departmental telephone record files, you can
concatenate the two files into one central database (i.e. corp_phones). Philip also
reminds you to set file permissions to let other users access the central directory
and file.
Based on what you have learned in previous sections and topics, try to create the
centralized telephone database in plbpc011.ouhk.edu.hk server.

This topic presents the UNIX file system. We look at its partitions and
directories and how to navigate the file system.

File and directory management are considered to be one of the important task
supported by all operating systems including UNIX. File and directory
management enables easy and efficient management of directories and files.

UNIX file system hierarchy


Current directory
Parent directory

Wildcard
UNIX file permission
Chmod modes