2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-19 03:25:44 +00:00
cheat.sheets/sheets/xargs
terminalforlife 8576445e17 Improve wording & code readability of xargs
I've also un-squished flags & their arguments to avoid confusion.
2020-11-12 03:39:19 +00:00

33 lines
1.4 KiB
Plaintext

# xargs
# Build and execute command lines from standard input
# Find all file names ending with .pdf, then remove them.
find -name \*.pdf | xargs rm
# The above, however, is better-written without xargs:
find -name \*.pdf -exec rm {} \+
# Although it's best to use find's own functionality, in this situation.
find -name \*.pdf -delete
# Find all file names ending with '.pdf' and remove them. This approach also
# handles filenames with '\n' and skips '*.pdf' directories. The xargs(1) flag
# `-r` is equivalent to `--no-run-if-empty`, and the use of `-n` will in this
# case group execution by 10 files.
find -name \*.pdf -type f -print0 | xargs -0 -r -n 10 rm
# If file names may contains spaces, you can use the xargs(1) flag `-I` and its
# proceeding argument to specify the filename placeholder, as in find(1)'s use
# of `{}` in `-exec`. Although find(1)'s `{}` needs not be cuddled by quotes, -
# xargs(1) does.
find -name \*.pdf | xargs -I {} rm -r '{}'
# Print a list of files in the format of `*FILE=`. The use of xargs(1) flag
# `-n` here with its argument of `1` means to process the files one-by-one.
find -name \*.pdf | xargs -I {} -n 1 echo '&{}='
# The above is, however, much faster, more efficient, and easier without xargs.
find -name \*.pdf -printf '&%f=\n'
# Group words by three in a string.
seq 1 10 | xargs -n 3
# Alternatively, and more efficiently, use Bash brace expansion, if available.
printf '%d ' {1..10} | xargs -n 3