old-www/LDP/intro-linux/html/sect_07_01.html

599 lines
13 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>General good housekeeping</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Introduction to Linux"
HREF="index.html"><LINK
REL="UP"
TITLE="Home sweet /home"
HREF="chap_07.html"><LINK
REL="PREVIOUS"
TITLE="Home sweet /home"
HREF="chap_07.html"><LINK
REL="NEXT"
TITLE="Your text environment"
HREF="sect_07_02.html"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Introduction to Linux: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="chap_07.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 7. Home sweet /home</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="sect_07_02.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="sect_07_01"
></A
>7.1. General good housekeeping</H1
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_07_01_01"
></A
>7.1.1. Introduction</H2
><P
>As we mentioned before, it is easy enough to make a mess of the system. We can't put enough stress on the importance of keeping the place tidy. When you learn this from the start, it will become a good habit that will save you time when programming on a Linux or UNIX system or when confronted with system management tasks. Here are some ways of making life easier on yourself:</P
><P
></P
><UL
><LI
><P
>Make a <TT
CLASS="filename"
>bin</TT
> directory for your program files and scripts.</P
></LI
><LI
><P
>Organize non-executable files in appropriate directories, and make as many directories as you like. Examples include separate directories for images, documents, projects, downloaded files, spreadsheets, personal files, and so on.</P
></LI
><LI
><P
>Make directories private with the <B
CLASS="command"
>chmod <TT
CLASS="parameter"
><I
>700</I
></TT
> <TT
CLASS="filename"
>dirname</TT
></B
> command.</P
></LI
><LI
><P
>Give your files sensible names, such as <TT
CLASS="filename"
>Complaint to the prime minister 050302</TT
> rather than <TT
CLASS="filename"
>letter1</TT
>.</P
></LI
></UL
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_07_01_02"
></A
>7.1.2. Make space</H2
><P
>On some systems, the <B
CLASS="command"
>quota</B
> system may force you to clean up from time to time, or the physical limits of your hard disk may force you to make more space without running any monitoring programs. This section discusses a number of ways, besides using the <B
CLASS="command"
>rm</B
> command, to reclaim disk space.</P
><P
>Run the <B
CLASS="command"
>quota <TT
CLASS="option"
>-v</TT
></B
> command to see how much space is left.</P
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_07_01_02_01"
></A
>7.1.2.1. Emptying files</H3
><P
>Sometimes the content of a file doesn't interest you, but you need the file name as a marker (for instance, you just need the timestamp of a file, a reminder that the file was there or should be there some time in the future). Redirecting the output of a null command is how this is done in the <SPAN
CLASS="application"
>Bourne</SPAN
> and <SPAN
CLASS="application"
>Bash</SPAN
> shells:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>andy:~&#62;</TT
> <B
CLASS="command"
>cat wishlist &#62; placeholder</B
>
<TT
CLASS="prompt"
>andy:~&#62;</TT
> <B
CLASS="command"
>ls -la placeholder</B
>
-rw-rw-r-- 1 andy andy 200 Jun 12 13:34 placeholder
<TT
CLASS="prompt"
>andy:~&#62;</TT
> <B
CLASS="command"
> &#62; placeholder</B
>
<TT
CLASS="prompt"
>andy:~&#62;</TT
> <B
CLASS="command"
>ls -la placeholder</B
>
-rw-rw-r-- 1 andy andy 0 Jun 12 13:35 placeholder
</PRE
></FONT
></TD
></TR
></TABLE
><P
>The process of reducing an existing file to a file with the same name that is 0 bytes large is called <EM
>truncating</EM
>.</P
><P
>For creating a new empty file, the same effect is obtained with the <B
CLASS="command"
>touch</B
> command. On an existing file, <B
CLASS="command"
>touch</B
> will only update the timestamp. See the <SPAN
CLASS="application"
>Info</SPAN
> pages on <B
CLASS="command"
>touch</B
> for more details.</P
><P
>To <SPAN
CLASS="QUOTE"
>"almost"</SPAN
> empty a file, use the <B
CLASS="command"
>tail</B
> command. Suppose user <EM
>andy</EM
>'s wishlist becomes rather long because he always adds stuff at the end but never deletes the things he actually gets. Now he only wants to keep the last five items:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>andy:~&#62;</TT
> <B
CLASS="command"
>tail -5 wishlist &#62; newlist</B
>
<TT
CLASS="prompt"
>andy:~&#62;</TT
> <B
CLASS="command"
>cat newlist &#62; wishlist</B
>
<TT
CLASS="prompt"
>andy:~&#62;</TT
> <B
CLASS="command"
>rm newlist</B
>
</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_07_01_02_02"
></A
>7.1.2.2. More about log files</H3
><P
>Some Linux programs insist on writing all sorts of output in a log file. Usually there are options to only log errors, or to log a minimal amount of information, for example setting the debugging level of the program. But even then, you might not care about the log file. Here are some ways to get rid of them or at least set some limits to their size:</P
><P
></P
><UL
><LI
><P
>Try removing the log file when the program is not running, if you are sure that you won't need it again. Some programs may even see, when restarted, that there is no log file and will therefore not log.</P
></LI
><LI
><P
>If you remove the log file and the program recreates it, read the documentation for this particular program in search for command options that avoid making log files.</P
></LI
><LI
><P
>Try making smaller log files by logging only the information that is relevant to you, or by logging only significant information.</P
></LI
><LI
><P
>Try replacing the log file with a symbolic link to <TT
CLASS="filename"
>/dev/null</TT
>; if you're lucky the program won't complain. Don't do this with the log files of programs that run at system boot or programs that run from cron (see <A
HREF="chap_04.html"
>Chapter 4</A
>). These programs might replace the symbolic link with a small file that starts growing again.</P
></LI
></UL
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_07_01_02_03"
></A
>7.1.2.3. Mail</H3
><P
>Regularly clean out your mailbox, make sub-folders and automatic redirects using <B
CLASS="command"
>procmail</B
> (see the <SPAN
CLASS="application"
>Info</SPAN
> pages) or the filters of your favorite mail reading application. If you have a trash folder, clean it out on a regular basis.</P
><P
>To redirect mail, use the <TT
CLASS="filename"
>.forward</TT
> file in your home directory. The Linux mail service looks for this file whenever it has to deliver local mail. The content of the file defines what the mail system should do with your mail. It can contain a single line holding a fully qualified E-mail address. In that case the system will send all your mail to this address. For instance, when renting space for a website, you might want to forward the mail destined for the webmaster to your own account in order not to waste disk space. The webmaster's <TT
CLASS="filename"
>.forward</TT
> may look like this:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>webmaster@www ~/&#62;</TT
> <B
CLASS="command"
>cat .forward</B
>
mike@pandora.be
</PRE
></FONT
></TD
></TR
></TABLE
><P
>Using mail forwarding is also useful to prevent yourself from having to check several different mailboxes. You can make every address point to a central and easily accessible account.</P
><P
>You can ask your system administrator to define a forward for you in the local mail aliases file, like when an account is being closed but E-mail remains active for a while.</P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_07_01_02_04"
></A
>7.1.2.4. Save space with a link</H3
><P
>When several users need access to the same file or program, when the original file name is too long or too difficult to remember, use a symbolic link instead of a separate copy for each user or purpose.</P
><P
>Multiple symbolic links may have different names, e.g. a link may be called <TT
CLASS="filename"
>monfichier</TT
> in one user's directory, and <TT
CLASS="filename"
>mylink</TT
> in another's. Multiple links (different names) to the same file may also occur in the same directory. This is often done in the <TT
CLASS="filename"
>/lib</TT
> directory: when issuing the command</P
><P
><B
CLASS="command"
>ls <TT
CLASS="option"
>-l</TT
> <TT
CLASS="filename"
>/lib</TT
></B
> </P
><P
>you will see that this directory is plenty of links pointing to the same files. These are created so that programs searching for one name would not get stuck, so they are pointed to the correct/current name of the libraries they need.</P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_07_01_02_05"
></A
>7.1.2.5. Limit file sizes</H3
><P
>The shell contains a built-in command to limit file sizes, <B
CLASS="command"
>ulimit</B
>, which can also be used to display limitations on system resources:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>cindy:~&#62;</TT
> <B
CLASS="command"
>ulimit -a</B
>
core file size (blocks) 0
data seg size (kbytes) unlimited
file size (blocks) unlimited
max locked memory (kbytes) unlimited
max memory size (kbytes) unlimited
open files 1024
pipe size (512 bytes) 8
stack size (kbytes) 8192
cpu time (seconds) unlimited
max user processes 512
virtual memory (kbytes) unlimited
</PRE
></FONT
></TD
></TR
></TABLE
><P
>Cindy is not a developer and doesn't care about core dumps, which contain debugging information on a program. If you do want core dumps, you can set their size using the <B
CLASS="command"
>ulimit</B
> command. Read the <SPAN
CLASS="application"
>Info</SPAN
> pages on <B
CLASS="command"
>bash</B
> for a detailed explanation.</P
><DIV
CLASS="note"
><P
></P
><TABLE
CLASS="note"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Core file?</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>A core file or <EM
>core dump</EM
> is sometimes generated when things go wrong with a program during its execution. The core file contains a copy of the system's memory, as it was at the time that the error occured.</P
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_07_01_02_06"
></A
>7.1.2.6. Compressed files</H3
><P
>Compressed files are useful because they take less space on your hard disk. Another advantage is that it takes less bandwidth to send a compressed file over your network. A lot of files, such as the man pages, are stored in a compressed format on your system. Yet unpacking these to get a little bit of information and then having to compress them again is rather time-consuming. You don't want to unpack a man page, for instance, read about an option to a command and then compress the man page again. Most people will probably forget to clean up after they found the information they needed.</P
><P
>So we have tools that work on compressed files, by uncompressing them only in memory. The actual compressed file stays on your disk as it is. Most systems support <B
CLASS="command"
>zgrep</B
>, <B
CLASS="command"
>zcat</B
>, <B
CLASS="command"
>bzless</B
> and other members of the z-family to prevent unnecessary decompressing/compressing actions. See your system's binary directory and the Info pages.</P
><P
>See <A
HREF="chap_09.html"
>Chapter 9</A
> for more on the actual compressing of files and examples on making archives.</P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="chap_07.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sect_07_02.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Home sweet /home</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="chap_07.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Your text environment</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>