10

I need to find and delete files older than 1 week in the Development unit. There are limited number utilities available on this unit. -mtime find's predicate is not available. How do I check all files which are older than x days in this case?

10
  • 2
    It is unclear what you mean with "mtime is not available". It's not a utility whose name I recognise.
    – Kusalananda
    Aug 21, 2018 at 9:35
  • /path/to/my/folder$ find -type f -mtime +7 delete find: unrecognized: -mtime
    – Ravi
    Aug 21, 2018 at 9:44
  • share the distro of linux you are using.
    – Siva
    Aug 21, 2018 at 9:47
  • 3
    It looks like it's busybox built without FEATURE_FIND_MTIME. Is FEATURE_FIND_NEWER enabled? Does find -newer work? Does your test utility or the [ builtin of your sh support -nt? Aug 21, 2018 at 9:50
  • 1
    You can work around this if you can use those touch -d/date +%s and [ file -nt other-file ] Aug 21, 2018 at 10:08

2 Answers 2

11

-mtime is a standard predicate of find (contrary to -delete) but it looks like you have a stripped down version of busybox, where the FEATURE_FIND_MTIME feature has been disabled at build time.

If you can rebuild busybox with it enabled, you should be able to do:

find . -mtime +6 -type f -exec rm -f {} +

Or if FEATURE_FIND_DELETE is also enabled:

find . -mtime +6 -type f -delete

If not, other options could be to use find -newer (assuming FEATURE_FIND_NEWER is enabled) on a file that is set to have a one week old modification time.

touch -d "@$(($(date +%s) - 7 * 86400))" ../ref &&
  find . ! -type f -newer ../ref -exec rm -f {} +

Or if -newer is not available but sh's [ supports -nt:

touch -d "@$(($(date +%s) - 7 * 86400))" ../ref &&
  find . ! -type f -exec sh -c '
    for f do
      [ "$f" -nt ../ref ] || printf "%s\0" "$f"
    done' sh {} + |
    xargs -0 rm -f
1
  • 1
    Yes, busybox was a lighter version here. After re-build, find . -mtime +6 -type f -exec rm {} \; worked for me
    – Ravi
    Aug 21, 2018 at 11:42
2

From man find:

-atime n

File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.

-ctime n

File's status was last changed n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file status change times.

Depending on the use cases of the files you want to delete, these are your only other options for find. Why is mtime not available? What filesystem are you using? Did you explore any other options?

1
  • Asking more questions of the OP should be done as clarifying comments, not answers.
    – Jeff Schaller
    Aug 21, 2018 at 11:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .