Search Tools Links Login

Working with Files and Directories in Unix





Section 5: Working with Files and Directories



Here is an index to the topics in this section:




The UNIX filesystem structure



All the stored information on a UNIX computer is kept in a
filesystem.  Any time you interact with the UNIX shell, the
shell considers you to be located somewhere within a filesystem.
Although it may seem strange to be "located" somewhere in a computer's
filesystem, the concept is not so different from real life.  After
all, you can't just be, you have to be somewhere.  The
place in the filesystem tree where you are located is called the
current working directory.

CONCEPT: The UNIX filesystem is heirarchical (resembling a
tree structure).  The tree is anchored at a place called the root,
designated by a slash "/".  Every item in the UNIX filesystem tree is
either a file, or a directory.  A directory is like a file folder.  A
directory can contain files, and other directories.  A directory
contained within another is called the child of the other.  A
directory in the filesystem tree may have many children, but it can
only have one parent.  A file can hold information, but cannot contain
other files, or directories.






CONCEPT: To decribe a specific location in the filesystem
heirarchy, you must specify a "path."  The path to a location can be
defined as an absolute path from the root anchor point, or as a
relative path, starting from the current location.  When
specifying a path, you simply trace a route through the filesystem
tree, listing the sequence of directories you pass through as you go
from one point to another.  Each directory listed in the sequence is
separated by a slash.



UNIX provides the shorthand notation of "." to refer to the current
location, and ".." to refer to the parent directory.



EXERCISE: Specify the absolute path to the directory named
"jon" at the bottom of the tree diagram.



EXPLANATION: Since the absolute path must always begin at the
root (/) directory, the path would
be:
/users/admin/jon




EXERCISE: Specify the relative path from the directory named
"student" to the directory named "jon" in the tree diagram.




EXPLANATION: Starting from the student directory, we would
first have to move up the filesystem tree (using the ".." notation) to
the directory called "users" before we could descend to the directory
called "jon".  The path would be:
../admin/jon





File and directory permissions



CONCEPT: UNIX supports access control.  Every file and
directory has associated with it ownership, and access permissions.
Furthermore, one is able to specify those to whom the permissions
apply.


Permissions are defined as read, write, and execute.  The read,
write, and execute permissions are referred to as r, w, and x,
respectively.


Those to whom the permissions apply are the user who owns the file,
those who are in the same group as the owner, and all others.  The
user, group, and other permissions are referred to as u, g, and o,
respectively.



A short note on groups: UNIX allows users to be placed in
groups, so that the control of access is made simpler for
administrators.



The meaning of file and directory permissions





Read permission

For a file, having read permission allows you to view the
contents of the file.  For a directory, having read permission allows
you to list the directory's contents.



Write permission

For a file, write permission allows you to modify the contents of
the file.  For a directory, write permission allows you to alter the
contents of the directory, i.e., to add or delete files.



Execute permission

For a file, execute permission allows you to run the file, if it
is an executable program, or script.  Note that file execute
permission is irrelevant for nonexecutable files.  For a directory,
execute permission allows you to cd to the directory, and make it your
current working directory.



Viewing permissions




To see the permissions on a file, use the ls command, with the -l
option.

EXAMPLE: Execute the command
ls -l
/etc/passwd
to view the information on the system password
database.  The output should look similar to this:


-rw-r--r-- 1 root sys 41002 Apr 17 12:05 /etc/passwd


The first 10 characters describe the access permissions.  The first
dash indicates the type of file (d for directory, s for special file,
- for a regular file).  The next three characters ("rw-") describe
the permissions of the owner of the file: read and write, but no
execute.  The next three characters ("r--") describe the permissions
for those in the same group as the owner: read, no write, no execute.
The next three characters describe the permissions for all others:
read, no write, no execute.

Setting permissions



UNIX allows you to set the permissions on files that you own.  The
command to change the file permission mode is chmod.  Chmod requires
you to specify the new permissions you want, and specify the file or
directory you want the changes applied to.

To set file permissions, you may use to the "rwx" notation to
specify the type of permissions, and the "ugo" notation to specify
those the permissions apply to.

To define the kind of change you want to make to the permissions,
use the plus sign (+) to add a permission, the minus sign (-) to
remove a permission, and the equal sign (=) to set a permission
directly.

EXAMPLE: Type the command
chmod g=rw-
~/.shrc
to change the file permissions on the file .shrc, in
your home directory.  Specifically, you are specifying group read
access and write access, with no execute access.

EXERCISE: Change the permissions on the .shrc file in your
home directory so that group and others have read permission only.


EXPLANATION: Typing the command
chmod go=r--
~/.shrc
would accomplish the task.


Changing Directories

In UNIX, your location in the
filesystem heirarchy is known as your "current working directory."
When you log in, you are automatically placed in your "home
directory."  To see where you are, type the command
pwd
which stands for "print working
directory."

To change your location in the filesystem heirarchy, use the cd
(change directory) command, followed by an argument defining where you
want to go.  The argument can be either an absolute path to the
destination, or a relative path.

EXAMPLE: Type the command
cd /tmp
to
go to the /tmp directory.  You can type
pwd
to
confirm that you're actually there.




If you type the cd command without an argument, the shell will place
you in your home directory.

EXERCISE: Type the command
pwd
and
note the result.  Then type
cd ..
to the shell.
Type
pwd
again to see where you ended up.

EXPLANATION: The "cd .." command should have moved you up
one level in the directory tree, because ".." is UNIX shorthand for
the parent directory.  The result of the second "pwd" command should
be the same as the first, with the last directory in the path omitted.



Listing the contents of a directory




The ls command allows you to see the contents of a directory, and to
view basic information (like size, ownership, and access permissions)
about files and directories.  The ls command has numerous options, so
see the manual page on ls (type man ls) for a complete listing.
The ls command also accepts one or more arguments.  The arguments can
be directories, or files.

EXAMPLE: Type the command
ls -lr
/etc/i*
to the UNIX shell.


In the example, the "l" and "r" options of the ls command are
invoked together.  Some commands permit you to group options in that
way, and some commands require the options to be named separately,
e.g., ls -l -r.  The l option calls for a long output, and
the r option causes ls to operate recursively, moving down directory
trees.



The last part of the example, "/etc/i*", directs the ls command to
list files and directories in the /etc directory, that begin with the
letter i.  The wildcard character, "*", matches any character(s).

EXERCISE: Type the command
ls -m
/etc/i*g
to the shell.  How did the shell respond, and why?

EXPLANATION: The shell responded by printing all the
entries in the /etc directory that start with the letter i, and end
with the letter g.  The -m option causes the output to be streamed
into a single line.  See the manual page for ls to get a complete
description of the ls command's options.


EXERCISE: Find the permissions on your home directory.


EXPLANATION: There are many ways to accomplish this.  You
could type
cd
to get to your home directory, and
then type
ls -la
The -a option instructs the ls
command to list all files, including those that start with the period
character.  The directory permissions are listed next to the "."
symbol.  Remember that "." is UNIX shorthand for the current working
directory.



Viewing the contents of a file




CONCEPT: Text files are intended for direct viewing, and other
files are intended for computer interpretation.



The UNIX file command allows you to determine whether an unknown file
is in text format, suitable for direct viewing.

EXERCISE: Type the command
file
/bin/sh
to see what kind of file the shell is.

EXPLANATION: The shell is a shared executable, indicating
that the file contains binary instructions to be executed by the
computer.



The cat command


The cat command concatenates files and sends them to the screen.  You
can specify one or more files as arguments.  Cat makes no attempt to
format the text in any way, and long output may scroll off the screen
before you can read it.

EXAMPLE: Send the contents of your .profile file to the
screen by typing
cat ~/.profile
to the shell.
The tilde character (~) is UNIX shorthand for your home directory.



The more command


The more command displays a text file, one screenful at a time.  You
can scroll forward a line at a time by pressing the return key, or a
screenful at a time by pressing the spacebar.  You can quit at any
time by pressing the q key.

EXAMPLE: Type
more /etc/rc
to the
shell.  Scroll down by pressing return, and by pressing the spacebar.
Stop the more command from displaying the rest of the file by typing
q.



The head and tail commands


The head command allows you to see the top part of a file.  You may
specify the number of lines you want, or default to ten lines.

EXAMPLE: Type
head -15 /etc/rc
to see
the first fifteen lines of the /etc/rc file.


The tail command works like head, except that it shows the last lines
of of file.

EXAMPLE: Type
tail /etc/rc
to see the
last ten lines of the file /etc/rc.  Because we did not specify the
number of lines as an option, the tail command defaulted to ten lines.



Copying files and directories




The UNIX command to copy a file or directory is cp.  The basic cp
command syntax is cp source destination.

EXAMPLE: The command
cp ~/.profile
~/pcopy
makes a copy of your .profile file, and stores it in
a file called "pcopy" in your home directory.

EXERCISE: Describe the permissions necessary to
successfully execute the command in the previous example.


EXPLANATION: To copy the .profile file, one must have read
permission on the file.  To create the new file called pcopy, one must
have write permission in the directory where the file will be created.




Moving and renaming files




The UNIX mv command moves files and directories.  You can move a file
to a different location in the filesystem, or change the name by moving
the file within the current location.

EXAMPLE: The command
mv ~/pcopy
~/qcopy
takes the pcopy file you created in the cp exercise,
and renames it "qcopy".





Removing files




The rm command is used for removing files and directories.  The syntax
of the rm command is rm filename.  You may include many
filenames on the command line.

EXAMPLE: Remove the the shrccopy file that you placed in
your home directory in the section on moving files by typing
rm ~/.shrccopy



Creating a directory




The UNIX mkdir command is used to make directories.  The basic syntax
is mkdir directoryname.  If you do not specify the place
where you want the directory created (by giving a path as part of the
directory name), the shell assumes that you want the new directory
placed within the current working directory.

EXAMPLE: Create a directory called foo within your home
directory by typing
mkdir ~/foo



EXERCISE: Create a directory called bar, within the directory
called foo, within your home directory.


EXPLANATION: Once the foo directory is created, you could
just type
mkdir ~/foo/bar
Alternately, you could
type
cd ~/foo; mkdir bar
In the second solution,
two UNIX commands are given, separated by a semicolon.  The first part
of the command makes foo the current working directory.  The second
part of the command creates the bar directory in the current working
directory.


Removing a directory




The UNIX rmdir command removes a directory from the filesystem tree.
The rmdir command does not work unless the directory to be removed is
completely empty.



The rm command, used with the -r option can also be used to remove
directories.  The rm -r command will first remove the contents of the
directory, and then remove the directory itself.

EXERCISE: Describe how to remove the "foo" directory you
created, using both rmdir, and rm with the -r option.

EXPLANATION: You could enter the commands
rmdir ~/foo/bar; rmdir ~/foo
to accomplish the
task with the rmdir command.  Note that you have to rmdir the bar
subdirectory bfore you can rmdir the foo directory.  Alternately, you
could remove the foo directory with the command
rm -r
~/foo




About this post

Posted: 2005-11-1
By: FortyPoundHead
Viewed: 1,977 times

Categories

Tutorials

Linux

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.