Linux Pipelines, xargs, split, diff, and Practical Recipes Cheat Sheet

Build practical shell pipelines, batch commands with xargs, compare files, and split or reshape data.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## xargs
Pass stdin items as arguments
printf '%s
' file1 file2 file3 | xargs rm

# Convert lines into command arguments.

Use null-delimited input
find . -type f -print0 | xargs -0 rm

# Handle spaces and special characters safely.

Run one item per command
printf '%s
' a b c | xargs -n 1 echo item:

# Invoke the command once per input line.

Use a placeholder in the command
printf '%s
' app1 app2 | xargs -I{} kubectl logs {}

# Insert each item into a custom position.

Run commands in parallel
printf '%s
' url1 url2 url3 | xargs -n 1 -P 4 curl -I

# Process multiple input items concurrently.

## split and csplit
Split a file by line count
split -l 1000 bigfile.txt chunk_

# Create chunks of 1000 lines each.

Split a file by size
split -b 50M archive.log log_

# Create 50 MB chunks.

Use numeric suffixes
split -d -l 1000 bigfile.txt chunk_

# Create chunks with numeric names.

Split using regex boundaries
csplit -f part_ report.txt '/^SECTION:/' '{*}'

# Break a file at repeated pattern boundaries.

## diff, cmp, and patch-Oriented Comparison
Show unified diff
diff -u old.conf new.conf

# Compare two text files with context.

Compare directories recursively
diff -ruN dir1 dir2

# Find differences across two directory trees.

Compare files byte by byte
cmp file1.bin file2.bin

# Detect exact byte-level differences.

Show side-by-side diff
sdiff file1.txt file2.txt

# Compare files in two visual columns.

## Practical Recipes
Show most frequent error messages
grep ERROR app.log | sort | uniq -c | sort -nr | head

# Count and rank repeated lines in a log.

Show effective config lines only
grep -Ev '^[[:space:]]*($|#)' nginx.conf

# Remove comments and blank lines from a config file.

Sum a CSV column
awk -F, 'NR>1 {sum += $3} END {print sum}' sales.csv

# Compute the total of a numeric CSV field after skipping the header.

Find the largest files in a tree
find . -type f -printf '%s %p
' | sort -nr | head -20

# List the top 20 largest files by size.

Extract unique IP addresses
grep -Eo '([0-9]{1,3}.){3}[0-9]{1,3}' access.log | sort -u

# Print unique IPs from a log file.

Replace text across many files safely
rg -l 'old-service-name' . | xargs sed -i 's/old-service-name/new-service-name/g'

# Search files first, then edit in place.

Recommended next

No recommendations yet.