0

I’m using Amazon Linux, bash shell. I’m trying to write a statement that will delete all child directories named “target” within a specific master directory. I would like to write things such that even if the directories don’t exist, there is no error thrown. I tried the below but I’m getting an error

[jboss@mydevbox ~]$ find ~/.jenkins/jobs/subco/workspace/myproject/ -name 'target' -print0 | xargs rm -rf
xargs: Warning: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?

What is a better way to write the above?

3

If you're using -print0 you should use the -0 flag to xargs so it will read the names correctly.

find ~/.jenkins/jobs/subco/workspace/myproject/ -name 'target' -print0 | xargs -0 rm -rf

alternately, if you have GNU find you could use the -delete flag, though it won't work if the directories are not empty

find ~/.jenkins/jobs/subco/workspace/myproject/ -name 'target' -delete

Or, you could also use -exec with find like

find ~/.jenkins/jobs/subco/workspace/myproject/ -name 'target' -exec rm -rf {} +
| improve this answer | |
0

Modern find (via POSIX, so the not-GNU find on OpenBSD does support this) can do the necessary work without a call to xargs via the -exec command {} + form.

find ... -exec rm -rf '{}' \+
| improve this answer | |

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.