-mtime "+$days"
selects files whose age (based on last modification time) rounded down to an integer number of days is stricktly greater than $days
.
A file that is 2 days and 23 hours old would not be selected by -mtime +2
because, it's considered to be 2 day old which is not greater than 2
.
So you need:
find "$users_dir"/*/workspace -mtime "+$(( DAYSOLD - 1 ))" -type f -delete
To delete files that are older than $DAYSOLD
days¹ in any workspace
directory² found in any subdirectory³ of $users_dir
. Though you could also do:
find "$users_dir"/*/workspace '(' -mtime "$DAYSOLD" -o -mtime "+$DAYSOLD" ')' \
-type f -delete
Which deletes files whose age (in integer number of days) is either $DAYSOLD
or greater than $DAYSOLD
.
-delete
is a non-standard extension, but is available in GNU find
as found on Ubuntu and is safer and more efficient than using -exec rm
. You can make it -delete -print
if you want the list of files that have been successfully deleted.
Also remember to quote your variables and check the exit status of cd
before carrying one: (cd -P -- "$users_dir" && exec du...)
or du
would run in the wrong directory if cd
failed. Also note the use of a subshell or it wouldn't work properly if $users_dir
was a relative path. It would actually make more sense to write it as:
cd -P -- "$users_dir" || exit
du ...
find . ...
(which would be more consistent if $users_dir
was a symlink)
¹ strictly speaking, that's at least $DAYSOLD
days old but considering that the comparison is done using nanosecond precision and that it takes several hundred nanoseconds to start find
, that distinction is hardly relevant.
² strictly speaking, if workspace
is a file that is not of type directory, that will still be passed to find
, and if that file is older than $DAYSOLD
days (and is a regular files for -type f
), it will be deleted as well. You could switch to zsh
and add (/)
to the pattern to make sure only workspace
files of type directory are considered. Do not just append /
as you'd run into ³ below with more severe consequences (like when /users/joe/workspace
is a symlink to /bin
or /
).
³ Note that symlinks are followed there. So if /users/joe
is a symlink to /
for instance, that will delete old files in the /workspace
directory.