From 262b77aaea2ebce16bfbe4295ddfe7189b881212 Mon Sep 17 00:00:00 2001 From: Waldir Pimenta Date: Mon, 17 Oct 2016 23:02:05 +0100 Subject: [PATCH] xargs: rework page to make it more beginner-friendly (#1039) * xargs: update as agreed on code review. --- pages/common/xargs.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pages/common/xargs.md b/pages/common/xargs.md index cbbe3cf704..db6e40d758 100644 --- a/pages/common/xargs.md +++ b/pages/common/xargs.md @@ -1,19 +1,20 @@ # xargs -> Execute a command with piped arguments. +> Execute a command with piped arguments coming from another command, a file, etc. +> The input is treated as a single block of text and split into separate arguments on spaces, tabs, newlines and end-of-file. -- Main use: +- Main usage pattern: -`{{arguments}} | xargs {{command}}` +`{{arguments_source}} | xargs {{command}}` -- Specific example: delete all files that start with 'M': +- Delete all files with a `.backup` extension: -`find . -name 'M*' | xargs rm` +`{{find . -name '*.backup'}} | xargs {{rm -v}}` -- Handle whitespace in arguments: +- Convert newlines in the input into NUL (`\0`) characters, and split on those only (useful if the input to xargs contains spaces): -`{{arguments_null_terminated}} | xargs -0 {{command}}` +`{{arguments_source}} | tr '\n' '\0' | xargs -0 {{command}}` -- Insert arguments at chosen position, using '%' as the placeholder marker: +- Execute the command once for each input line, replacing any occurrences of the placeholder (here marked as `_`) with the input line: -`{{arguments}} | xargs -I '%' {{command}} % {{extra_arguments}}` +`{{arguments_source}} | xargs -I _ {{command}} _ {{optional_extra_arguments}}`