Why is `find | xargs rm` dangerous and what is the safe form?
IntermediateConceptfind, xargs, jq and yq
Answer
Default xargs splits on spaces and newlines, so /data/old report.csv becomes two arguments, and a crafted name like x /etc/passwd could delete something unrelated. With empty input, GNU xargs still runs rm once with no arguments (harmless for rm, but not for every command).
find /data -name '*.csv' -mtime +30 -print0 | xargs -0 -r rm -f --
# or simply
find /data -name '*.csv' -mtime +30 -delete
-- stops rm from treating a file named -rf as options.
More on find, xargs, jq and yq
What are the most useful `find` predicates for operations work?BeginnerWhat is the difference between `-exec cmd {} \;`, `-exec cmd {} +` and `-delete`?IntermediateHow does `find` evaluate `-o` (OR) and why do you need parentheses?AdvancedWhat does `xargs` do, and which flags matter?IntermediateWhat is `jq` and what are the filters you use every day?BeginnerHow do you pass shell variables into `jq` safely?Intermediate