by db48x · 4mo ago · 0 comments
Always use -0. Most gnu utilities support it. It makes them put a null byte after every filename instead of a newline. Completely eliminates the problem of dealing with whitespace in the filenames.
find . -name '*.log' | xargs rm
With: find . -name '*.log' -print0 | xargs -0 rm find . -name '*.log' -delete #order files from different levels in a file hierarchy by size:
find . -type f -maxdepth 1 -exec ls -hlrS {} +
#output metadata for each file sequentially
find . -type f -maxdepth 1 -exec file {} \; find ... | tr '\n' '\0' | xargs -0 ...
I.e., a blind replacement without the tool having any particular semantic understanding of what it's translating.That still requires your xargs to have -0 support, though, and I'd be surprised to have that without the corresponding option on find. But I've done this before when feeding xargs from something other than find.
It's baffling to me that POSIX allows non-printable characters in file names, but here we are. Surely they had a reason for this decision.