Using Periodic to delete specific files

How do I get periodic to delete files over a month old in a specifc directory?

Is there an existing script that I need to amend or do I need to write something from scratch?
 
Try to set the variables in /etc/periodic.conf to use built-in periodic script.
Code:
daily_clean_tmps_enable="YES"                # Delete stuff daily
daily_clean_tmps_dirs="/tmp /your/specifc/directory"                # Delete under here
daily_clean_tmps_days="30"                # If not accessed for
Sometimes you should not perform /tmp cleanup because of some important temporary files. You can specify only your target directory for cleanup, without /tmp.

If you want to write your own script then try to review built-in examples:
/etc/periodic/daily/110.clean-tmps

It uses some variables in /etc/defaults/periodic.conf
Code:
daily_clean_tmps_enable="NO"                # Delete stuff daily
daily_clean_tmps_dirs="/tmp"                # Delete under here
daily_clean_tmps_days="3"                # If not accessed for
daily_clean_tmps_ignore=".X*-lock .X11-unix .ICE-unix .font-unix .XIM-unix"
daily_clean_tmps_ignore="$daily_clean_tmps_ignore quota.user quota.group .snap"
daily_clean_tmps_ignore="$daily_clean_tmps_ignore .sujournal"
daily_clean_tmps_verbose="YES"                # Mention files deleted

So you can write your own script looking at 110.clean-tmps
Most important lines like
Code:
args="-atime +$daily_clean_tmps_days -mtime +$daily_clean_tmps_days"
args="${args} -ctime +$daily_clean_tmps_days"
dargs="-empty -mtime +$daily_clean_tmps_days"

for dir in $daily_clean_tmps_dirs
do
find -x -d . -type f $args -delete $print
find -x -d . ! -name . -type d $dargs -delete $print
 
Here's what i use to delete old quarantine e-mails via crontab

0 3 * * * /usr/bin/find /var/virusmails/ -type f -mtime +30 -delete

If you want to limit the search result to specific file you can use -name "*.txt" this will match all txt files it will look like this:
find /path/ -type f -name "*.txt" -mtime +30 -delete
 
[…] delete files over a month old in a [specific] directory?
Perhaps your question would benefit from telling us what you want to delete? Applications producing data that simply become too old after one month usually provide some specific means for deleting old data, e. g. log rotation. The generic solution is of course, as yuripv79 wrote, periodic invocation of find(1), but maybe there is a more “targeted” mechanism for the files in your case.​
 
Last edited:
Try to set the variables in /etc/periodic.conf to use built-in periodic script.
Code:
daily_clean_tmps_enable="YES"                # Delete stuff daily
daily_clean_tmps_dirs="/tmp /your/specifc/directory"                # Delete under here
daily_clean_tmps_days="30"                # If not accessed for
Sometimes you should not perform /tmp cleanup because of some important temporary files. You can specify only your target directory for cleanup, without /tmp.

If you want to write your own script then try to review built-in examples:
/etc/periodic/daily/110.clean-tmps

It uses some variables in /etc/defaults/periodic.conf
Code:
daily_clean_tmps_enable="NO"                # Delete stuff daily
daily_clean_tmps_dirs="/tmp"                # Delete under here
daily_clean_tmps_days="3"                # If not accessed for
daily_clean_tmps_ignore=".X*-lock .X11-unix .ICE-unix .font-unix .XIM-unix"
daily_clean_tmps_ignore="$daily_clean_tmps_ignore quota.user quota.group .snap"
daily_clean_tmps_ignore="$daily_clean_tmps_ignore .sujournal"
daily_clean_tmps_verbose="YES"                # Mention files deleted

Do I understand this correctly that every day the system will delete all the files, including subdirectories older than 3 days from /tmp?

Where do you see the report?

I assume that all I need to do is change enable to "YES"
 
Anyone interested in using 'periodic' might find something useful here.

One thing I've found is that

Code:
daily_clean_tmps_dirs="/tmp"                            # Delete under here

This deletes all the files but leaves subdirectories intact. Is such behaviour to be expected?
 
Do I understand this correctly that every day the system will delete all the files, including subdirectories older than 3 days from /tmp?
Yes. You can specify any other period in days.

Where do you see the report?
If daily_clean_tmps_verbose set to "YES" then report will be mailed as 'daily run output' if you configured MTA on your server.

his deletes all the files but leaves subdirectories intact. Is such behaviour to be expected?
Yes. Any change in the directory update/change its time records.
Empty directories should be deleted when it will be unused/unchanged more than "daily_clean_tmps_days".
 
Yes. You can specify any other period in days.


If daily_clean_tmps_verbose set to "YES" then report will be mailed as 'daily run output' if you configured MTA on your server.

As an alternative you can output the results to a log file.

eg

daily_output=/var/log/periodic/daily.log
Yes. Any change in the directory update/change its time records.
Empty directories should be deleted when it will be unused/unchanged more than "daily_clean_tmps_days".

I was wondering whether a search through subdirectories would alter the last access time, which would mean they never remain unused for three days....

Under:-
# 100.clean-disks
there is:-
daily_clean_disks_files="[#,]* .#* a.out *.core *.CKP .emacs_[0-9]*"

Which directories are searched for such files? And is there way to find out what this directive translates to as an actual shell script?
 
Under:-
# 100.clean-disks
there is:-
daily_clean_disks_files="[#,]* .#* a.out *.core *.CKP .emacs_[0-9]*"

Which directories are searched for such files? And is there way to find out what this directive translates to as an actual shell script?

Don't know how I missed this....

100.clean-disks exists here

/etc/periodic/daily/100.clean-disks:-

Bash:
#!/bin/sh
#
# $FreeBSD$
#
# Remove garbage files more than $daily_clean_disks_days days old
#

# If there is a global system configuration file, suck it in.
#
if [ -r /etc/defaults/periodic.conf ]
then
    . /etc/defaults/periodic.conf
    source_periodic_confs
fi

case "$daily_clean_disks_enable" in
    [Yy][Ee][Ss])
        if [ -z "$daily_clean_disks_days" ]
        then
            echo '$daily_clean_disks_enable is set but' \
                '$daily_clean_disks_days is not'
            rc=2
        elif [ -z "$daily_clean_disks_files" ]
        then
            echo '$daily_clean_disks_enable is set but' \
                '$daily_clean_disks_files is not'
            rc=2
        else
            echo ""
            echo "Cleaning disks:"
            set -f noglob
            args="-name "`echo "$daily_clean_disks_files" |
                sed -e 's/^[    ]*//' \
                    -e 's/[     ]*$//' \
                    -e 's/[     ][      ]*/ -o -name /g'`

            case "$daily_clean_disks_verbose" in
                [Yy][Ee][Ss])
                    print=-print;;
                *)
                    print=;;
            esac

            rc=$(find / \( ! -fstype local -o -fstype rdonly \) -prune -o \
                \( $args \) -atime +$daily_clean_disks_days \
                -execdir rm -df {} \; $print | tee /dev/stderr | wc -l)
            [ -z "$print" ] && rc=0
            [ $rc -gt 1 ] && rc=1
            set -f glob
        fi;;

    *)  rc=0;;
esac

exit $rc

I'm not very good with shell scripts, but I guess this is the bit where files are found and deleted:-


Bash:
find / \( ! -fstype local -o -fstype rdonly \) -prune -o \

                \( $args \) -atime +$daily_clean_disks_days \

                -execdir rm -df {}

Could someone explain which files are searched for? By my understanding ' find /' starts searching from the root of the file system. I have numerous files, /root/serverauth.**** some of which go back four years. How do I get 100.clean-disks to find and delete them?
 
daily_clean_tmps is not /etc/periodic/daily/100.clean-disks,

it is /etc/periodic/daily/110.clean-tmps
Code:
dargs="-empty -mtime +$daily_clean_tmps_days"
find -x -d . ! -name . -type d $dargs -delete $print
It uses file "last modification time" for directories.
 
Under:-
# 100.clean-disks
there is:-
daily_clean_disks_files="[#,]* .#* a.out *.core *.CKP .emacs_[0-9]*"

Which directories are searched for such files? And is there way to find out what this directive translates to as an actual shell script?
Code:
rc=$(find / \( ! -fstype local -o -fstype rdonly \) -prune -o \
                \( $args \) -atime +$daily_clean_disks_days \
                -execdir rm -df {} \; $print | tee /dev/stderr | wc -l)
It searches starting from / but not for filesystems marked as local or readonly. Run lsvfs()
 
Code:
rc=$(find / \( ! -fstype local -o -fstype rdonly \) -prune -o \
                \( $args \) -atime +$daily_clean_disks_days \
                -execdir rm -df {} \; $print | tee /dev/stderr | wc -l)
It searches starting from / but not for filesystems marked as local or readonly. Run lsvfs()

Does daily_clean_disks only search for [#,]* .#* a.out *.core *.CKP .emacs_[0-9]* anywhere on the filesystem and delete them if they are more than x days old?
 
daily_clean_tmps is not /etc/periodic/daily/100.clean-disks,

it is /etc/periodic/daily/110.clean-tmps
Code:
dargs="-empty -mtime +$daily_clean_tmps_days"
find -x -d . ! -name . -type d $dargs -delete $print
It uses file "last modification time" for directories.
Do directories become modified if their contents are deleted?
 
Do directories become modified if their contents are deleted?
Think of a directory as a special kind of file, which contains a list of pair: (filename -> internal file ID known as inode number). Then anytime a new entry is added to a directory, or deleted from it, the directory itself changes. So the answer to your question is yes.

Matter-of-fact, the earliest versions of directories were implemented exactly like that. And it was relatively recently (FreeBSD 8 or 9) that you could still read directories, with tools like cat. You got binary gobbledygook on the screen, but the content was all there.
 
Think of a directory as a special kind of file, which contains a list of pair: (filename -> internal file ID known as inode number). Then anytime a new entry is added to a directory, or deleted from it, the directory itself changes. So the answer to your question is yes.

Matter-of-fact, the earliest versions of directories were implemented exactly like that. And it was relatively recently (FreeBSD 8 or 9) that you could still read directories, with tools like cat. You got binary gobbledygook on the screen, but the content was all there.
OK, so how do I get periodic to delete empty directories under /tmp?
 
Just add to the /etc/periodic.conf
Code:
daily_clean_tmps_enable="YES"                # Delete stuff daily
daily_clean_tmps_dirs="/tmp"                # Delete under here
daily_clean_tmps_days="3"                # If not accessed for
It works for me. Empty dirs will be deleted after "daily_clean_tmps_days" since last modification time for the directory.
 
Just add to the /etc/periodic.conf
Code:
daily_clean_tmps_enable="YES"                # Delete stuff daily
daily_clean_tmps_dirs="/tmp"                # Delete under here
daily_clean_tmps_days="3"                # If not accessed for
It works for me. Empty dirs will be deleted after "daily_clean_tmps_days" since last modification time for the directory.
When I run:-

stat -f "%N%t acc: %Sa%t mod: %Sm%t chg: %Sc%t crt: %SB" /tmp/.*

I see:
Code:
/tmp/.org.chromium.Chromium.ro53gQ     acc: Apr 14 06:27:53 2020     mod:  Apr 14 06:27:53 2020     chg: Apr 14 06:27:53 2020     crt: Apr 14 06:27:53 2020
/tmp/.org.chromium.Chromium.sDvvD9     acc: Nov 30 15:37:23 2019     mod:  Nov 30 15:37:23 2019     chg: Nov 30 15:37:23 2019     crt: Nov 30 15:37:23 2019
/tmp/.org.chromium.Chromium.scsVM4     acc: Apr 16 00:02:17 2020     mod:  Apr 16 00:02:17 2020     chg: Apr 16 00:02:17 2020     crt: Apr 16 00:02:17 2020
/tmp/.org.chromium.Chromium.sez7yP     acc: Oct 29 00:01:41 2019     mod:  Oct 29 00:01:41 2019     chg: Oct 29 00:01:41 2019     crt: Oct 29 00:01:41 2019
/tmp/.org.chromium.Chromium.ued831     acc: Aug 14 22:20:08 2023     mod:  Aug 14 22:20:08 2023     chg: Aug 14 22:20:08 2023     crt: Aug 14 22:20:08 2023
/tmp/.org.chromium.Chromium.umDDY1     acc: Nov  7 11:58:15 2019     mod:  Nov  7 11:58:15 2019     chg: Nov  7 11:58:15 2019     crt: Nov  7 11:58:15 2019
/tmp/.org.chromium.Chromium.v5ajeR     acc: Oct 23 15:35:36 2020     mod:  Oct 23 15:35:36 2020     chg: Oct 23 15:35:36 2020     crt: Oct 23 15:35:36 2020
/tmp/.org.chromium.Chromium.wV2Hid     acc: Apr  9 00:27:45 2021     mod:  Apr  9 00:27:45 2021     chg: Apr  9 00:27:45 2021     crt: Apr  9 00:27:45 2021
/tmp/.org.chromium.Chromium.xwt825     acc: Mar  5 20:16:38 2021     mod:  Mar  5 20:16:38 2021     chg: Mar  5 20:16:38 2021     crt: Mar  5 20:16:38 2021

As you can some of these files have been around for years but periodic hasn't deleted them.

Can anyone suggest why not?
 
Check the file owner and permissions.

Edit:
and check for typo errors in /etc/periodic.conf then you can run periodic daily to test
 
Check the file owner and permissions.

Edit:
and check for typo errors in /etc/periodic.conf then you can run periodic daily to test
I'm using the original file, the only change is to enable 110. - clean tmps.

Just wondering if there is any way to single step through a shell script to see what is going on. Something clearly isn't working.
 
Still no progress....

As far as I can tell, this is the bit in:-

/etc/periodic/daily/110.clean-tmps

which deletes files and directories in /tmp/
Bash:
                do
                    [ ."${dir#/}" != ."$dir" -a -d $dir ] && cd $dir && {
                        find -x -d . -type f $args -delete $print
                        find -x -d . ! -name . -type d $dargs -delete $print
                    } | sed "s,^\\.,  $dir,"
                done | tee /dev/stderr | wc -l)


The second find seems to deal with directories. When it runs $dargs has the value:-

Code:
-empty -mtime +3 ! -name .X*-lock ! -name .X11-unix ! -name .ICE-unix ! -name .font-unix ! -name .XIM-unix ! -name quota.user ! -name quota.group ! -name .snap ! -name .sujournal

Will any of these parameters stop empty directories being deleted? Presumably, this runs as an ordinary cron job, so would permissions be be a factor to bare in mind?
 
Looks like I was mistaken about empty directories...I have around 100 directories called
.org.chromium.Chromium.??????and each has two files:
Code:
lrwxr-xr-x    1 root  wheel     20 Nov  8  2019 SingletonCookie -> 18038160609090945950
srwxr-xr-x    1 root  wheel      0 Nov  8  2019 SingletonSocket

I'm not sure what they are but some have been around for four year and periodic's daily/110.clean-tmps is unable to delete these files and so their parent directories.
 
Back
Top