UNIX / Linux Tutorial
 
1.9.3  Pipes  |  1.9.4  Non-Destructive Redirection Of Output
1.10 File Permissions  |  1.10.1  Concepts  Of File Permissions
1.10.2  Interpreting File Permissions
1.9.3  Pipes

We already demonstrated how to use sort as a filter. However, these examples assume
that you have data stored in a file somewhere or are willing to type the data from the
standard input yourself. What if the data that you wanted to sort came from the output of
another command, like ls?

The -r option to sort sorts the data in reverse-alphabetical order. If you want to list
the files in your current directory in reverse order, one way to do it is follows:

/home/patrick/papers# ls
                   english-list

                   history-final

                   masters-thesis

                   notes

Now redirect the output of the ls command into a file called file-list:

                  /home/patrick/papers#  ls  >  > file - list

                  /home/patrick/papers#   sort   -r  file - list.

                  notes

                  masters-thesis

                  history-final

                  english-list

                  home/patrick/papers#

Here, you save the output of ls in a file, and then run sort -r on that file. But this is
unwieldy and uses a temporary file to save the data from ls.

The solution is pipelining. This is a shell feature that connects a string of commands via
a "pipe." The stdout of the first command is sent to the stdin of the second command.
In this case, we want to send the stdout of ls to the stdin of sort. Use the "|"
symbol to create a pipe, as follows:

                  /home/patrick/papers# ls  |  j sort   - r

                  notes

                  masters-thesis

                  history-final

                  english-list

                 /home/patrick/papers#


This command is shorter and easier to type.

Here's another useful example, the command:

                /home/patrick/papers#  ls   /usr  /bin

displays a long list of files, most of which fly past the screen too quickly for you to read.
So, let's use more to display the list of files in /usr/bin.

               /home/patrick/papers#   ls  /usr/   bin  j more

Now you can page down the list of files at your leisure.

But the fun doesn't stop here! You can pipe more than two commands together. The
command head is a filter that displays the first lines from an input stream (in this case,
input from a pipe). If you want to display the last filename in alphabetical order in the
current directory, use commands like the following:

              /home/patrick/papers# ls  | j sort -r   |  j head -1

               notes

              /home/patrick/papers#

where head -1 displays the first line of input that it receives (in this case, the stream of
reverse-sorted data from ls).


1.9.4   Non - Destructive Redirection Of Output

Using ">" to redirect output to a file is destructive: in other words, the command:

             /home/patrick/papers#  ls  >  >   file-list

overwrites the contents of the file file-list. If instead, you redirect with the symbol
">>", the output is appended to (added to the end of) the named file instead of overwriting
it. For example:

            /home/patrick/papers#   ls  >  >  >> file - list

appends the output of the ls command to file-list.

Keep in mind that redirection and pipes are features of the shell—which supports the
use of ">", ">>"and"|". It has nothing to do with the commands themselves.


1.10   File Permissions

1.10.1  Concepts  Of File Permissions

Because there is typically more than one user on a Linux system, Linux provides a
mechanism known as file permissions, which protect user files from tampering by other
users. This mechanism lets files and directories be "owned" by a particular user. For
example, because Larry created the files in his home directory, Larry owns those files and
has access to them.

Linux also lets files be shared between users and groups of users. If Larry desired, he
could cut off access to his files so that no other user could access them. However, on most
systems the default is to allow other users to read your files but not modify or delete them
in any way.

Every file is owned by a particular user. However, files are also owned by a particular
group, which is a defined group of users of the system. Every user is placed into at least
one group when that user's account is created. However, the system administrator may
grant the user access to more than one group.

Groups are usually defined by the type of users who access the machine. For example,
on a University Linux system users may be placed into the groups student, staff,
faculty or guest. There are also a few system-defined groups (like bin and admin)
which are used by the system itself to control access to resources—very rarely do actual
users belong to these system groups.

Permissions fall into three main divisions: read, write, and execute. These permissions
may be granted to three classes of users: the owner of the file, the group to which the file
belongs, and to all users, regardless of group.

Read permission lets a user read the contents of the file, or in the case of directories, list
the contents of the directory (using ls). Write permission lets the user write to and modify
the file. For directories, write permission lets the user create new files or delete files within
that directory. Finally, execute permission lets the user run the file as a program or shell
script (if the file is a program or shell script). For directories, having execute permission
lets the user cd into the directory in question.


1.10.2  Interpreting File Permissions

Let's look at an example that demonstrates file permissions. Using the ls command
with the -l option displays a "long" listing of the file, including file permissions.


/home   / patrick   / foo#    ls   - l     stuff

-rw-r--r--    1  patrick     users      505    Mar   13    19:05   stuff

/home  /  patrick  / foo#

The first field in the listing represents the file permissions. The third field is the owner
of the file (patrick) and the fourth field is the group to which the file belongs (users).
Obviously, the last field is the name of the file (stuff). We'll cover the other fields later.

This file is owned by patrick and belongs to the group users. The string
-rw-r--r-- lists, in order, the permissions granted to the file's owner, the file's group,
and everybody else.

The first character of the permissions string ("-") represents the type of file. A "-"
means that this is a regular file (as opposed to a directory or device driver). The next three
characters ("rw-") represent the permissions granted to the file's owner, patrick. The
"r" stands for "read" and the "w" stands for "write". Thus, patrick has read and write
permission to the file stuff.

As mentioned, besides read and write permission, there is also "execute" permission—
represented by an "x". However, a "-" is listed here in place of an "x", so Patrick doesn't
have execute permission on this file. This is fine, as the file stuff isn't a program of any
kind. Of course, because Patrick owns the file, he may grant himself execute permission for
the file if he so desires. (This will be covered shortly).

The next three characters, ("r--"), represent the group's permissions on the file. The
group that owns this file is users. Because only an "r" appears here, any user who
belongs to the group users may read this file.
The last three characters, also ("r--"), represent the permissions granted to every other
user on the system (other than the owner of the file and those in the group users). Again,
because only an "r" is present, other users may read the file, but not write to it or execute
it.

Here are some other examples of permissions:

-rwxr-xr-x    The owner of the file may read, write, and execute the file. Users in the
              file's group, and all other users, may read and execute the file.

-rw-------    The owner of the file may read and write the file. No other user can
              access the file.

-rwxrwxrwx    All users may read, write, and execute the file.

HOME

1.1 Introduction   1.2.10 Referring To Home Directories   1.3.4  Copying Files

1.6 Exploring The File System   1.8   Wildcards   1.9.3 Pipes   1.10.3 Permissions Dependencies

1.12.4  Stopping And Restarting Jobs   1.13.3 Inserting Text   1.13.9 Including Other Files

1.14.3 Shell Initialization Scripts   System Administration   2.3.1 The /etc/imitate file

2.4 Managing File Systems   2.6 Managing Users  2.6.5 Groups   2.7.2 gzip and compress

2.8.3 Making Backups To Tape Devices   2.9.1 Upgrading The Kernel   

2.9.3 Installing A Device Driver Module

BOOK: LINUX QUICK COMMAND REFERENCE

http://personal.atl.bellsouth.net/~psadler

© copyright KnowledgeWorks, Inc. (2001)