Professional Documents
Culture Documents
Workingin
UNIX
Directories
andFiles
LEARNING OUTCOMES
By the end of this topic, you should be able to:
1.
2.
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
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
(root)
bin
home
sbin
var
boot
dey
log
lib
usr
bin
jones
messages
man
sbin
etc
lib
local
bin
share
lib
/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.
2.1.1
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
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
$ 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
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.
2.
3.
4.
5.
14 X
2.1.5
TOPIC 2
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
$ 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
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
Type
Access modes
Links
Owner
Group
Modification date
Name
2.1.8
Deleting Files
$ 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
16 X
TOPIC 2
You can also create a file by using the cat utility. For example, you can create a
new file by typing:
2.1.10
Copying Files
$ cp old_file new_file
For example:
$ cp file1 file2
2.1.11
Renaming Files
$ 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
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 *
$ cat test*
$ ls test?
$ cat test.?
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
SELF-CHECK 2.1
1.
2.
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.
2.
3.
4.
5.
6.
7.
TOPIC 2
19
Type cd and press Enter to make sure you are in your home
directory.
2.
3.
4.
file1
Press Enter.
5.
7.
8.
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.
12.
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
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
20 X
TOPIC 2
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
21
SELF-CHECK 2.2
Describe the file permission level of the following executable file:
2.1.16
3 Mar
2 13:42 hello
The command chmod is used to change the permissions on a file. The format of
this command is:
2.1.17
Chmod Modes
In the command:
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
(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
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
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.
Wildcard
UNIX file permission
Chmod modes