. A pipe is a form of redirection that is used in Linux
to send the output of one program to another for further processing. Redirection is the
transferring of standard output to some other destination, such as another program, a file
or a printer, instead of the display monitor (the default destination). Standard output,
sometimes abbreviated as stdout, is the destination of the output from command line
(i.e., all-text mode) programs in Linux.
Command pipes are used to create what can be visualized as a pipeline of commands, which is
a temporary direct connection between two or more simple programs. This connection makes
possible the performance of some highly specialized task that none of the constituent
programs could perform by themselves. A command is merely an instruction provided by a user
telling a computer to do something, such as showing the contents of a directory. The command
line programs that carries out further processing are commonly known as as filters.
This direct connection between programs allows them to operate simultaneously and permits data
to be transferred between them continuously rather than having to pass it through temporary
text files or through the display screen and having to wait for one program to be completed
before the next program begins.
A pipe is designated in commands by the vertical bar character, which is located on the same key
as the backslash on US keyboards. The general syntax for pipes is:
command_1 | command_2 [| command_3 . . . ]
This chain can continue for any number of commands or programs. The output result of one command
is fed to the next command in the chain. For example ls -l | grep ".jpg" | sort -r
to display the files ending with .jpg from a directory in a reverse alphabetical order.
The command ls lists the directory, grep filters out the files ending with .jph, and finally the
command sort does the sorting. Output result from ls is fed into grep, and the result from grep
is fed into sort to do the required sorting.
Hard link and soft link.
You are probably familiar with desktop shortcuts in Microsoft Windows. Shortcuts which store a
path name to a target file provide a convenient mechanism to point to other commonly used files,
programs, or directories, without having to duplicate the original file item. Shortcuts are
often organized on the computer desktop so that frequently used items can be accessed quickly
by a simple mouse click. The Linux file system supports the convenient function of shortcuts with
soft links (also known as symbolic links). This concept is extended further to support hard links.
Files in Linux file system do not actually live in directories. They are located by an internal
structure known as inode. A file can be uniquely identified by telling the file system its inode.
Information from the inode helps the file system to locate where the file is stored and its file
size. A hard link is a special file containing inode information of the original file to give the
illusion of using the same original file but having a different name and at a different disk
location. Hard links do not consume any extra space on your disk since there is no duplication
of data involved. Changing the contents of the original file or one of its hard links change the
contents of all of its hard links as well. A file can have multiple hard links, appearing in
multiple directories, but is not deleted until there are no remaining hard links to it.
Here are some other differences between hard links and symbolic links:
1. Hard link cannot be created for a directory.
2. If you remove the original file of a hard link, the hard link still shows the contents
of the original file, since the actual contents on disk can still be located with inode information.
3. A symbolic link can link to a directory.
4. A symbolic link, like a Windows shortcut, becomes useless when the original file is deleted or renamed.
Following is an example to create soft link and hard link.
[email@example.com ~]$ ln 66.us.jpg 66.jpg
[firstname.lastname@example.org ~]$ ln -s 66.us.jpg 66s.jpg
[email@example.com ~]$ ls -alh
drwxrwxrwt 16 brucelee brucelee 8.0K 2008-11-11 14:00 .
drwxr-xr-x 23 root root 4.0K 2008-10-16 09:42 ..
-rw-r--r-- 2 brucelee brucelee 26K 2008-11-11 05:07 66.us.jpg
-rw-r--r-- 2 brucelee brucelee 26K 2008-11-11 05:07 66.jpg
lrwxrwxrwx 1 brucelee brucelee 21 2008-11-11 14:00 66s.jpg -> 66.us.jpg
More usage of this command is discussed to simplify the protection of downloadable directories
and files in the subsequent apache section.