Go to main content

Managing Devices in Oracle® Solaris 11.3

Exit Print View

Updated: April 2018
 
 

Removing Old or Inactive Files

With the ls –t command, you can generate a list of files in a directory and sort them according to their respective time stamps. By default, the files will be listed from the newest files to the oldest. The following example lists the files in /var/adm starting from the most recent file.

$ ls -t /var/adm
total 134
-rw-------   1 root     root         315 Sep 24 14:00 sulog
-r--r--r--   1 root     other     350700 Sep 22 11:04 lastlog
-rw-r--r--   1 root     bin         4464 Sep 22 11:04 utmpx
-rw-r--r--   1 adm      adm        20088 Sep 22 11:04 wtmpx
-rw-r--r--   1 root     root       11510 Sep 10 16:13 messages.1
drwxrwxr-x   5 adm      adm          512 Sep 10 15:19 acct
drwxrwxr-x   2 adm      sys          512 Sep 10 15:19 sa
drwxr-xr-x   2 adm      adm          512 Sep 10 15:17 log

You can then remove old files that you determine as no longer needed.

How to Find Old or Inactive Files for Deletion

The find command enables you to search for files within a defined time range that you can mark for deletion.

  1. Become an administrator.

    For more information, see Using Your Assigned Administrative Rights in Securing Users and Processes in Oracle Solaris 11.3.

  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 > output-file &
    directory

    Identifies the directory you want to search. Subdirectories below are also searched.

    –atime +nnn

    Specifies the number of days that files that have not been accessed.

    –mtime +nnn

    Specifies the number of days that files have not been modified.

    output-file

    Refers to the file to which the output of the command will be written.

  3. Ensure that the list of files in the output file can be safely removed.

    If some files in the list are still needed, remove their filenames from the output file.

  4. Remove the files that are listed in the output file.
    # rm `cat output-file`
Example 30  Finding and Removing Old or Inactive Files

The following example shows files in the /var/adm directory and the subdirectories that have not been accessed in the last 60 days. The /var/tmp/deadfiles file contains the list of inactive files. The rm command removes these inactive files.

# find /var/adm -type f -atime +60 -print > /var/tmp/deadfiles &
# more /var/tmp/deadfiles
/var/adm/aculog
/var/adm/spellhist
/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`
#