This command correctly finds all files in the directory that are older than 1 day:
$ find /Users/[me]/test -type f ! -newermt -1day | xargs -0 -n1
/Users/[me]/test/1.txt
/Users/[me]/test/2.txt
However, when I change the 'xargs' parameter to delete them it throws an error:
$ find /Users/[me]/test -type f ! -newermt -1day | xargs -0 rm
rm: /Users/[me]/test/2.txt
/Users/[me]/test/1.txt
: No such file or directory
Anyone know what the problem is and how to fix it?
Or maybe there's a better way of doing this? (I'm not very familiar with the macos command line, as you can probably tell!)
-0
option w/xargs
, you must pair that with theprint0
option infind
; i.e.find /Users/[me]/test -type f ! -newermt -1day print0 | xargs -0 rm
. But this may depend on which version offind
&xargs
you use; i.e. the native FreeBSD-derived macOS version, OR the GNU version that you'd have if you've installed (for example) MacPortsfindutils
. If you have a lot of files to delete,xargs
is more efficient than using thedelete
option infind
.find
seems to support-print0
but if you have GNUfind
, you might as well use-exec rm {} +
which should be at least as efficient asxargs
.-delete
.