-->
Page 299
The stat command will give you a lot of information about a file or directory. Much of this information is technical, but you may find the stat command useful for checking symbolic links. The stat command does not have any command-line options, but you can specify directories or filenames, for example:
# stat . File: "." Size: 2048 Filetype: Directory Mode: (0775/drwxrwxr-x) Uid: ( 500/ bball) Gid: ( 500/ bball) Device: 3,3 Inode: 91932 Links: 19 Access: Wed Nov 19 17:29:42 1997(00000.00:04:46) Modify: Wed Nov 19 16:52:12 1997(00000.00:42:16) Change: Wed Nov 19 16:52:12 1997(00000.00:42:16)
You can see that the stat command shows who the file or directory belongs to, along with permissions, and other information. If you want to use stat to check symbolic links, you can try
# touch file1 # ln -s file1 file2 # rm file1 rm: remove `file1'? y # stat file2 Can't stat file2
As a beginning sysadmin, you can use this knowledge to devise an even better approach to help stat look at file directories recursively. There are a number of ways to do this. Here's one handy command line you can use to check all the symbolic links in your Linux filesystem:
# find / -xdev | xargs stat | fgrep "Can't stat"
This command line uses the find command to feed pathnames to the stat command (through the xargs command), and then the fgrep command to print all matches when stat can't find a symbolic link. As you become a more proficient sysadmin, you'll devise your own bag of tricks to help diagnose your system.
This section gives you some tips on saving disk space. Part of being a sysadmin is maintaining the health of your filesystem, and performing cleanup operations occasionally to free up disk space. Once you've found some techniques or approaches that work for your system and the way you work on your system, you'll find that you can routinely trim and recover megabytes of disk space.
One good way to save disk space is not to install a lot of software. For example, how many different word processors do you need? How many graphics programs do you need? If you find a capable program, delete others that do the same thing. Make sure to read Hour 22
Page 300
to see how the rpm command can help you free up disk space and customize your Linux installation by deleting packages of programs and supporting software.
You can often trim the size of your directories by looking for less often used programs, or collections of graphics or text you don't need. You've already seen one way to find directories that may be candidates for cleanup. But you should also consider some other file types that may more obviously be deleted.
For example, some Linux programs create backup files with the tilde prefix. You can search for these as follows:
# find / -name ~* -xdev
Once you feel comfortable with the results, you can pipe the filenames into the rm command, using the xargs command to build a cleanup command line (although you can also use the find command's -exec command-line option):
# find / -name ~* -xdev | xargs rm -f
You should also search for files named core. These are core dumps, or dumps of program memory created if a program abnormally aborts. Some of these files can be huge, for example:
# ls -l /usr/lib/rhs/glint/core -rw------ 1 root root 10768384 Sep 16 15:26 /usr/lib/rhs/glint/core
You can use the bash or pdksh shells' ulimit command to limit the size of these core files. To see the current allowable size of core files, use the ulimit command's -c option, for example:
# ulimit -c 1000000
This doesn't mean that core dumps are limited to 1MB, but 1,000,000 512-byte blocks! You can limit the size of core files with the -c option, as in
# ulimit -c 1000
This sets the maximum size of core files to 512,000 bytes. If you're using the csh shell, use
# limit coredumpsize 1000
Limiting the size of these files is one way to can save disk space in the future. Other candidates for removal include
*.bak, *.BAK | Backup files |
*.o | Compiled object files from compiling operations |
#* | Backup files |
*.1, *.2, *.3 | File extensions for system log files under the /var/log directory. |
Page 301
Some logs can grow quite large, and unless you really need them, they should be deleted.
CAUTION |
Be careful about deleting files with file-number extensions, such as .1, across your system: you will delete a lot of libraries and manual pages if you automate a search-and-destroy mission starting at the root, or /, directory! |
If you feel uncomfortable with letting the find command run through your file system and deleting files, just have the command generate a report of candidate files as follows:
# find / \( -name core -o -name *.o \) -xdev > deletelist.txt
Note that you can also set this command to run unattended, at regular intervals, and have the report emailed to you as an automatic reminder. See Hour 24, "Scheduling," for details.
If you experiment with your own combination of commands, you'll soon come up with your own customized reports and cleanup actions. You now know how to get information about your drive space. You'll also learn about managing disk space, using quotas (software limits on hard drive usage) in the "Managing User Access" section. For now, you'll learn how to find out more about what's going on in your computer's memory when you run Linux.
Although the industry trend has been to offer more hard drive space and more memory for less money, many people don't like to outlay more cash to expand their systems. The good news is that Linux is very efficient at using memory, because even a 16MB system provides enough room (with an equal amount of swap space) to run X11 and most programs well. The bad news is that programs are getting larger all the time, especially with feature creep, in which more and more functions creep into programs. This section introduces you to some programs you may find helpful in understanding your system's memory, and gives you some tips on conserving memory.
The free command shows breakdowns of the amounts and totals of free and used memory, including your swapfile usage. This command has several command-line options, but is easy to run and understand, for example: