Daniel Lyons' Notes

Xargs Explained

Description

The xargs command explained.Previous video: https://www.youtube.com/watch?v=U2fsUy0viLA

Notes

00:00 Introduction to xargs

  • xargs is a tool that runs a command against every line of input from standard input
  • More powerful alternative to find -exec action covered in previous video

00:15 xargs vs find -exec comparison

  • Using find -exec:
    • Syntax: find . -type f -exec sha1sum {} \;
  • Using xargs:
    • Syntax: find . -type f | xargs sha1sum
    • Output of find command piped into xargs using the pipe character (|)

01:04 Standard input, output, and piping

  • Every command has standard input and standard output streams
  • By default, both are attached to your terminal
  • Pipe character (|) attaches output of one command to input of another

01:36 Handling filenames with spaces

  • Problem: Filenames with spaces cause xargs to treat them as separate files
  • Solution: Use -print0 flag with find to use null byte separator instead of newline
    • 01:57 Syntax: find . -type f -print0 | xargs -0 sha1sum
    • The -0 flag tells xargs to use null byte as separator

03:03 Practical example: DNS lookups

  • Use case: Look up IP addresses for multiple domain names stored in a file
  • Tool: host command with -t a flag to query A records
  • 03:27 Initial attempt: cat hostnames | xargs host -t a

03:45 Issue with passing all input at once

  • xargs passes all input lines to command at once by default
  • Works for commands like sha1sum but fails for host command
  • Network error occurred because host command couldn't handle multiple domains at once

04:12 Using -n flag to limit input

  • -n flag limits number of input lines passed to command at once
  • 04:15 Example: cat hostnames | xargs -n1 host -t a
  • Processes one domain at a time

04:30 Using -I flag for custom placeholder

  • -I flag allows specifying where to place input in the command using a placeholder
  • 04:38 Syntax: cat hostnames | xargs -I {} host -t a {} 8.8.8.8
  • Curly braces {} used as placeholder (matches find -exec syntax)
  • Using -I automatically implies -n1 behavior

05:24 Parallel execution with -P flag

  • -P flag runs multiple commands in parallel using multiple CPU cores
  • 05:40 Example: cat hostnames | xargs -I {} -P 4 host -t a {} 8.8.8.8
  • -P 4 runs 4 commands simultaneously, significantly faster

06:05 Cleaning up output with tail

  • tail command outputs only the end of input
  • -n flag controls number of lines to output
  • 06:31 Initial attempt with pipe failed: ... | xargs ... | tail -n1
  • Problem: Shell features like piping don't automatically work inside xargs commands

07:03 Using sh -c for complex commands

  • Solution: Have xargs run shell itself using sh -c
  • 07:35 Syntax: xargs -I {} sh -c "command | pipe"
  • 08:01 Complete example: cat hostnames | xargs -I {} -P 4 sh -c "host -t a {} 8.8.8.8 | tail -n1"
  • This technique is useful for moderately complex commands

08:34 When to use while loops

  • For even more complicated scenarios, consider using while loops instead of xargs
  • Topic for next video
Xargs Explained
Interactive graph
On this page
Description
Notes
00:00 Introduction to xargs
00:15 xargs vs find -exec comparison
01:04 Standard input, output, and piping
01:36 Handling filenames with spaces
03:03 Practical example: DNS lookups
03:45 Issue with passing all input at once
04:12 Using -n flag to limit input
04:30 Using -I flag for custom placeholder
05:24 Parallel execution with -P flag
06:05 Cleaning up output with tail
07:03 Using sh -c for complex commands
08:34 When to use while loops