>_ IzzyLab

Hands-on task

This task is step 15 of the course “find and xargs: search and bulk operations”

xargs -0: names with spaces

medium~7 min

/root/docs and its subdirectories hold dozens of .txt files, many with spaces in their names (my file.txt, sub dir/part 1.txt). Append the line DONE to the end of every .txt file (without wiping what's already in it) using a space-safe find … | xargs … pipeline. Don't touch the .md files.

What you'll practise:
  • xargs
  • -0
  • find -print0
  • null delimiter
  • spaces in names

Topic breakdown

File names can contain spaces and newlines, so passing them line by line through xargs is unsafe — it splits its input on whitespace. The canonical solution is the null byte as the delimiter: find … -print0 terminates each name with \0 (a null byte can't appear in a file name), and xargs -0 reads input in the same format. This pair is the standard for reliably handling arbitrary names; grep, sort and sed have the same -z/-0 options.

What's checked

  1. DONE at the end of every .txt
  2. the previous content is preserved
  3. .md files untouched

What it looks like