mirror of
https://github.com/chubin/cheat.sheets
synced 2024-11-17 09:25:32 +00:00
f04907ea0d
The examples given using xargs are so far mostly never or rarely ever best used or even well used with xargs, such as with find, which is popular, unfortunately. My reason for adding counters to some of these examples, is that, as an educational resource, I believe it's important users understand what is and is _not_ good practice; this is, at least, how I taught myself.
27 lines
910 B
Plaintext
27 lines
910 B
Plaintext
# Find all file names ending with .pdf and remove them
|
|
find -name \*.pdf | xargs rm
|
|
# The above, however, is preferable written without xargs:
|
|
find -name \*.pdf -exec rm {} \+
|
|
# Or, for find's own functionality, it's best as:
|
|
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
|
|
|
|
# If file name contains spaces you should use this instead
|
|
find -name \*.pdf | xargs -I{} rm -rf '{}'
|
|
|
|
# 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"
|
|
|
|
# Group words by three in a string
|
|
seq 1 10 | xargs -n3
|