Improve wording & code readability of xargs

I've also un-squished flags & their arguments to avoid confusion.
pull/130/head
terminalforlife 4 years ago
parent ac2480bca9
commit 8576445e17

@ -1,26 +1,32 @@
# Find all file names ending with .pdf and remove them
# 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 preferable written without xargs:
# The above, however, is better-written without xargs:
find -name \*.pdf -exec rm {} \+
# Or, for find's own functionality, it's best as:
# Although it's best to use find's own functionality, in this situation.
find -name \*.pdf -delete
# Find all file name ending with .pdf and remove them
# (bulletproof version: handles filenames with \n and skips *.pdf directories)
# -r = --no-run-if-empty
# -n10 = group by 10 files
find -name \*.pdf -type f -print0 | xargs -0rn10 rm
# 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 name contains spaces you should use this instead
find -name \*.pdf | xargs -I{} rm -rf '{}'
# 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 '{}'
# Will show every .pdf like:
# &toto.pdf=
# &titi.pdf=
# -n1 => One file by one file. ( -n2 => 2 files by 2 files )
find -name \*.pdf | xargs -I{} -n1 echo '&{}='
# The above is, however, much faster and easier without xargs:
find -name \*.pdf -printf "%p\n"
# 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 -n3
# 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

Loading…
Cancel
Save