How to Remove Files Older than N days using tmpreaper in Linux

by Lakshmanan Ganapathy on October 21, 2013

tmpreaper is a tool to remove files which are not accessed for a certain period of time.

On Linux desktop distros, this is good for cleaning directories like “~/Downloads”, where files get accumulated over a period of time.

On Linux server distros, this is good for cleaning old log files or backup files that are not required any more.

tmpreaper recursively searches and removes files and directories which are not accessed for certain period of time.

WARNING: Before you install tmpreaper and start playing around with it, make sure you understand the implication of running tmpreaper, as it will delete all the files from your system that matches the given criteria. Do NOT run tmpreaper on / (root directory), which might delete critical files that are required to keep your system running. There is no safeguard built into the tmpreaper program to prevent you from running on root directory, as that would make it difficult to use tmpreaper in a chrooted environment.

1. Install tmpreaper

On debian based systems like Ubuntu, use apt-get:

$ sudo apt-get install tmpreaper

On RPM based systems like CentOS and RedHat, use yum:

$ sudo yum -y install tmpreaper

Tmpreaper command syntax:

$ tmpreaper [options]  <time_spec> <dirs>

2. Remove Files which are N Days Older

To remove files which are 5 days older, use “5d” as timespec.

For example, the following command will delete files from the ~/Downloads folder that are not accessed in the last 5 days.

$ tmpreaper 5d ~/Downloads

3. Remove Files which are Not Modified for N Days

By default tmpreaper, will delete files based on “Access Time”. You can use “-m” option to tell tmpreaper to delete files based on “Modification time”.

The following command will delete files which are not modified for 5 days in the Downloads folder.

$ tmpreaper -m 5d ~/Downloads

You can also use the following characters for time_spec parameter

  • d – for days
  • h – for hours
  • m – for minutes
  • s – for seconds

4. Remove Symbolic Links using -s

Use -s option to remove symbolic links also, not just files and directories.

Apart from cleaning up the files and directories, the following command will also clean-up the symbolic links that matches the given time specifications.

$ tmpreaper -s 5h ~/Downloads

5. Remove all File Types using -a Option

Use -a option to remove all type of files, not just regular files, directories, and symbolic links.

$ tmpreaper -a 5m ~/Downloads

6. Do a Dryrun – Test for Deletion using -t Option

Use -t option, to test what files are going to be deleted.

This is very helpful when you are running this against a important directory and you want to exactly what files will be deleted before it really gets deleted.

This option does not remove the files.

$ tmpreaper -t 5d ~/Downloads
(PID 5415) Pretending to clean up directory `/home/lakshmanan/Downloads'.
(PID 5416) Pretending to clean up directory `.tmp_versions'.
(PID 5416) Back from recursing down `.tmp_versions'.

7. Force Delete Files using -f Option

Use -f option to force delete files. Normally files owned by current user (EUID) with no write access are not removed. Using -f will remove those files also.

$ tmpreaper -f 5h ~/Downloads

8. Don’t Delete Files Matching a Pattern using –protect Option

Use –protect ‘<shell_pattern>’ to protect the pattern matching files from deletion.

For example, the following command will delete all files except “.c” files.

$ tmpreaper  --protect '*.c' -t 5h ~/my_prg
Entry matching `--protect' pattern skipped. `hello.c'
Pretending to remove file `./.hello.o'.

9. Using tmpreaper in Cron

By default when you install tmpreaper, it will put an entry in crontab ( /etc/cron.daily/tmpreaper ). It will read the options from /etc/tmpreaper.conf and execute tmpreaper command based on those options.

The /etc/tmpreaper.conf is self explanatory, and easy to understand. By default it will delete files that are 7 days old in /tmp expect some file types. If you plan to use this, then remove the ‘SHOWWARNING=true’ line from the /etc/tmpreaper.conf.

Once the line is removed, tmpreaper will run daily to clean the specified directories without user intervention.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 9 comments… read them below or add one }

1 musab October 22, 2013 at 12:43 am

how to get it on rhel 6

2 Siva October 22, 2013 at 2:12 am

Nice tool…..but anything related to deletion and putting it in crontab makes me more curious…

3 Charly Kuehnast October 22, 2013 at 11:13 am

Delete anything in /tmp that older than 7 days:

/usr/bin/find /tmp/ -type f -mtime +7 -exec rm -f {} \;

4 Cicuta October 22, 2013 at 12:18 pm

One question I have: Why most system administrators do not keep on top of log files and regular files, and in some cases some file names which incite trouble? The best way to avoid future problems is to prevent them from happening; meaning, cleaning up the system and not wait till the file system is at 100% full. Most UNIX systems rotate log files; however, log files which are more than 30 days old should be deleted but keep them on a backup to be used if needed.
The command rm will delete any file with no problem except some weird file names which users might use and rm cannot dele them. If that is the case use the inode command to delete the file with no problem. So, why use any other utility such as tmpreaper when UNIX flavored OS s has it all? In my book, that is asking for trouble.
If you want to delete files more than N number of days old, use the find command together with inode command and problem solved.

5 Clayton Lima October 23, 2013 at 5:31 am

Nice tips! I´m using a CentOS 6.4 distribution with Epel and Rpmforge repositories but they can’t find tmpreaper to install.

Do you can share a file to download?

Thanks in advance!

6 Jacob October 25, 2013 at 2:06 am

tmpwatch achieves the same results in RHEL. It is syntactically same as tmpreaper

7 Cicuta October 25, 2013 at 9:34 am

In UNIX platforms a simple script will do the job automatically. UNIX has been in existence since the early 60s and even then it had all the tools for administration. The problems is that most people now days have become lazy and ignorant and want to do things with applications which all they do is use the pre-existing UNIX commands an tools, such as cron, in a sophisticated script. So, why not use your own script to automate the system and forget about it. Everything you have mentioned in the article can be done with the UNIX commands and tools which come with the OS. A good UNIX administrator is one who uses command line administration and scripting.

8 michaelA October 30, 2013 at 1:04 am

Cicuta, I believe you’re good sysadmin and I see your point. However I don’t think we need to be such rigid as you propose. It’s good there is something like tmpreaper/tmpwatch, it enhances possibilities how to manage things and provide us just another choice. I’m sure we’d not be very happy using UNIX and it’s command set from sixties. This is evolution, just take it or leave it. Nobody forces you to use new features. So live and let live ;-)

9 Cicuta November 1, 2013 at 2:44 pm

You said it michaelA…To each its own; However, here you are using Linux for who knows how long and, as most Linux users, and do not know that Linux also was derived from AT&T UNIX system V. So you know, Solaris, HP-UNIX, IBM AIX, Digital Ultrix, Linux (All flavors), Berkeley BSD were all derived from AT&T UNIX which was placed in the public domain by Bell Labs (AT&T) in the late 1970s and massaged with features they wanted to include in their UNIX brand. Linux was developed by Linus Torvalds in Helsinki as a hobby to use it with his PC computer after Intel came up with the 386 processor with UNIX capabilities and becoming popular in Europe before the USA. It was also SCO Company here in the US that developed a UNIX like OS and GNU modified Linus Torvalds OS with compilers and editors, such as emacs to become to what Linux is now days. As of myself, I am an engineer, before a system administrator with ample experience in the design of computer centers being my first experience as an administrator in the early 1980s with VAX/VMS and Ultrix and ever since involved with all other flavors of UNIX mentioned above. The reason why I mentioned that with any UNIX like OS a reaper is not needed (unlike Windows) is because the OS has all the tools to do whatever the developer or system administrator wants to do with the commands and utilities UNIX, such as Linux, has to offer. If you like to use a reaper is your choice; however, what would you do if the OS becomes corrupted and cannot use the reaper in single user mode? Then, the only choice is to know the OS very well and use the command line mode to fix the problem. I am not by any means against knew utilities in any flavor of UNIX but my favorite one is the command line which is very powerful if you know how to use it and of course scripting to automate the system.
Any reaper has been developed using the commands the OS offers, including Windows; however, UNIX like OSs are mostly used by industry companies and system administrators have been dealing with problems such as deleting off the wall files for a very long time without the use of reapers. Where I see a good use for a reaper is with Windows and Mac System X as most users are not system’s administrators and do not know the guts of the OS and hence do not know how to delete files which have become corrupted.

Leave a Comment

Previous post:

Next post: