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 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 {} \+ 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 -name \*.pdf -delete
# Find all file name ending with .pdf and remove them # Find all file names ending with '.pdf' and remove them. This approach also
# (bulletproof version: handles filenames with \n and skips *.pdf directories) # handles filenames with '\n' and skips '*.pdf' directories. The xargs(1) flag
# -r = --no-run-if-empty # `-r` is equivalent to `--no-run-if-empty`, and the use of `-n` will in this
# -n10 = group by 10 files # case group execution by 10 files.
find -name \*.pdf -type f -print0 | xargs -0rn10 rm find -name \*.pdf -type f -print0 | xargs -0 -r -n 10 rm
# If file name contains spaces you should use this instead # If file names may contains spaces, you can use the xargs(1) flag `-I` and its
find -name \*.pdf | xargs -I{} rm -rf '{}' # 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: # Print a list of files in the format of `*FILE=`. The use of xargs(1) flag
# &toto.pdf= # `-n` here with its argument of `1` means to process the files one-by-one.
# &titi.pdf= find -name \*.pdf | xargs -I {} -n 1 echo '&{}='
# -n1 => One file by one file. ( -n2 => 2 files by 2 files ) # The above is, however, much faster, more efficient, and easier without xargs.
find -name \*.pdf | xargs -I{} -n1 echo '&{}=' find -name \*.pdf -printf '&%f=\n'
# The above is, however, much faster and easier without xargs:
find -name \*.pdf -printf "%p\n"
# Group words by three in a string # Group words by three in a string.
seq 1 10 | xargs -n3 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