>_ IzzyLab

Hands-on task

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

xargs: delete files from a list with spaces

medium~5 min

After cleaning up the media library, a list of files to delete was collected in /root/obsolete.list — one full path per line, and the names contain spaces. Delete all listed files with a single command using xargs. Everything not on the list must stay — including a file named /srv/media/draft.

What you'll practise:
  • xargs
  • xargs -d
  • rm
  • spaces in file names
  • input redirection

Topic breakdown

xargs turns lines of standard input into command arguments — that's how a list in a file becomes the call rm path1 path2 …. The trap is in the splitting: by default xargs splits its input on any whitespace (and treats quotes in its own way), so a path with a space falls apart. The pieces may turn out to be real files — and then something entirely different from the list gets deleted. The reliable option is to set the separator explicitly: only the newline (-d) or the null byte (-0), which never appears in file names at all.

What's checked

  1. files from the list deleted
  2. other files in place

What it looks like