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
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
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
0 3 * * * /usr/bin/find /var/virusmails/ -type f -mtime +30 -delete
-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?
Try to set the variables in /etc/periodic.conf to use built-in periodic script.
Sometimes you should not perform /tmp cleanup because of some important temporary files. You can specify only your target directory for cleanup, without /tmp.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
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
Yes. You can specify any other period in days.Do I understand this correctly that every day the system will delete all the files, including subdirectories older than 3 days from /tmp?
If daily_clean_tmps_verbose set to "YES" then report will be mailed as 'daily run output' if you configured MTA on your server.Where do you see the report?
Yes. Any change in the directory update/change its time records.his deletes all the files but leaves subdirectories intact. Is such behaviour to be expected?
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.
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".
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?
/etc/periodic/daily/100.clean-disks
:-#!/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
find / \( ! -fstype local -o -fstype rdonly \) -prune -o \
\( $args \) -atime +$daily_clean_disks_days \
-execdir rm -df {}
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?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?
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)
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?Do directories become modified if their contents are deleted?daily_clean_tmps is not /etc/periodic/daily/100.clean-disks,
it is /etc/periodic/daily/110.clean-tmps
It uses file "last modification time" for directories.Code:dargs="-empty -mtime +$daily_clean_tmps_days" find -x -d . ! -name . -type d $dargs -delete $print
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.Do directories become modified if their contents are deleted?
OK, so how do I get periodic to delete empty directories under /tmp?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.
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
When I run:-Just add to the /etc/periodic.conf
It works for me. Empty dirs will be deleted after "daily_clean_tmps_days" since last modification time for the directory.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
stat -f "%N%t acc: %Sa%t mod: %Sm%t chg: %Sc%t crt: %SB" /tmp/.*
/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
That made me go and try it, looks like the error message needs fixing:And it was relatively recently (FreeBSD 8 or 9) that you could still read directories, with tools like cat.
$ mkdir foo
$ cat foo
cat: stdout: Is a directory
I'm using the original file, the only change is to enable 110. - clean tmps.Check the file owner and permissions.
Edit:
and check for typo errors in /etc/periodic.conf then you can runperiodic daily
to test
/etc/periodic/daily/110.clean-tmps
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)
find
seems to deal with directories. When it runs $dargs has the value:--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
lrwxr-xr-x 1 root wheel 20 Nov 8 2019 SingletonCookie -> 18038160609090945950
srwxr-xr-x 1 root wheel 0 Nov 8 2019 SingletonSocket
daily/110.clean-tmps
is unable to delete these files and so their parent directories.