Index->Advanced Filesystem UtilitiesAdvanced Filesystem Utilities
Overview and Goals
At the completion of this lab, you will be able to...
- decipher the properties of files and directories, including permissions,
hard links, and ownership.
- change the group owner of a file.
- change the permission of files.
- examine hidden files.
The following utilities are used in this lab:
chgrp(1)
chmod(1),
File Properties
Every file (and directory) in Linux has two owners and a set of
permissions. In this lab you will learn about these owners and how to set
permissions so you can protect your files.
You can view many of the properties of a file by using the "-l"
(el) option to ls. "-l" instructs ls to give
you a "long" listing of files. Type "ls -l /etc" and you will see
all of the files that live in /etc.
Let's decipher a typical line from the output (The horizontal spacing
in the following output has been modified from the typical "ls -l" output
to clarify the different component as described below):
-rwxrw-r-- 1 goofy faculty 462 Apr 5 12:11 runme
drwxr-x--x 4 goofy faculty 4096 Jan 20 2002 Mail
We'll describe this from right to left.
- The rightmost in the line is the name of the file. In this case
"runme" and "Mail"
- Then we have the date and time of last modification (Apr 5 12:11, Jan 20, 2002).
- Then the size of the file in bytes (462, 4096).
- Then the group owner (faculty).
- Then the (user) owner (goofy).
- Then the number of hard links (1 and 4).
- Then the file type and access permissions
(-rwxrw-r--, drwx-r-x--x). File type is encoded as
the leftmost character. In the above example, "runme" is a regular
file and "Mail" is a directory.
Timestamps
Linux stores three dates and times for each file. The date/time of
last modification of file content, the date/time of last access,
and the date/time of last modification of file status
information/properties (access permissions, user owner, group owner,
file name, ...). By default ls shows you the content
modification date. See the manual page for ls to discover how
to display the other two times. In the manual page search for "ctime",
"atime", or "time".
| Question(s)
|
- What is the difference between directories and ordinary
files? How can they be distinguished on the output of ls -l
(the character following '-' is a lowercase 'L' not digit one) command?
- Enter the command "date" and copy its output to the report of
this exercise
- Enter the command "ls -l" from your home directory. Copy and
paste its output to the report of this lab exercise.
- From the output of the previous "ls -l" identify the
files/directories that have been modified in the last three
days.
- Change your current directory to /lab/web/htdocs/GeneralInfo. All
the questions in this exercise assume that this is your current
directory.
- Compare the output of "ls -l" and "ls -lt" (the character
after "-" is an "el" not "one"). Pay close attention to the
order of timestamp. The timestamps shown by these two
commands are the last content modification time.
- Which command shows sorted timestamp?
- Which file has not been modified the longest? When is the
file modified?
- Change the current directory to a subdirectory under your home
directory where you keep a file that have not been modified the
longest (perhaps a subdirectory of your first programming project).
- Run the "ls -ltu" to show the timestamp of last use/access.
- Select a text file (.txt, .html, .java, .c, .cc, ...) that has
not been used / accessed the longest. Pick one at random, if
there are several files with the same timestamp. Record the
timestamp of the file.
- Run "cat name-of-file-you-selected" to show the content of the
file. Then run "ls -ltu" again to check the timestamp of last
use/access. How the timestamp of last access changes?
|
File Ownership
All files have two owners: a "user" owner and a "group" owner. Think of
this as two sets of people that the file belongs to; a single individual as
well as a group of people. In the file above, the group owner is "faculty",
while the "user" owner is also "goofy".
Files have two different owners to permit different access to the file
depending on who wants to open it. Permissions are covered later. For now,
it is only important to remember that the ownership of a file determines who
can access that file.
The "user" owner of a file should be easy to understand. It is typically
the person who creates the file. In our example above, the "goofy" user owns
the file.
Linux supports the concept of a group of users working together on a
project. For example, engineers working on the "Banana" project might belong
to a group called "banana". All the files that belong to the Banana project
would have a group owner of "banana", ensuring that all the members of the
group could access them. In our example above, the group owner is
"faculty".
For security reasons, the average user cannot change the "user" owner of
file. However, you are able to change the group ownership of any file that
you own. You can use the chgrp(1) command to do so. We won't cover
chgrp here. See the man page if you're interested in using it.
| Question(s)
|
- Change your current directory to /lab/web/htdocs/GeneralInfo.
Use "ls -l" to show its content. Include the output in the report of
this exercise.
- What are the (sub)directories found there?
- Who is the user owner of those directories?
- Who is the group owner of those directories?
|
Hard Links
In a previous lab you learned the ln(1) command for creating
symbolic links, or shortcuts to files. In Linux, there is another kind of
link known as a "hard" link. Hard links are fundamentally different from
symbolic links. For now it is enough to know that Linux keeps track of the
number of hard links pointing to a file, and shows you that number when you
use ls -l (that's an "el" not "one") and look at the number in the
second column.
| Question(s)
|
- Change your current directory to your home directory. Type the
following two commands at the prompt:
echo Line one > out.txt
echo Line two >> out.txt
You should now have a new file "out.txt" in your home directory.
Write down the link count of this file (second column of "ls -l"
output).
Create a hardlink (alias) of the file by typing
ln out.txt out-alias.txt
How the link count changes after the alias is created?
- Compare the size and content of the two files (out.txt and
out-alias.txt). Use "cat" to see the content of a file.
- Now remove the file out.txt using the "rm" command. How
is the link count of out-alias.txt affected? What is the content of
out-alias.txt now?
- Based on the experiment in above steps, explain "hard link"
in your own word.
- Now create a soft / symbolic link of out-alias.txt by typing
ln -s out-alias.txt sym-alias.txt
How is the link count of these two files affected?
- Enter the command "ls -l out-alias.txt sym-alias.txt" [again,
the option is an "el" not "one"]. What are
the two differences in output of ls on symbolic links and
regular files.
- Remove/delete the file out-alias.txt and then show the content of
sym-alias.txt by typing
cat sym-alias.txt
What error message did you get?
- Based on this observation, explain the difference between a
hard link and a soft/symbolic link.
|
File Permissions
Linux has a flexible, yet easy to understand scheme for determining
who can access a file. Every file has three sets of permissions: one
set for the "user" owner, one set for the group owner, and one set for
everybody else. File permissions consist of three types of access: read
(r), write (w), and execute (x).
In the output of ls -l, file permissions are shown as nine
characters. Our example of "runme" above has the permissions
"rwxrw-r--" (the initial "-" is not part of the
permissions) which can be viewed as 3 groups:
rwx rw- r--
The first three characters (rwx) are the "user" owner permissions.
They describe what the owner of the file can do to the file (Do you remember
who the owner of the file is?). "r" means the user can read the file
(i.e., look at its contents). "w" means the user can write to the
file (i.e., edit the contents). "x" means the user can execute the
file.
The next three characters (rw-) are the group owner permissions.
They define what members of the group (faculty) can do to the file.
They can read and write the file (rw), but they cannot execute the
file (there's a - instead of the x).
The last three characters (r--) are the "world" permissions. It
describes what everybody else can do to the file. If you try to access a file
and you're not the owner and you don't belong to the group, then you fall
under "world" permissions. In this example, everybody else can only read the
file. They cannot write nor execute it.
Directory Permissions
The meaning of "rwx" on directories is different from that of files.
- "r" means that the directory entries can be listed. In order to run
"ls" on a directory you must have read permission to the directory.
- "w" means that files can be added/removed from the directory.
- "x" means that we can "cd" to the directory.
| Question(s)
|
- The the command "id". The output shows three different parts. The
leftmost is your userid, the middle is your default group, and the
rightmost is group(s) that you are member of. What is your default
group?
- Change the current directory to the Unix root directory
(by typing "cd /").
- Enter the command "ls -l" and include its output in the
report of this lab.
- Which files/directories you can't access?
- In which directory can you create a new file?
|
Changing Permissions
The file permission scheme is quite powerful and allows you to control who
and how people can access your files. You can change the permissions of your
files with the chmod(1) command (NB: "mod" stands for "mode", another
term for permissions). There are many ways to change permissions using
chmod. We'll describe the easiest.
The format for chmod is:
chmod [ugoa][+-][rwx],... filename...
Square brackets are not typed when you enter the command. They indicate
that the options ugoa, +-, rwx are not mandatory.
You first specify who your changing permissions for "u" = user owner, "g" =
group owner, "o" = others, "a" = all three. Then you specify how you want to
change the permissions: "+" = add permissions, "-" = delete permissions.
Finally, you specify what permissions you want to change. The last thing is
to specify the file(s) you want to change.
Some examples are given below:
- "chmod ugo+r foo" means add (+) read permissions to
user owner, group owner, the world.
- "chmod a+r foo" is a shorthand for the previous command.
(a is the same as "ugo").
- "chmod ug+x,o-r bar" means add execute permissions for the
user and group (ug+x), and remove read permissions from "others"
(o-r) for the file bar.
Notice that in the above commands no white spaces may be inserted in
the "permission specification". For instance, the following is
incorrect:
chmod ugo + r foo
chmod ug+x , o-r bar
| Question(s)
|
- Suppose you have a subdirectory called "ImportantStuff".
What command would you type to make sure that nobody (including
yourself) can delete any files from "ImportantStuff"?
- What is the shorter way to type the following command "chmod
u-r,g-r,o-r myFile. How about "chmod "u+rw,g-w,o+rw
myNotes"?
|
Hidden Files
When you use ls, by default it shows you only the "visible" files.
Linux provides the ability to create hidden files, and ls can show
them to you. Use the -a (mneumonic: "all") option to display all the
files in a directory. Try running this command in your home directory.
You'll be surprised at how many hidden files exist.
A file is hidden simply by putting a "." in front of the filename.
For example, you probably have a .bashrc file in your home directory.
The . is a real part of the filename, and must be specified when you
are referring to the file. For example, you can make a copy of
.bashrc with the command "cp .bashrc mybashrc". You can
view the file with "cat .bashrc". You can make any file hidden
simply by changing the name of the file.
Top
|