What's new

Help With Custom Script

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

ecnav

New Around Here
I'm trying to make a script that finds DIRECTORIES that are older than X days and delete them. So far, I am successful doing so if I'm trying to delete FILES. But I need it to include directories.

This is what I have:
#!/bin/sh
find /tmp/mnt/sdb1/Left/Archive/ -mtime +15 -exec rm -rf {} \;
find /tmp/mnt/sdb1/Right/Archive/ -mtime +15 -exec rm -rf {} \;

I don't believe this will include the directories inside the Archive directory. Adding -type d does not work; -type is not a valid switch here. It seems to be valid in DDWRT but not in Merlin.

Here's a quick test:
RT-N66U:/jffs/scripts# find /tmp/mnt/sdb1/Left/ -type d -mtime +15 -exec ls -lh {} \;
find: unrecognized: -type
BusyBox v1.20.2 (2015-01-09 00:44:43 EST) multi-call binary.

Usage: find [PATH]... [OPTIONS] [ACTIONS]

Search for files and perform actions on them.
First failed action stops processing of current file.
Defaults: PATH is current directory, action is '-print'

-follow Follow symlinks

Actions:
! ACT Invert ACT's success/failure
ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2
ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2
Note: -a has higher priority than -o
-name PATTERN Match file name (w/o directory name) to PATTERN
-iname PATTERN Case insensitive -name
-mtime DAYS mtime is greater than (+N), less than (-N),
or exactly N days in the past
If none of the following actions is specified, -print is assumed
-print Print file name
-print0 Print file name, NUL terminated
-exec CMD ARG ; Run CMD with all instances of {} replaced by
file name. Fails if CMD exits with nonzero

There is a directory in there:

RT-N66U:/tmp/mnt/sdb1/Left# ls -lh |grep -i archive
drwxrwxrwx 2 Admin root 704.0K Feb 24 01:00 Archive

+++
Any other suggestions as how I can accomplish this? I don't mind if it blows out all the files AND directories. I just need the directories gone.
 
Last edited:
How about "find tmp/mnt/sdb1/Left/Archive/ -ctime +15 -delete"?
 
You just have to be patient. :)

Because you have just deleted files within a directory the modification time of that directory is updated, so it no longer matches the "-mtime +15". Assuming that you don't modify that directory again, after another 15 days the test will match that directory and it will be deleted.
 
Thanks for your responses all.

What will +ctime do that +mtime is not? I also do not see -ctime listed as a valid option.

ColinTaylor - That makes perfect sense now. I'll try to wait it out to see if the directories ultimately go away. I don't mind if it is behind; as long as it cleans itself up without me having to log in.
 
Perhaps install "findutils" via Entware?
(I forgot I had installed it.)

Here are some it's extended features, as seen on my RT-N66U.

Code:
admin@merlin:/tmp/home/root# find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xdev -ignore_readdir_race -noignore_readdir_race

tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context CONTEXT


actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Report (and track progress on fixing) bugs via the findutils bug-reporting
page at http://savannah.gnu.org/ or, if you have no web access, by sending
email to <bug-findutils@gnu.org>.
 

Similar threads

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top