🖥️xargs

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the xargs command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#                ██╗  ██╗ █████╗ ██████╗  ██████╗ ███████╗
#                ╚██╗██╔╝██╔══██╗██╔══██╗██╔════╝ ██╔════╝
#                 ╚███╔╝ ███████║██████╔╝██║  ███╗███████╗
#                 ██╔██╗ ██╔══██║██╔══██╗██║   ██║╚════██║
#                ██╔╝ ██╗██║  ██║██║  ██║╚██████╔╝███████║
#                ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝ ╚══════╝
                

                                                        
# find all file name ending with .pdf and remove them
find -name *.pdf | xargs rm -rf

# find all file name ending with .pdf and remove them
find -name \*.pdf | xargs rm

# 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

# group words by three in a string
seq 1 10 | xargs -n3 echo

# 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 '&{}='

xargs 
# does some amazing things. But it seems a bit large for the Unix philosophy of tools.

xargs < file.txt 
# This is an easy way to join a file into one line if you're in a hurry. Be careful with large data though, might truncate.

# but if you want to run different commands, well you can still do that.
echo -e "sleep 1; echo hi;\0echo hi; sleep 5;" | xargs -I {} -0 -n 1 -P 4 bash -c "{}";

# bash find Finding files which contain a certain string u
find . -type f -print | xargs grep "some string"

# Simple command to erase all the folders with a given name
# This is how to remove all folders with a given name (e.g. "CVS") starting from a root folder ('.' is the current folder)       
find . -name <fileName> -type d -print0 | xargs -0 rm -fr
        e.g. 
find . -name CVS -type d -print0 | xargs -0 rm -fr   

#Faciliate the work for lftp ('all' is needed if you wanna use it with getopts, otherwise its enough with the lftp line)
#Add this to a script and you can do ./scriptname site help or ./scriptname mput etc etc.
all="$(echo -e $*|awk '{for(i=3;i<=NF;++i)print $i}'|xargs)"; lftp -e open <HOSTNAME> -p <PORT> -u <USER>:<PASSWORD> -e "$all;exit"

# Remove all at jobs
atq | sed 's_\([0-9]\{1,8\}\).*_\1_g' | xargs atrm
# Explanation: It asks all jobs from atq, then parses a number with 1-8 digits (job id), then forwards that number via xargs to atrm
# Limitations: Only works with job id-s of up to 8 digits, but if you can find the 8, you can get around that.

# Remove files and directories whose name is a timestamp older than a certain time
ls | grep '....-..-..-......' | xargs -I {} bash -c "[[ x{} < x$(date -d '3 days ago' +%Y-%m-%d-%H%M%S) ]] && rm -rfv {}"
# Explanation: Suppose you have a backup directory with backup snapshots named by timestamp:
       #ls
	2013-05-03-103022
	2013-05-04-103033
	2013-05-05-103023
	2013-05-06-103040
	2013-05-07-103022
# You want to remove snapshots older than 3 days. The one-liner does it:
       # date
	# Tue May  7 13:50:57 KST 2013ls 
        # | grep '....-..-..-......' | sort | xargs -I {} bash -c "[[ x{} < x$(date -d '3 days ago' +%Y-%m-%d-%H%M%S) ]] && rm -rfv {}"
	# removed directory: `2013-05-03-103022'
	# removed directory: `2013-05-04-103033'
# Limitations: It does not work on OS X due to the differences between GNU date and BSD date).

# Clear the Exim Queue (remove all messages)
exim -bp | exiqgrep -i | xargs exim -Mrm

#==============================##==============================#
# CMD XARGS						       #
#==============================##==============================#
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░