System Administration Guide

Finding and Removing Old and Inactive Files

Part of the job of cleaning up heavily loaded file systems involves locating and removing files that have not been used recently. You can locate unused files using the ls or find commands. For more information, see ls(1) and find(1).

Other ways to conserve disk space include emptying temporary directories such as the ones located in /var/tmp or /var/spool, and deleting core and crash dump files. For more information about these files, refer to Chapter 69, Generating and Saving System Crash Information.

How to List the Newest Files

List files, displaying the most recently created or changed files first, by using the ls -t command.


$ ls -t [directory]

-t  

Sorts listings by latest time stamp first. 

directory

Directory you want to search. 

Example--Listing the Newest Files

The following example shows how to use ls -t to locate the most recent files within the /var/adm directory. sulog, messages, utmpx, wtmpx, utmp, and lastlog were created or edited most recently. This is verified using output from ls -l, which shows that these three files were created or edited in March, while the other files in /var/spool were created or edited earlier.


$ ls -t /var/adm
sulog      wtmpx        wtmp        messages.1  vold.log   spellhist
messages   utmp         sa          messages.2  log        aculog
utmpx      lastlog      messages.0  messages.3  acct       passwd
$ ls -l /var/adm
total 686
drwxr-xr-x  5 adm       adm         512 Feb 13 16:20 acct
-rw-------  1 uucp      bin           0 Feb 13 16:04 aculog
-r--r--r--  1 root      other      8456 Mar 27 10:34 lastlog
drwxr-xr-x  2 adm       adm         512 Feb 13 16:36 log
-rw-r--r--  1 root      other    117376 Mar 27 13:11 messages
-rw-r--r--  1 root      other      4620 Jan 30 08:30 messages.0
-rw-r--r--  1 root      other     11176 Jan 23 04:30 messages.1
-rw-r--r--  1 root      other        60 Jan 13 09:45 messages.2
-rw-r--r--  1 root      other         0 Jan 31 04:05 messages.3
drwxr-xr-x  2 adm       adm         512 Feb 13 16:03 passwd
drwxr-xr-x  2 adm       sys         512 Mar 20 06:59 sa
-rw-rw-rw-  1 bin       bin           0 Feb 13 16:04 spellhist
-rw-------  1 root      root       1647 Mar 27 13:28 sulog
-rw-r--r--  1 root      bin         504 Mar 27 10:34 utmp
-rw-r--r--  1 root      bin        5208 Mar 27 10:34 utmpx
-rw-rw-rw-  1 root      root        500 Jan 11 14:40 vold.log
-rw-rw-r--  1 adm       adm       14724 Mar 27 10:34 wtmp
-rw-rw-r--  1 adm       adm      151404 Mar 27 10:34 wtmpx

How to Find and Remove Old or Inactive Files

  1. Become superuser.

  2. Find files that have not been accessed for a specified number of days and list them in a file.


    # find directory  -type f [-atime +nnn] [-mtime +nnn] -print > filename
    

    directory

    Directory you want to check. Directories below this also will be checked. 

    -atime +nnn

    Finds files that have not been accessed within the number of days you specify. 

    -mtime +nnn

    Finds files that have not been modified within the number of days you specify. 

    filename

    File containing the list of inactive files.

  3. Remove the inactive files that you listed in the previous step.


    # rm `cat filename`
    

    filename

    File created by this command which contains the list of inactive files. 

Example--Finding and Removing Old or Inactive Files

The following example locates regular files in /var/adm and its directories that have not been accessed in the last 60 days and saves the list of inactive files in /var/tmp/deadfiles. These files are then removed with the rm command.


# find /var/adm -type f -atime +60 -print > /var/tmp/deadfiles &
# more /var/tmp/deadfiles
/var/adm/log/asppp.log
/var/adm/aculog
/var/adm/spellhist
/var/adm/wtmp
/var/adm/wtmpx
/var/adm/sa/sa13
/var/adm/sa/sa27
/var/adm/sa/sa11
/var/adm/sa/sa23
/var/adm/sulog
/var/adm/vold.log
/var/adm/messages.1
/var/adm/messages.2
/var/adm/messages.3
# rm `cat /var/tmp/deadfiles`

How to Clear Out Temporary Directories

  1. Become superuser.

  2. Change to the /var/tmp directory.


    # cd /var/tmp
    

    Caution - Caution -

    Be sure you are in the right directory before completing the following step. The next step deletes all files in the current directory.


  3. Delete the files and subdirectories in the current directory.


    # rm -r *
    
  4. Change to other directories containing temporary or obsolete subdirectories and files (for example, mail, lost+found, or quotas), and delete them by repeating Step 3 above.

Example--Clearing Out Temporary Directories

The following example shows how to clear out the /var/tmp directory, and verifies that all files and subdirectories were removed.


# cd /var/tmp
# ls
deadfiles           wxconAAAa0003r:0.0  wxconAAAa000NA:0.0
test_dir            wxconAAAa0003u:0.0  wxconAAAa000cc:0.0 
wxconAAAa000zs:0.0
# rm -r *
# ls
#

How to Find and Delete core Files

  1. Become superuser.

  2. Change the directory to where you want to start the search.

  3. Find and remove any core files in this directory and its subdirectories.


    # find . -name core -exec rm {} \;
    

Example--Finding and Deleting core Files

The following example shows how to find and remove core files from the user account belonging to jones using the find command.


# cd /home/jones
# find . -name core -exec rm {} \;
 

How to Delete Crash Dump Files

Crash dump files can be very large, so if you have enabled your system to store these files, do not retain them for longer than necessary.

  1. Become superuser.

  2. Change to the directory where crash dump files are stored.


    # cd /var/crash/system
    

    system

    System that created the crash dump files. 


    Caution - Caution -

    Be sure you are in the right directory before completing the following step. The next step deletes all files in the current directory.


  3. Remove the crash dump files.


    # rm *
    
  4. Verify the crash dump files are removed.


    # ls
    

Example--Deleting Crash Dump Files

The following example shows how to remove crash dump files from the system venus, and how to verify that the crash dump files were removed.


# cd /var/crash/venus
# rm *
#