HomeSolarisSolaris system administrationDelete Solaris Files Older Than a Certain Date and Time

Delete Solaris Files Older Than a Certain Date and Time

Deleting files conditionally based on their age is sometimes useful. This tech-recipe describes a procedure for deleting aged files using the UNIX find command.


The find command has a -newer expression which compares the found files against a reference file. It returns files with a modification time newer than the reference file. To find older files, preceed the -newer expression with the negation operator !.

If you do not have a file to use with an appropriate timestamp, you can create a file with this tech recipe: Create/modify a UNIX file with an arbitrary timestamp.

Given the reference file /tmp/timeref, you can find all older files in the current working directory and beneath using the following command:

find . ! -newer /tmp/timeref -exec ls -l {} \; | more

This command is safe and will only provide a long listing of the target files, showing the files one page at a time. Using this command, you can make sure that the command syntax is correct before performing the irreversible deletion.

Once you are comfortable with the find syntax, you can change to command to cause deletion:

find . ! -newer /tmp/timeref -exec rm {} \;

Be very careful using this command. There is no easy way to undelete files in UNIX. Using the long file list version of this command first provides a great deal of safety since you can see exactly what will happen. Changing to a directory and performing the find command within it using the . as the path provides some additional protection from mistyping the path.

Quinn McHenry
Quinn McHenry
Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!