📄 lsg18.htm
字号:
<P>If you run du on a large directory tree, the output can be very long (and boring to read). You can summarize the information using the -s (summarize) option:
<BR>
<PRE>
<FONT COLOR="#000080">merlin$ du -s /usr/ychow
3315 /usr/ychow</FONT></PRE>
<P>The output with this option includes all subdirectories and the directory being reported. This output is useful for determining the amount disk space each user on the system uses.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>You can easily combine the du command with other commands to generate lists of disk usage by directory. For example, to show a complete list of all directories in order of size, issue the command</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<UL>
<UL>
<BR>
<PRE>
<FONT COLOR="#000080">du / | sort -rn</FONT></PRE></UL></UL>
<P>The du command has several useful options. Most Linux versions support the following options:
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-a, -all
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
This option displays a total count for all files and directories.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-b, -bytes
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
This option displays size in bytes.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-c, -total
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
This option displays a grand total.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-k
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
This option displays the sizes in kilobytes, overriding any environment variable set to 512 bytes.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-l
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
This option displays the size of all files (including links).</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-s
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
This option displays only totals.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-v
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
This option displays version information.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
-x
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
This option ignores directories on another filesystem (mounted into the current filesystem, of course).</FONT>
</TABLE><P>The du command may take a while to generate output if there are a lot of entries to process, especially when run, for example, from the root filesystem on a heavily loaded system. The best use for the du command is in scripts or cronjobs that are run when the system is not heavily loaded.
<BR>
<BR>
<A NAME="E69E134"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B> Making the Most of Your Disk Space</B></FONT></CENTER></H4>
<BR>
<P>When you're running out of disk space, the easiest solutions are to buy another disk, create another Linux partition, or add a remote disk to your system. Presumably, if you can do any of these solutions you will, but sometimes expanding the total amount of disk space is not practical or desirable. Instead, the solution is to manage what you have.
<BR>
<P>As a general rule, disk performance starts to degrade when the system hits 90 percent capacity or more. This system degradation is primarily due to fragmentation of the disk and the heads having move further to access and save files. Many system administrators use about 75 percent capacity as the first warning sign to do something about disk space. You'll develop your own guidelines, but try to avoid running out of disk space; you can find yourself in very awkward circumstances if you do.
<BR>
<P>A good first step to reducing disk space usage is to examine all the applications and software sets loaded on your system and remove the ones you don't use. For example, if you are not using the C compilers you loaded when you installed Linux, you can remove them and free up over 50M of disk space.
<BR>
<P>Another good practice is to scan user areas for users with large disk usage. Tell those users to clean up their areas by deleting or archiving material they don't want or need. In many cases, users keep multiple copies of files around, just in case. Remove the old ones! Get rid of automatic backup files, and clean out large log files. Just cleaning out the system logs can free up 30M on some systems. The primary log files you should look at are the following:
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
/usr/spool/lp/log
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
printing log</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
/usr/lib/cron.log
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
cron log file</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
/usr/spool/uucp/LOGFILE
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
UUCP log file</FONT>
</TABLE><P>These three files can grow to amazing sizes. There are also log files for all the printers, many communications packages, the system, compilers, and other utilities. Check your filesystem for files that grow unreasonably large. Also check mailboxes, which can collect error messages (such as from a bad cron job) and grow to many megabytes in size.
<BR>
<P><A NAME="I2"></A>If you want to keep some of the lines in the log files instead of just deleting them all, use the tail command with the number of lines you want to keep. For example, the following series of commands keeps the first 100 lines of the log file, but deletes the rest:
<BR>
<PRE>
<FONT COLOR="#000080">cd /usr/spool/lp
tail -100 log > tmp
mv tmp log</FONT></PRE>
<P>Next, get in the habit of routinely backing up material you don't need except as archive material. Use floppy disks, a tape drive, or other archive material and stick the data on the shelf instead of on your hard drive. You'll save lots of room by regularly going through your system and cleaning up files. If you really need to keep them on your hard disk, use compress or gzip to shrink the file size noticeably. To find all files that haven't been accessed (read or write) in a certain number of days, use the find command. This command searches for all files older than 120 days and displays them on-screen:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">find / -atime +120 -print</FONT></PRE>
<P>When you have the list of old files, you can consider archiving them.
<BR>
<P>You can write a shell script that searches for and deletes unwanted files, such as core files, .bak (and similar backups for editors and word processors)files, .log files, .error files, and so on. You can create a list of the files you want to regularly remove from your system, embed them in a find command such as the following one, and execute the command to clean out disk space. The following command looks for all files called core and deletes them:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">find / -name core -exec rm {} \;</FONT></PRE>
<P>The find command locates all core files and passes the path to the rm command. The trailing backslash and semicolon are necessary to execute the command properly. There are more elegant (and less CPU-intensive) methods of doing the same task, but this command is a solid, reliable method.
<BR>
<BR>
<A NAME="E68E101"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Understanding Links</B></FONT></CENTER></H3>
<BR>
<P>File links are an oft-misunderstood aspect of filesystems, despite their simplicity. A link, in its simplest form, creates a second filename for a file. For example, if you have the file /usr/bill/testfile and want to have the same file in the /usr/tim directory, you don't have to copy it. Just create a link with the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">ln /usr/bill/testfile /usr/tim/testfile</FONT></PRE>
<P>The format of the command is always the current filename followed by an additional filename, just as with the cp or mv commands.
<BR>
<P>The reason for links is basically twofold, in this example. First, both the file /usr/bill/testfile and /usr/tim/testfile refer to the exact same file, so any changes made by bill or tim are reflected immediately in the other directory (removing the need to copy files every time). Both bill and tim can modify the file, as long as they don't make changes to the file at the same time.
<BR>
<P>The link also gets by file permission and ownership problems. If bill owns the file /usr/bill/testfile and is the only one who can write to it, he can create a link to /usr/tim/testfile and set the ownership of the new link to tim. In this way, both bill and tim can work on the same file despite ownerships and permissions, as each copy has its own ownerships. If set correctly, the ownerships and permissions can prevent anyone other than bill and tim from reading or writing to the file.
<BR>
<P>The ln command, such as in the preceding example, is creating hard links. A hard link is a link in the same filesystem with two i-node table entries pointing to the same physical contents (with the same i-node number because they point to the same data). If you want to see the effect of a link on the i-node table, display the i-node entry for a file in a directory, for example:
<BR>
<PRE>
<FONT COLOR="#000080">$ ls -i testfile
14253 testfile</FONT></PRE>
<P>Then, create a link to another filename and display the i-node entries again:
<BR>
<PRE>
<FONT COLOR="#000080">$ ln testfile test2
$ ls -i testfile test2
14253 testfile 14253 test2</FONT></PRE>
<P>As you can see, both file i-node numbers are the same. A directory listing of the two files shows that they have their own permissions and ownerships. The only thing indicating a link is the second column of the ls output, which shows a two for the number of links. Deleting a linked filename doesn't delete the file until there are no more links to it.
<BR>
<P>A symbolic link is another type of link that doesn't use the i-node entry for the link. You used these links when you were creating device drivers, such as /dev/modem instead of /dev/cua1. The -s option to the ln command creates a symbolic link. For example, you can recreate the preceding example with a symbolic link:
<BR>
<PRE>
<FONT COLOR="#000080">$ ls -i bigfile
6253 bigfile
$ ln -s bigfile anotherfile
$ ls -i bigfile anotherfile
6253 bigfile 8358 anotherfile</FONT></PRE>
<P>As you can see, the i-node table entries are different. A directory listing shows the symbolic link as an arrow:
<BR>
<PRE>
<FONT COLOR="#000080">lrwxrwxrwx 1 root root 6 Sep 16:35 anotherfile -> bigfile
-rw-rw-r-- 1 root root 2 Sep 17:23 bigfile</FONT></PRE>
<P>The file permissions for a symbolic link are always set to lrwxrwxrwx. Permissions for access to the symbolic link name are determined by the permissions and ownership of the file it is symbolically linked to (bigfile in this case).
<BR>
<P>The difference between hard links and symbolic links is more than just i-node table entries. You can create symbolic links to files that don't exist yet, which you can't do with hard links. You can also follow symbolic links to find out what they point to, which is an almost impossible task with hard links. The kernel processes the two types of links differently, too.
<BR>
<BR>
<A NAME="E68E102"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Summary</B></FONT></CENTER></H3>
<BR>
<P>This chapter examined the common disk and filesystem utilities you have available for checking the integrity of the filesystem. It also looked at the basic methods you should use to keep down disk space usage. This chapter also briefly examined links and how you can use both symbolic and hard links to help provide access to some files. Following these simple steps can make your life a lot easier.
<P ALIGN=LEFT>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -