4

I want to delete all the folders that are older than 1 day with following command:

find /test -mmin +1440 | xargs rm -rf

But the output of find lists /test (and would remove it accordingly).

How can I find only the sub dirs of /test? (-maxdepth / -mindepth not available in AIX)

  • 1
    you could simply find /test/*. – meuh Aug 27 '15 at 9:39
  • Which subdirs exactly? Any under /test but not /test, or max-depth limited? – cxw Aug 27 '15 at 9:45
  • Consider an "old" directory containing "new" directories. Should these "new" ones still all be deleted because they're within the "old" directory? Or do we need to avoid deleting an "old" directory that contains "new" directories? – roaima Aug 27 '15 at 10:11
  • What do you consider to be the age of a directory? Note that the modification time of a directory file only reflect the last time an entry was added, removed or renamed in it. It is not updated when any of the files (of type regular or directory or other) linked in it are modified. In particular, any change made to subdirectories or their content doesn't affect the modification time of a directory. – Stéphane Chazelas Aug 27 '15 at 11:35
  • you need to empty folders before you can delete them. – AquaAlex Aug 31 '15 at 12:42
3

As @meuh said in his comment, you could use /test/* instead of /test. Your command could then look similar to this:

find /test/* -type d -mmin +1440 | xargs rm -rf

In this case only the subfolders of /test would be removed.

  • 1
    Note that /test/* omits the hidden directories (in /test, not in its subdirs). – Stéphane Chazelas Aug 27 '15 at 14:49
  • 2
    find's output cannot be processed reliably (let alone by xargs for which blanks and quotes are also a problem). You should use the -exec predicate instead. – Stéphane Chazelas Aug 27 '15 at 14:50
7

POSIXly:

find /test/. ! -name . -type d -mtime +0 -exec rm -rf {} \; -prune

(we use -prune for the directories that we successfully remove so that find doesn't complain that they're suddenly gone).

In any case, note that the modification time (as checked by -mtime above) of a directory file only reflects the last time an entry was added, removed or renamed in it.

It is not updated when the content of any of the files (of type regular or directory or other) linked in it are modified. In particular, any change made to subdirectories or their content doesn't affect the modification time of a directory.

Note that all of -mindepth, -maxdepth and -mmin are GNU extensions (though they are supported in some other implementations).

The standard equivalent of find . -maxdepth 1 would be:

find . -name . -o -prune

For -mindepth 1:

find . ! -name .

For -mindepth 1 -maxdepth 1:

find . ! -name . -prune

For directories other than ., use find some/dir/. ... as above.

For other values of depths, you can use -path, but note that since it has only been recently added to the standard, some systems (like AIX) still don't have it.

For: -maxdepth 2:

find . ! -path '*/*/*' -o -prune

For: -mindepth 2:

find . -path '*/*/*'

For another dir:

find some/dir//. -path '*//*/*/*'
1

Well, the -mmin primary is a GNUism that is most likely not supported on AIX as it is a silly expansion compared to what the BSD people and I used as extension in the 1980s already. Given the fact that sfind compiles fine on AIX, I recommend:

sfind . -mindepth 1 -type d -mtime +24h -exec rm -rf {} +

The code is in schilytools at: https://sourceforge.net/projects/schilytools/files/

  • -mmin appears to be supported by AIX 7.1's find. Here, one can use the POSIX -mtime +0 though. – Stéphane Chazelas Aug 27 '15 at 12:34
  • POSIX -mtime -0 has a different meaning than -mtime +24h even though this specific case evaluates the same. The simple POSIX -mtime evaluates true if the time stamp matches within the seconds of a day. The extended time spec evaluates true if there is an exact match, so you may e.g. use -mtime +23h. I still don't understand why a POSIX certified UNIX added a non-POSIX GNUism imstead of a POSIX compliant extension. – schily Aug 27 '15 at 13:19
  • While I also find -mtime +1440m makes more sense (though the behaviour is not consistent with that without suffix or with other predicates that take suffices (like -size) (though it's more intuitive)), adding support for a -mmin doesn't break POSIX conformance in any way. POSIX explicitely allows implementations adding extensions starting with - as it does say that in find -- x -anything, -anything should not be treated as a file argument (The first operand that starts with a '-', or is a '!' or a '(', and all subsequent arguments shall be interpreted as an expression...) – Stéphane Chazelas Aug 27 '15 at 15:05
  • Given that POSIX does currently not permit -newer +24h, this is a clean POSIX extension. I am sure this will be added to POSIX in the near future (with Issue 8). – schily Aug 27 '15 at 15:11
0
touch -t 201508260000 dummyfile
find /path/to/files -type f ! -newer dummyfile -delete

Timestamp format yyyyMMddhhmm

The first line creates a file which was last modified on the 26th August 2015. The second line finds all files in /path/to/file which has a date not newer than the dummyfile, and then deletes them.

If you want to double check it is working correctly, then drop the -delete argument and it should just list the files which would be deleted.

Copied from link

0
find /test -type d -mtime +1| egrep -v '^/test$'|xargs -I{} rm -rf {}
  • -mtime +1 gives you the files that are at least 2 days (48 hours) old. Use -mtime +0 for files at least 1 day old. – Stéphane Chazelas Aug 27 '15 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.