old-www/LDP/www.debian.org/doc/manuals/debian-tutorial/ch-advanced.html

1034 lines
36 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>Debian Tutorial (Obsolete Documentation) - Advanced topics</title>
<link href="index.html" rel="start">
<link href="ch-troubleshooting.html" rel="prev">
<link href="ch-next.html" rel="next">
<link href="index.html#contents" rel="contents">
<link href="index.html#copyright" rel="copyright">
<link href="ch-about.html" rel="chapter" title="1 About this manual">
<link href="ch-introduction.html" rel="chapter" title="2 Introduction">
<link href="ch-start.html" rel="chapter" title="3 Getting started">
<link href="ch-basics.html" rel="chapter" title="4 The Basics">
<link href="ch-docs.html" rel="chapter" title="5 Reading documentation and getting help">
<link href="ch-shell.html" rel="chapter" title="6 Using the shell">
<link href="ch-files.html" rel="chapter" title="7 More on files">
<link href="ch-editor.html" rel="chapter" title="8 Creating and editing text files">
<link href="ch-custom_shell.html" rel="chapter" title="9 Customizing the shell">
<link href="ch-X.html" rel="chapter" title="10 The X Window System">
<link href="ch-text_tools.html" rel="chapter" title="11 Text tools">
<link href="ch-file_tools.html" rel="chapter" title="12 File tools">
<link href="ch-disks.html" rel="chapter" title="13 Using disks">
<link href="ch-dpkg.html" rel="chapter" title="14 Removing and installing software">
<link href="ch-troubleshooting.html" rel="chapter" title="15 Troubleshooting">
<link href="ch-advanced.html" rel="chapter" title="16 Advanced topics">
<link href="ch-next.html" rel="chapter" title="17 Where to from here?">
<link href="ch-helping.html" rel="chapter" title="18 Contributing to Debian: How can I help?">
<link href="ap-apps_appendix.html" rel="appendix" title="A A brief survey of available applications">
<link href="ap-components_appendix.html" rel="appendix" title="B Summary of GNU/Linux system components">
<link href="ap-booting_appendix.html" rel="appendix" title="C Appendix C: Booting the system">
<link href="ap-misc_appendix.html" rel="appendix" title="D Miscellaneous">
<link href="ch-about.html#s-acknowldgements" rel="section" title="1.1 Acknowledgements">
<link href="ch-introduction.html#s-introduction-debian" rel="section" title="2.1 What is Debian?">
<link href="ch-introduction.html#s-introduction-DFSG" rel="section" title="2.2 What's free software?">
<link href="ch-introduction.html#s-introduction-how" rel="section" title="2.3 How to Read This Book">
<link href="ch-introduction.html#s-introduction-ldp" rel="section" title="2.4 The Linux Documentation Project">
<link href="ch-start.html#s-start-multi" rel="section" title="3.1 A multiuser, multitasking operating system">
<link href="ch-start.html#s-start-login" rel="section" title="3.2 Logging in">
<link href="ch-start.html#s-start-keys" rel="section" title="3.3 Keys">
<link href="ch-start.html#s-start-commandline" rel="section" title="3.4 Command history and editing the command line">
<link href="ch-start.html#s-start-root" rel="section" title="3.5 Logging in as root">
<link href="ch-start.html#s-start-VC" rel="section" title="3.6 Virtual consoles">
<link href="ch-start.html#s-start-shutdown" rel="section" title="3.7 Shutting down">
<link href="ch-basics.html#s-basics-commandline" rel="section" title="4.1 The command line and <samp>man</samp> pages">
<link href="ch-basics.html#s-basics-files" rel="section" title="4.2 Files and Directories">
<link href="ch-basics.html#s-basics-processes" rel="section" title="4.3 Processes">
<link href="ch-basics.html#s-basics-shell" rel="section" title="4.4 The shell">
<link href="ch-basics.html#s-basics-jobs" rel="section" title="4.5 Managing processes with Bash">
<link href="ch-basics.html#s-basics-bash" rel="section" title="4.6 A few Bash features">
<link href="ch-basics.html#s-basics-identity" rel="section" title="4.7 Managing your identity">
<link href="ch-docs.html#s-docs-sources" rel="section" title="5.1 Kinds of documentation">
<link href="ch-docs.html#s-docs-info" rel="section" title="5.2 Using info">
<link href="ch-docs.html#s-docs-textfiles" rel="section" title="5.3 Viewing text files with more and less">
<link href="ch-docs.html#s-docs-howtos" rel="section" title="5.4 HOWTOs">
<link href="ch-docs.html#s-docs-support" rel="section" title="5.5 Getting help from a person">
<link href="ch-docs.html#s-docs-sysinfo" rel="section" title="5.6 Getting information from the system">
<link href="ch-shell.html#s-shell-variables" rel="section" title="6.1 Environment variables">
<link href="ch-shell.html#s-shell-path" rel="section" title="6.2 Where commands live: the <samp>PATH</samp> variable">
<link href="ch-shell.html#s-custom_shell-aliases" rel="section" title="6.3 Aliases and shell functions">
<link href="ch-shell.html#s-shell-io" rel="section" title="6.4 Controlling input and output">
<link href="ch-shell.html#s-shell-modifiers" rel="section" title="6.5 Specifying how and when to run commands">
<link href="ch-shell.html#s-shell-wildcards" rel="section" title="6.6 Filename expansion (&quot;Wildcards&quot;)">
<link href="ch-shell.html#s6.7" rel="section" title="6.7 Interactive/non-interactive">
<link href="ch-files.html#s-files-permissions" rel="section" title="7.1 Permissions">
<link href="ch-files.html#s-files-tour" rel="section" title="7.2 What files are on my system? Where can I put my own files?">
<link href="ch-files.html#s-file-manager" rel="section" title="7.3 Using a filemanager">
<link href="ch-editor.html#s-editor-textfile" rel="section" title="8.1 What's a text file?">
<link href="ch-editor.html#s-editor-editor" rel="section" title="8.2 Text editors">
<link href="ch-editor.html#s-editor-vi" rel="section" title="8.3 Creating and editing a text file with <code>vi</code>">
<link href="ch-custom_shell.html#s-custom_shell-dotfiles" rel="section" title="9.1 .rc files and <samp>ls -a</samp>">
<link href="ch-custom_shell.html#s-custom_shell-etc" rel="section" title="9.2 System-wide vs. user-specific configuration">
<link href="ch-X.html#s-X-starting" rel="section" title="10.1 Starting the X environment">
<link href="ch-X.html#s-X-intro" rel="section" title="10.2 Intro: What is X">
<link href="ch-X.html#s-X-basics" rel="section" title="10.3 Basic X operations">
<link href="ch-X.html#s-X-startup" rel="section" title="10.4 Customizing your X startup">
<link href="ch-text_tools.html#s-text_tools-regexps" rel="section" title="11.1 Regular expressions">
<link href="ch-file_tools.html#s-file_tools-backup" rel="section" title="12.1 Backup tools">
<link href="ch-file_tools.html#s-file_tools-compression" rel="section" title="12.2 File compression with <samp>gzip</samp>">
<link href="ch-file_tools.html#s-file_tools-split" rel="section" title="12.3 Splitting files into smaller pieces">
<link href="ch-file_tools.html#s-file_tools-find" rel="section" title="12.4 Finding files">
<link href="ch-file_tools.html#s-file_tools-file" rel="section" title="12.5 Determining a file's contents">
<link href="ch-disks.html#s-disks-concepts" rel="section" title="13.1 Concepts">
<link href="ch-disks.html#s-disks-mount" rel="section" title="13.2 <samp>mount</samp> and <samp>/etc/fstab</samp>">
<link href="ch-disks.html#s-network-ppp" rel="section" title="13.3 PPP">
<link href="ch-dpkg.html#s-dpkg-dpkg" rel="section" title="14.1 The <code>dpkg</code> package utility">
<link href="ch-dpkg.html#s-dpkg-what" rel="section" title="14.2 What a package maintenance utility does">
<link href="ch-dpkg.html#s-dpkg-apt" rel="section" title="14.3 Apt">
<link href="ch-dpkg.html#s-dpkg-dselect" rel="section" title="14.4 Using dselect.">
<link href="ch-dpkg.html#s-dpkg-commandline" rel="section" title="14.5 Using dpkg manually">
<link href="ch-dpkg.html#s-dpkg-compiling" rel="section" title="14.6 Compiling software yourself">
<link href="ch-dpkg.html#s-dpkg-proprietary" rel="section" title="14.7 Proprietary software not in .deb format">
<link href="ch-troubleshooting.html#s-troubleshooting-frozen" rel="section" title="15.1 Debian is frozen or crashed!">
<link href="ch-troubleshooting.html#s-troubleshooting-termgarbage" rel="section" title="15.2 My terminal isn't behaving properly">
<link href="ch-troubleshooting.html#s-troubleshooting-vi" rel="section" title="15.3 The computer beeps when I press a key, and my screen shows a text file and/or a lot of <samp>~</samp> symbols.">
<link href="ch-advanced.html#s-advanced-scripting" rel="section" title="16.1 Introduction to shell scripting">
<link href="ch-advanced.html#s-advanced-files" rel="section" title="16.2 Advanced files">
<link href="ch-advanced.html#s-advanced-kernel" rel="section" title="16.3 Compiling the kernel">
<link href="ch-advanced.html#s-advanced-security" rel="section" title="16.4 A few words on security">
<link href="ch-advanced.html#s-advanced-programming" rel="section" title="16.5 Programming on Linux">
<link href="ch-next.html#s-next-manuals" rel="section" title="17.1 Other Debian manuals to read">
<link href="ch-next.html#s-next-resources" rel="section" title="17.2 Other resources">
<link href="ch-helping.html#s-helping-bugs" rel="section" title="18.1 Submit bug reports">
<link href="ch-helping.html#s-helping-other" rel="section" title="18.2 Other things">
<link href="ap-apps_appendix.html#s-A_apps-whatever" rel="section" title="A.1 (Subheadings could copy the structure of the menu system)">
<link href="ap-misc_appendix.html#s-unix-history" rel="section" title="D.1 Unix History">
<link href="ap-misc_appendix.html#sD.2" rel="section" title="D.2 GNU/Linux History">
<link href="ap-misc_appendix.html#sD.3" rel="section" title="D.3 The Linux kernel's version numbering">
<link href="ch-introduction.html#s-introduction-debian-os" rel="subsection" title="2.1.1 What's an operating system, and what sort of operating system is Debian?">
<link href="ch-introduction.html#s-introduction-debian-how" rel="subsection" title="2.1.2 Who creates Debian?">
<link href="ch-basics.html#s-basics-commandline-description" rel="subsection" title="4.1.1 Describing the command line">
<link href="ch-basics.html#s-basics-files-intro" rel="subsection" title="4.2.1 Introduction to files">
<link href="ch-basics.html#s-basics-files-using" rel="subsection" title="4.2.2 Using files: a tutorial">
<link href="ch-basics.html#s-basics-bash-completion" rel="subsection" title="4.6.1 Tab Completion">
<link href="ch-docs.html#s-docs-support-posting" rel="subsection" title="5.5.1 Dos and Don'ts of asking a question">
<link href="ch-shell.html#s-shell-variables-parents" rel="subsection" title="6.1.1 Parent and child processes">
<link href="ch-shell.html#s-interactive-shells" rel="subsection" title="6.7.1 Interactive shells">
<link href="ch-files.html#s-files-permissions-groups" rel="subsection" title="7.1.1 File Ownership">
<link href="ch-files.html#s-files-permissions-mode" rel="subsection" title="7.1.2 Mode">
<link href="ch-files.html#s-files-permissions-tutorial" rel="subsection" title="7.1.3 Permissions in practice">
<link href="ch-editor.html#s-editor-vi-creating" rel="subsection" title="8.3.1 Creating a file">
<link href="ch-editor.html#s-editor-vi-editing" rel="subsection" title="8.3.2 Editing an existing file">
<link href="ch-editor.html#s-editor-vi-editing-movement" rel="subsection" title="8.3.2.1 Moving around in a file">
<link href="ch-editor.html#s-editor-vi-editing-deletion" rel="subsection" title="8.3.2.2 Deleting text">
<link href="ch-editor.html#s-editor-vi-editing-moremoving" rel="subsection" title="8.3.2.3 Sophisticated movement">
<link href="ch-editor.html#s-editor-vi-editing-numbers" rel="subsection" title="8.3.2.4 Repeating commands">
<link href="ch-editor.html#s-editor-vi-editing-advanced" rel="subsection" title="8.3.2.5 Advanced reference">
<link href="ch-X.html#s-X-basics-mouse" rel="subsection" title="10.3.1 The mouse">
<link href="ch-X.html#s-X-basics-clients" rel="subsection" title="10.3.2 X clients">
<link href="ch-X.html#s-X-basics-trouble" rel="subsection" title="10.3.3 Troubleshooting">
<link href="ch-X.html#s-X-basics-leaving" rel="subsection" title="10.3.4 Leaving the X environment">
<link href="ch-disks.html#s-disks-mount-mounting" rel="subsection" title="13.2.1 Mounting a filesystem">
<link href="ch-disks.html#s-disks-mount-cdromexample" rel="subsection" title="13.2.2 Example: Mounting a CD-ROM">
<link href="ch-disks.html#s-disks-mount-fstab" rel="subsection" title="13.2.3 <samp>/etc/fstab</samp>: Automating the mount process">
<link href="ch-disks.html#s-disks-mount-removable" rel="subsection" title="13.2.4 Removable disks (floppies, Zip disks, etc.)">
<link href="ch-disks.html#s-network-ppp-introduction" rel="subsection" title="13.3.1 Introduction">
<link href="ch-disks.html#s-network-ppp-preparation" rel="subsection" title="13.3.2 Preparation">
<link href="ch-disks.html#s-network-ppp-wvdial" rel="subsection" title="13.3.3 The Easy Way: <samp>wvdial</samp>">
<link href="ch-disks.html#s-network-ppp-pppd" rel="subsection" title="13.3.4 Doing It Manually">
<link href="ch-dpkg.html#s-apt-config" rel="subsection" title="14.3.1 Configuring Apt">
<link href="ch-dpkg.html#s-apt-usage" rel="subsection" title="14.3.2 Using apt-get.">
<link href="ch-advanced.html#s-advanced-scripting-why" rel="subsection" title="16.1.1 What and why">
<link href="ch-advanced.html#s-advanced-scripting-example" rel="subsection" title="16.1.2 A simple example">
<link href="ch-advanced.html#s-advanced-files-hardlinks" rel="subsection" title="16.2.1 The real nature of files: hard links and inodes">
<link href="ch-advanced.html#s-advanced-files-types" rel="subsection" title="16.2.2 Types of files">
<link href="ch-advanced.html#s-advanced-files-types-symlinks" rel="subsection" title="16.2.2.1 Symbolic links">
<link href="ch-advanced.html#s-files-misc-types-devices" rel="subsection" title="16.2.2.2 Device files">
<link href="ch-advanced.html#s-advanced-files-types-devices-null" rel="subsection" title="16.2.2.2.1 <samp>/dev/null</samp>">
<link href="ch-advanced.html#s-advanced-files-types-pipes" rel="subsection" title="16.2.2.3 Named pipes (FIFOs)">
<link href="ch-advanced.html#s-advanced-files-types-sockets" rel="subsection" title="16.2.2.4 Sockets">
<link href="ch-advanced.html#s-advanced-files-proc" rel="subsection" title="16.2.3 The <samp>proc</samp> filesystem">
<link href="ch-advanced.html#s-advanced-files-permissions" rel="subsection" title="16.2.4 Advanced aspects of file permissions">
<link href="ch-advanced.html#s-advanced-files-permissions-absolute" rel="subsection" title="16.2.4.1 Using numeric arguments with <samp>chmod</samp>">
<link href="ch-advanced.html#s-advanced-files-chattr" rel="subsection" title="16.2.5 chattr">
<link href="ch-advanced.html#s-advanced-files-copying" rel="subsection" title="16.2.6 Large-scale copying">
<link href="ch-advanced.html#s-advanced-files-undocumented" rel="subsection" title="16.2.7 Other concepts not yet covered, but should be">
</head>
<body>
<p><a name="ch-advanced"></a></p>
<hr>
<p>
[ <a href="ch-troubleshooting.html">previous</a> ]
[ <a href="index.html#contents">Contents</a> ]
[ <a href="ch-about.html">1</a> ]
[ <a href="ch-introduction.html">2</a> ]
[ <a href="ch-start.html">3</a> ]
[ <a href="ch-basics.html">4</a> ]
[ <a href="ch-docs.html">5</a> ]
[ <a href="ch-shell.html">6</a> ]
[ <a href="ch-files.html">7</a> ]
[ <a href="ch-editor.html">8</a> ]
[ <a href="ch-custom_shell.html">9</a> ]
[ <a href="ch-X.html">10</a> ]
[ <a href="ch-text_tools.html">11</a> ]
[ <a href="ch-file_tools.html">12</a> ]
[ <a href="ch-disks.html">13</a> ]
[ <a href="ch-dpkg.html">14</a> ]
[ <a href="ch-troubleshooting.html">15</a> ]
[ 16 ]
[ <a href="ch-next.html">17</a> ]
[ <a href="ch-helping.html">18</a> ]
[ <a href="ap-apps_appendix.html">A</a> ]
[ <a href="ap-components_appendix.html">B</a> ]
[ <a href="ap-booting_appendix.html">C</a> ]
[ <a href="ap-misc_appendix.html">D</a> ]
[ <a href="ch-next.html">next</a> ]
</p>
<hr>
<h1>
Debian Tutorial (Obsolete Documentation)
<br>Chapter 16 - Advanced topics
</h1>
<hr>
<p>
(Should advanced topics be here? I think it would be nice to have some of
these, just to show people the possibilities and give some conceptual
explanation that won't really be in a reference manual. Also it always feels
nice to make it to a chapter called &quot;advanced topics&quot;. Self-esteem
booster for the newbie. :)
</p>
<hr>
<h2><a name="s-advanced-scripting"></a>16.1 Introduction to shell scripting</h2>
<hr>
<h3><a name="s-advanced-scripting-why"></a>16.1.1 What and why</h3>
<p>
Automate simple tasks.
</p>
<hr>
<h3><a name="s-advanced-scripting-example"></a>16.1.2 A simple example</h3>
<p>
Ideas?
</p>
<hr>
<h2><a name="s-advanced-files"></a>16.2 Advanced files</h2>
<hr>
<h3><a name="s-advanced-files-hardlinks"></a>16.2.1 The real nature of files: hard links and inodes</h3>
<p>
Each file on your system is represented by an <em>inode</em> (for Information
Node; pronounced &quot;eye-node&quot;): an inode contains all the information
about the file. However, the inode is not directly visible. Instead, each
inode is linked into the filesystem by one or more <em>hard links</em>. Hard
links contain the name of the file, and the inode number. The inode contains
the file itself, i.e., the location of the information being stored on disk,
its access permissions, the type of the file, and so on. The system can find
any inode once it has the inode number.
</p>
<p>
A single file can have more than one hard link. What this means is that
multiple filenames refer to the same file (that is, they are associated with
the same inode number). However, you can't make hard links across filesystems:
all hard references to a particular file (inode) must be on the same
filesystem. This is because each filesystem has its own set of inodes, and
there can be duplicate inode numbers between filesystems.
</p>
<p>
Since all hard links to a given inode are referring to <em>the same file</em>,
you can make changes to the file, referring to it by one name, and then see
those changes when referring to it by a different name. Try this:
</p>
<ol type="1" start="1" >
<li>
<p>
<samp>cd; echo &quot;hello&quot; &gt; firstlink</samp>
</p>
<p>
<samp>cd</samp> to your home directory and create a file called
<samp>firstlink</samp> containing the word &quot;hello&quot;. What you've
actually done is redirect the output of <samp>echo</samp> (<samp>echo</samp>
just echoes back what you give to it), placing the output in
<samp>firstlink</samp>. See the chapter on shells for a full explanation.
</p>
</li>
</ol>
<ol type="1" start="2" >
<li>
<p>
<samp>cat firstlink</samp>
</p>
<p>
Confirm the contents of <samp>firstlink</samp>.
</p>
</li>
</ol>
<ol type="1" start="3" >
<li>
<p>
<samp>ln firstlink secondlink</samp>
</p>
<p>
Create a hard link: <samp>secondlink</samp> now points to the same inode as
<samp>firstlink</samp>.
</p>
</li>
</ol>
<ol type="1" start="4" >
<li>
<p>
<samp>cat secondlink</samp>
</p>
<p>
Confirm that <samp>secondlink</samp> is the same as <samp>firstlink</samp>
</p>
</li>
</ol>
<ol type="1" start="5" >
<li>
<p>
<samp>ls -l</samp>
</p>
<p>
Notice that the number of hard links listed for <samp>firstlink</samp> and
<samp>secondlink</samp> is 2.
</p>
</li>
</ol>
<ol type="1" start="6" >
<li>
<p>
<samp>echo &quot;change&quot; &gt;&gt; secondlink</samp>
</p>
<p>
This is another shell redirection trick - don't worry about the details. We've
appended the word &quot;change&quot; to <samp>secondlink</samp>. Confirm this
with <samp>cat secondlink</samp>.
</p>
</li>
</ol>
<ol type="1" start="7" >
<li>
<p>
<samp>cat firstlink</samp>
</p>
<p>
<samp>firstlink</samp> also has the word &quot;change&quot; appended! It's
because <samp>firstlink</samp> and <samp>secondlink</samp> refer to <em>the
same file</em>. It doesn't matter what you call it when you change it.
</p>
</li>
</ol>
<ol type="1" start="8" >
<li>
<p>
<samp>chmod a+rwx firstlink</samp>
</p>
<p>
Change permissions on <samp>firstlink</samp>. Do <samp>ls -l</samp> to confirm
that permissions on <samp>secondlink</samp> were also changed. This means that
permissions information is stored in the inode, not in links.
</p>
</li>
</ol>
<ol type="1" start="9" >
<li>
<p>
<samp>rm firstlink</samp>
</p>
<p>
Delete this link. This is a subtlety of <samp>rm</samp> --- it really removes
links, not files. Now type <samp>ls -l</samp> and notice that
<samp>secondlink</samp> is still there. Also notice that the number of hard
links for <samp>secondlink</samp> has been reduced to one.
</p>
</li>
</ol>
<ol type="1" start="10" >
<li>
<p>
<samp>rm secondlink</samp>
</p>
<p>
Delete the other link. When there are no more links to a file, Linux deletes
the file itself, that is, its inode.
</p>
</li>
</ol>
<p>
All files work like this --- even special types of files such as devices (e.g.
<samp>/dev/hda</samp>).
</p>
<p>
A directory is simply a list of filenames and inode numbers, that is, a list of
hard links. When you create a hard link, you're just adding a name-number pair
to a directory. When you delete a file, you're just removing a hard link from
a directory.
</p>
<hr>
<h3><a name="s-advanced-files-types"></a>16.2.2 Types of files</h3>
<p>
One detail we've been concealing up to now is that the Linux kernel considers
nearly everything to be a file. That includes directories and devices: they're
just special kinds of files.
</p>
<p>
As you may remember, the first character of an <samp>ls -l</samp> display
represents the type of the file. For an ordinary file, this will be simply
<samp>-</samp>. Other possibilities are:
</p>
<ul>
<li>
<p>
<samp>d</samp> (directory)
</p>
</li>
</ul>
<ul>
<li>
<p>
<samp>l</samp> (symbolic link)
</p>
</li>
</ul>
<ul>
<li>
<p>
<samp>b</samp> (block device)
</p>
</li>
</ul>
<ul>
<li>
<p>
<samp>c</samp> (character device)
</p>
</li>
</ul>
<ul>
<li>
<p>
<samp>p</samp> (named pipe)
</p>
</li>
</ul>
<ul>
<li>
<p>
<samp>s</samp> (socket)
</p>
</li>
</ul>
<hr>
<h4><a name="s-advanced-files-types-symlinks"></a>16.2.2.1 Symbolic links</h4>
<p>
Symbolic links (also called symlinks or soft links) are the other kind of link
besides hard links. A symlink is a special file that &quot;points to&quot; a
hard link on any mounted filesystem. When you try to read the contents of a
symlink, it gives the contents of the file it's pointing to rather than the
contents of the symlink itself. Since directories, devices, and other symlinks
are types of files, you can point a symlink at any of those things.
</p>
<p>
So a hard link is a filename and an inode number. A file is really an inode: a
location on disk, file type, permissions mode, etc. A symlink is an inode that
contains the name of a hard link. A symlink pairs one filename with a second
filename, while a hard link pairs a filename with an inode number.
</p>
<p>
All hard links to the same file have equal status. That is, one is as good as
the other; if you perform any operation on one it's just the same as performing
that operation on any of the others. This is because the hard links all refer
to the same inode. Operations on symlinks, on the other hand, sometimes affect
the symlink's own inode (the one containing the name of a hard link) and
sometimes affect the hard link being pointed to.
</p>
<p>
There are a number of important differences between symlinks and hard links:
</p>
<ul>
<li>
<p>
Symlinks can cross filesystems. This is because they contain complete
filenames, starting with the root directory, and all complete filenames are
unique. Since hard links point to inode numbers, and inode numbers are unique
only within a single filesystem, they would be ambiguous if the filesystem
wasn't known.
</p>
</li>
</ul>
<ul>
<li>
<p>
You can make symlinks to directories, but you can't make hard links to them.
Each directory has hard links --- its listing in its parent directory, its
<samp>.</samp> entry, and the <samp>..</samp> entry in each of its
subdirectories --- but to impose order on the filesystem, no other hard links
to directories are allowed. Consequently, the number of files in a directory
is equal to the number of hard links to that directory minus two (you subtract
the directory's name and the <samp>.</samp> link).
</p>
</li>
</ul>
<ul>
<li>
<p>
You can only make a hard link to a file that exists, because there must be an
inode number to refer to. However, you can make a symlink to any filename,
whether or not there actually is such a filename.
</p>
</li>
</ul>
<ul>
<li>
<p>
Removing a symlink removes only the link. It has no effect on the linked-to
file. Removing the only hard link to a file removes the file.
</p>
</li>
</ul>
<p>
Try this:
</p>
<ol type="1" start="1" >
<li>
<p>
<samp>cd; ln -s /tmp/me MyTmp</samp>
</p>
<p>
<samp>cd</samp> to your home directory. <samp>ln</samp> with the
<samp>-s</samp> option makes a symbolic link; in this case, one called
<samp>MyTmp</samp> which points to the filename <samp>/tmp/me</samp>.
</p>
</li>
</ol>
<ol type="1" start="2" >
<li>
<p>
<samp>ls -l MyTmp</samp>
</p>
<p>
Output should look like this:
</p>
<pre>
lrwxrwxrwx 1 havoc havoc 7 Dec 6 12:50 MyTmp -&gt; /tmp/me
</pre>
<p>
The date and user/group names will be different for you, of course. Notice
that the file type is <samp>l</samp>, indicating that this is a symbolic link.
Also notice the permissions - symbolic links always have these permissions. If
you attempt to <samp>chmod</samp> a symlink, you'll actually change the
permissions on the file being pointed to.
</p>
</li>
</ol>
<ol type="1" start="3" >
<li>
<p>
<samp>chmod 700 MyTmp</samp>
</p>
<p>
You will get a &quot;No such file or directory&quot; error, because the file
<samp>/tmp/me</samp> doesn't exist. Notice that you could create a symlink to
it anyway.
</p>
</li>
</ol>
<ol type="1" start="4" >
<li>
<p>
<samp>mkdir /tmp/me</samp>
</p>
<p>
Create the directory <samp>/tmp/me</samp>.
</p>
</li>
</ol>
<ol type="1" start="5" >
<li>
<p>
<samp>chmod 700 MyTmp</samp>
</p>
<p>
Should work now.
</p>
</li>
</ol>
<ol type="1" start="6" >
<li>
<p>
<samp>touch MyTmp/myfile</samp>
</p>
<p>
Create a file in <samp>MyTmp</samp>.
</p>
</li>
</ol>
<ol type="1" start="7" >
<li>
<p>
<samp>ls /tmp/me</samp>
</p>
<p>
The file was actually created in <samp>/tmp/me</samp>.
</p>
</li>
</ol>
<ol type="1" start="8" >
<li>
<p>
<samp>rm MyTmp</samp>
</p>
<p>
Remove the symbolic link. Notice that this removes the link, not what it
points to. Thus you use <samp>rm</samp> not <samp>rmdir</samp>.
</p>
</li>
</ol>
<ol type="1" start="9" >
<li>
<p>
<samp>rm /tmp/me/myfile; rmdir /tmp/me</samp>
</p>
<p>
Clean up after ourselves.
</p>
</li>
</ol>
<hr>
<h4><a name="s-files-misc-types-devices"></a>16.2.2.2 Device files</h4>
<p>
Device files refer to physical or virtual devices on your system, such as your
hard disk, video card, screen, or keyboard. An example of a virtual device is
the console, represented by <samp>/dev/console</samp>.
</p>
<p>
There are two kinds of devices: <em>character devices</em> can be accessed one
character at a time, that is, the smallest unit of data which can be written to
or read from the device is a character (byte).
</p>
<p>
<em>Block devices</em> must be accessed in larger units called blocks, which
contain a number of characters. Your hard disk is a block device.
</p>
<p>
You can read and write device files just as you can from other kinds of files,
though the file may well contain some strange incomprehensible-to-humans
gibberish. Writing random data to these files is probably a Bad Idea.
Sometimes it's useful, though: for example, you can dump a postscript file into
the printer device <samp>/dev/lp0</samp>, or send modem commands to the device
file for the appropriate serial port.
</p>
<hr>
<h5><a name="s-advanced-files-types-devices-null"></a>16.2.2.2.1 <samp>/dev/null</samp></h5>
<p>
<samp>/dev/null</samp> is a special device file that discards anything you
write to it. If you don't want something, throw it in <samp>/dev/null</samp>.
It's essentially a bottomless pit. If you read <samp>/dev/null</samp>, you'll
get an end-of-file (EOF) character immediately. <samp>/dev/zero</samp> is
similar, only if you read from it you get the <samp>\0</samp> character (not
the same as the number zero).
</p>
<hr>
<h4><a name="s-advanced-files-types-pipes"></a>16.2.2.3 Named pipes (FIFOs)</h4>
<p>
A named pipe is a file that acts like a pipe. You put something into the file,
and it comes out the other end. Thus it's called a FIFO, or
First-In-First-Out: the first thing you put in the pipe is the first thing to
come out the other end.
</p>
<p>
If you write to a named pipe, the process which is writing to the pipe doesn't
terminate until the information being written is read from the pipe. If you
read from a named pipe, the reading process waits until there's something to
read before terminating. The size of the pipe is always zero --- it doesn't
store data, it just links two processes like the shell <samp>|</samp>.
However, since this pipe has a name, the two processes don't have to be on the
same command line or even be run by the same user.
</p>
<p>
You can try it by doing the following:
</p>
<ol type="1" start="1" >
<li>
<p>
<samp>cd; mkfifo mypipe</samp>
</p>
<p>
Makes the pipe.
</p>
</li>
</ol>
<ol type="1" start="2" >
<li>
<p>
<samp>echo &quot;hello&quot; &gt; mypipe &amp;</samp>
</p>
<p>
Puts a process in the background which tries to write &quot;hello&quot; to the
pipe. Notice that the process doesn't return from the background; it is
waiting for someone to read from the pipe.
</p>
</li>
</ol>
<ol type="1" start="3" >
<li>
<p>
<samp>cat mypipe</samp>
</p>
<p>
At this point the <samp>echo</samp> process should return, since
<samp>cat</samp> read from the pipe, and the <samp>cat</samp> process will
print <samp>hello</samp>.
</p>
</li>
</ol>
<ol type="1" start="4" >
<li>
<p>
<samp>rm mypipe</samp>
</p>
<p>
You can delete pipes just like any other file.
</p>
</li>
</ol>
<hr>
<h4><a name="s-advanced-files-types-sockets"></a>16.2.2.4 Sockets</h4>
<p>
Sockets are similar to pipes, only they work over the network. This is how
your computer does networking: you may have heard of &quot;WinSock&quot;, which
is sockets for Windows.
</p>
<p>
We won't go into these further, because you probably won't have occasion to use
them unless you're programming. However, if you see a file marked with type
<samp>s</samp> on your computer, you know what it is.
</p>
<hr>
<h3><a name="s-advanced-files-proc"></a>16.2.3 The <samp>proc</samp> filesystem</h3>
<p>
The Linux kernel makes a special filesystem available, which is mounted under
<samp>/proc</samp> on Debian systems. This is a &quot;pseudo-filesystem&quot;
--- it doesn't really exist on any of your physical devices.
</p>
<p>
The <samp>proc</samp> filesystem contains information about the system and
running processes. Some of the &quot;files&quot; in <samp>/proc</samp> are
reasonably understandable to humans (try typing <samp>cat /proc/meminfo</samp>
or <samp>cat /proc/cpuinfo</samp>) while some others are arcane collections of
numbers. Often, system utilities use these to gather information and present
it to you in a more understandable way.
</p>
<p>
People frequently panic when they notice one file in particular ---
<samp>/proc/kcore</samp> --- which is generally huge. This is (more or less) a
copy of the contents of your computer's memory. It's used to debug the kernel.
It doesn't actually exist anywhere, so don't worry about its size.
</p>
<p>
If you want to know about all the things in <samp>/proc</samp>, type <samp>man
5 proc</samp>.
</p>
<hr>
<h3><a name="s-advanced-files-permissions"></a>16.2.4 Advanced aspects of file permissions</h3>
<hr>
<h4><a name="s-advanced-files-permissions-absolute"></a>16.2.4.1 Using numeric arguments with <samp>chmod</samp></h4>
<p>
Earlier in this chapter, we briefly mentioned that you can set file permissions
using numbers. The numeric notation is called an absolute mode, as opposed to
the symbolic notation (e.g. <samp>u+rx</samp>) which is often called a
relative mode. This is because the number specifies an exact mode to set, and
the symbol just specifies a change to make (e.g. &quot;add user read and
execute permissions&quot;).
</p>
<p>
The numeric mode is a series of four octal digits or twelve binary digits.
Each octal (base eight) digit represents three binary digits: one octal digit
and three binary digits are two ways to represent the decimal digits 0 through
7.
</p>
<p>
Deriving a particular mode is pretty straightforward. You simply add up the
modes you want to combine, or subtract modes you don't want. For example, user
permissions, with only read permission turned on, would be <samp>100</samp> in
binary. User permissions with write only would be <samp>010</samp> binary.
User permissions with read and write both turned on would be <samp>100 + 010 =
110</samp>. Alternatively, you could put it in octal: <samp>4 + 2 = 6</samp>.
</p>
<p>
For the full mode, simply add up digits from this table:
</p>
<pre>
0001 others, execute
0002 others, write
0004 others, read
0010 group, execute
0020 group, write
0040 group, read
0100 user, execute
0200 user, write
0400 user, read
1000 sticky bit
2000 set group id
4000 set user id
</pre>
<p>
To use the table, first decide what permissions you want to set. Then add up
the numbers for those permissions. The total is your mode. For example, to
get mode <samp>0755</samp>:
</p>
<pre>
0001 o=x
0004 o=r
0010 g=x
0040 g=r
0100 u=x
0200 u=w
+ 0400 u=r
-------
0755 u=rwx go=rw
</pre>
<p>
You'd actually call this mode simply <samp>755</samp>, without the leading
<samp>0</samp>, because <samp>chmod</samp> automatically adds zeroes at the
beginning of the mode --- <samp>7</samp> means mode <samp>0007</samp>.
</p>
<p>
To set a file to <samp>755</samp>, you'd type <samp>chmod 755 myfile</samp>.
</p>
<p>
<samp>755</samp> is a very common mode for directories, as it allows anyone to
use the directory but only the owner to create and delete files in the
directory. <samp>644</samp> is the analogous mode for files, and it is also
very common. It allows anyone to use the file but only the owner can change
it. For executable files, <samp>755</samp> is a common mode; this is just
<samp>644</samp> plus execute permissions (<samp>644 + 111 = 755</samp>).
</p>
<hr>
<h3><a name="s-advanced-files-chattr"></a>16.2.5 chattr</h3>
<p>
A useful tip?
</p>
<hr>
<h3><a name="s-advanced-files-copying"></a>16.2.6 Large-scale copying</h3>
<p>
cp -a and variants on the theme.
</p>
<p>
how to copy an old system to a new one.
</p>
<p>
FIXME whoops, I also listed this topic under Backup Tools. need to decide.
</p>
<hr>
<h3><a name="s-advanced-files-undocumented"></a>16.2.7 Other concepts not yet covered, but should be</h3>
<p>
fsck, dd, fdisk, etc.
</p>
<p>
what package is a file in?
</p>
<p>
MSDOS vs. Mac vs. Unix text files
</p>
<p>
sync
</p>
<hr>
<h2><a name="s-advanced-kernel"></a>16.3 Compiling the kernel</h2>
<p>
How, what, and why
</p>
<hr>
<h2><a name="s-advanced-security"></a>16.4 A few words on security</h2>
<p>
The basics of security from a user standpoint. Maintaining one's privacy.
What other users can see of your account.
</p>
<hr>
<h2><a name="s-advanced-programming"></a>16.5 Programming on Linux</h2>
<p>
Something about the Linux programming environment. Aimed at, say, people
taking CS101. Nothing on <em>how</em> to program, just Emacs, gcc, gdb, ddd,
etc. as programming tools.
</p>
<p>
Likely based on debug.tex
</p>
<hr>
<p>
[ <a href="ch-troubleshooting.html">previous</a> ]
[ <a href="index.html#contents">Contents</a> ]
[ <a href="ch-about.html">1</a> ]
[ <a href="ch-introduction.html">2</a> ]
[ <a href="ch-start.html">3</a> ]
[ <a href="ch-basics.html">4</a> ]
[ <a href="ch-docs.html">5</a> ]
[ <a href="ch-shell.html">6</a> ]
[ <a href="ch-files.html">7</a> ]
[ <a href="ch-editor.html">8</a> ]
[ <a href="ch-custom_shell.html">9</a> ]
[ <a href="ch-X.html">10</a> ]
[ <a href="ch-text_tools.html">11</a> ]
[ <a href="ch-file_tools.html">12</a> ]
[ <a href="ch-disks.html">13</a> ]
[ <a href="ch-dpkg.html">14</a> ]
[ <a href="ch-troubleshooting.html">15</a> ]
[ 16 ]
[ <a href="ch-next.html">17</a> ]
[ <a href="ch-helping.html">18</a> ]
[ <a href="ap-apps_appendix.html">A</a> ]
[ <a href="ap-components_appendix.html">B</a> ]
[ <a href="ap-booting_appendix.html">C</a> ]
[ <a href="ap-misc_appendix.html">D</a> ]
[ <a href="ch-next.html">next</a> ]
</p>
<hr>
<p>
Debian Tutorial (Obsolete Documentation)
</p>
<address>
29 Dezember 2009<br>
<br>
Havoc Pennington <code><a href="mailto:hp@debian.org">hp@debian.org</a></code><br>
<br>
</address>
<hr>
</body>
</html>