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.