aboutsummaryrefslogtreecommitdiff
path: root/toys/posix
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2016-02-01 12:46:15 -0600
committerRob Landley <rob@landley.net>2016-02-01 12:46:15 -0600
commit8aee3e5b5e774e3d79951d240e08384590811ffc (patch)
tree984ebbbc0b5cac8049f6ad74aaa73dbbbd4b2ae3 /toys/posix
parent3f3049c2f1b225f244fe3c1205935c48c542a055 (diff)
downloadtoybox-8aee3e5b5e774e3d79951d240e08384590811ffc.tar.gz
Add find -delete
Diffstat (limited to 'toys/posix')
-rw-r--r--toys/posix/find.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/toys/posix/find.c b/toys/posix/find.c
index e16c0df0..febe688b 100644
--- a/toys/posix/find.c
+++ b/toys/posix/find.c
@@ -8,7 +8,7 @@
* Parentheses can only stack 4096 deep
* Not treating two {} as an error, but only using last
*
- * TODO: -empty (dirs too!) -delete -execdir +
+ * TODO: -empty (dirs too!)
USE_FIND(NEWTOY(find, "?^HL[-HL]", TOYFLAG_USR|TOYFLAG_BIN))
@@ -46,6 +46,7 @@ config FIND
-print Print match with newline -print0 Print match with null
-exec Run command with path -execdir Run command in file's dir
-ok Ask before exec -okdir Ask before execdir
+ -delete Remove matching file/dir
Commands substitute "{}" with matched file. End with ";" to run each file,
or "+" (next argument after "{}") to collect and run with multiple files.
@@ -277,7 +278,13 @@ static int do_find(struct dirtree *new)
} else s++;
if (!strcmp(s, "xdev")) TT.xdev = 1;
- else if (!strcmp(s, "depth")) TT.depth = 1;
+ else if (!strcmp(s, "delete")) {
+ // Delete forces depth first
+ TT.depth = 1;
+ if (new && check)
+ test = !unlinkat(dirtree_parentfd(new), new->name,
+ S_ISDIR(new->st.st_mode) ? AT_REMOVEDIR : 0);
+ } else if (!strcmp(s, "depth")) TT.depth = 1;
else if (!strcmp(s, "o") || !strcmp(s, "or")) {
if (not) goto error;
if (active) {