Outils pour utilisateurs

Outils du site


tech:notes_commande_xargs

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
tech:notes_commande_xargs [2025/03/24 15:06] – créée - modification externe 127.0.0.1tech:notes_commande_xargs [2026/06/07 19:12] (Version actuelle) – modification externe 127.0.0.1
Ligne 1: Ligne 1:
 +<!DOCTYPE markdown>
 +{{tag>Brouillon Shell CA}}
 +
 +# Notes commande xargs
 +
 +
 +Strip char / trim => utiliser `xargs` ex :
 +~~~bash
 +load5=$(echo $UPTIME | awk -F, '{print $5}' | xargs)
 +~~~
 +
 +md5sum multithread
 +~~~bash
 +#find . -not -name "md5sum.txt" -follow -type f -exec md5sum {} \; > md5sum.txt
 +find . -not -name "md5sum.txt" -follow -type f -print0 | xargs -0 -L1 -P"$(nproc)" md5sum > md5sum.txt
 +~~~
 +
 +~~~bash
 +dig A +short acme.local | xargs -P5 -I '{}' bash -c 'ping -c 1 -W 2 {} >/dev/null 2>&1 || echo {}'
 +~~~
 +
 +~~~bash
 +cat 20_ok_ping.txt | xargs -L1 -I '{}' -P50 bash -c 'SRV={} ; ssh -p 22 -o PasswordAuthentication=no -o ChallengeResponseAuthentication=no -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no -o ConnectTimeout=3 root@${SRV} true 2>/dev/null && echo +${SRV} || echo -${SRV}' | tee 30_ssh_p22.txt
 +~~~
 +
 +~~~bash
 +pidof sleep | xargs kill -9
 +~~~
 +
 +Renommer tous les fichiers *.txt d'un répertoire
 +~~~bash
 +ls -1 ${repertoire}/*.txt | xargs -I {} -t mv {} {}.csv
 +~~~
 +
 +
 +~~~bash
 +cat bash-xargs-demo.txt | xargs -I % sh -c 'echo %; mkdir %'
 +xargs -a bash-xargs-demo.txt -I % sh -c 'echo %; mkdir %'
 +~~~
 +
 +
 +Prompt before launch
 +~~~bash
 +find * | xargs -p rm -rf
 +~~~
 +
 +use xargs to copy a file to multiple directories at once; in this example we are trying to copy the file.
 +~~~bash
 +echo ./Templates/ ./Documents/ | xargs -n 1 cp -v ./Downloads/SIC_Template.xlsx 
 +~~~
 +
 +A la place de '-L' pour le nombre de ligne, il est possible de préciser le nombre d'arguments avec '-n'
 +~~~bash
 +echo /etc/* |xargs -n 1 basename
 +~~~
 +
 +Avec délimitateur 
 +~~~
 +$ echo 'my new file' | xargs -d '\n' touch
 +$ ls -l
 +total 0
 +-rw-r--r--. 1 shs shs 0 Oct 15 12:41  file1
 +-rw-r--r--. 1 shs shs 0 Oct 15 12:41  file2
 +-rw-r--r--. 1 shs shs 0 Oct 15 12:41  file3
 +-rw-r--r--. 1 shs shs 0 Oct 15 12:57 'my new file'
 +~~~
 +
 +~~~
 +$ echo "1,2,3,4,5" | xargs -d, echo 
 +1 2 3 4 5
 +~~~
 +
 +~~~
 +$ echo "1,2,3,4,5" | xargs -d, -L 1 echo 
 +1
 +2
 +3
 +4
 +5
 +~~~
 +
 +Copie de fichiers
 +~~~bash
 +find /backup/ -type f | xargs -n1 -i cp {} /var/www/backup/ 
 +~~~
 +
 +Curl - flood
 +~~~bash
 +seq 1 200 | xargs -n1 -P10  curl "http://localhost:5000/example"
 +~~~
 +
 +Curl - test urls
 +~~~bash
 +xargs -a urls.txt -L1 -I '{}' -P50 bash -c 'echo $( curl --connect-timeout 2 -k -s -o /dev/null -w "%{http_code};" "{}" ; echo "{}" )' | tee ping_urls.csv
 +~~~
 +
 +
 +Curl can now fetch several websites in parallel
 +~~~bash
 +curl --parallel --parallel-immediate --parallel-max 3 --config websites.txt
 +~~~
 +
 +`websites.txt`
 +~~~python
 +url = "website1.com"
 +url = "website2.com"
 +url = "website3.com"
 +~~~
 +
 +
 +
 +
 +
 +
 +
 +### xargs avec Ansible
 +
 +Voir aussi :
 +* [[Ansible - Accélérer Ansible grâce au module Mitogen]]
 +* traitements_paralleles [[notes_ansible]]
 +
 +~~~bash
 +xargs -a inventory.lst -L1 -I % -P50 bash -c "ansible --ssh-common-args='-o StrictHostKeyChecking=no' -i %, -e ansible_port=2222 -b -u admin -m command -a 'grep -q 192\.168\.223\.0 /etc/hosts.allow' all" |tee outpout.log
 +~~~
 +
 +4 GB RAM is recommended per 100 forks... about 100MB per fork... The baseline value for [CPU capacity for forks] is 4 forks per core.
 +
 +Ref : https://docs.ansible.com/ansible-tower/3.4.3/html/userguide/jobs.html#at-capacity-determination-and-job-impact
 +
 +
 +
 +
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki