Golfreeze.packetlove.com: Life style of Golfreeze Canon400D Family kammtan.com Jazz Freebsd Unix Linux System Admin guitar Music
		All about unix linux freebsd and FAQ for Packetlove.com Web hosting , Mail hosting , VoIP + IP PBX server => command , shell script  ,tool , crontab => Topic started by: golfreeze on เมษายน 05, 2020, 04:15:41 pm
		
			
			- 
				=== Basic command xargs เป็นการส่งผ่าน command echo เพื่อนำค่านั้นไปรันงานต่อ 
=== -t = verbose
echo  "file1 file2 file3" | xargs -t touch
=result
touch file1 file2 file3
=== -p = prompt interactive
echo  "file1 file2 file3" | xargs -p touch
=result
touch file1 file2 file3 ?...y
=== -n show 1 column with touch 
echo  "file1 file2 file3" |  xargs -n 1 -t touch
As you can see from the verbose output below, the touch command is executed separately for each argument:
=result
touch file1
touch file2
touch file3
=== -n show number string 
echo a b c d e f | xargs -n 3
a b c
d e f
=== read value from file ips.txt 
== ips.txt
8.8.8.8
1.1.1.1
xargs -t -L 1 -a ips.txt ping -c 1
=result
ping -c 1 8.8.8.8 
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=50 time=68.1 ms
...
ping -c 1 1.1.1.1 
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=21.4 ms
===Combine find command and Xargs with Grep command 
find . -name '*.c' | xargs grep ‘stdlib.h’
==== Combind find command with rm file with space file name
find . -name "*.c" -print0 | xargs -0 rm -rf
			 
			
			- 
				มีโจทย์อยู่ว่า มีชื่อไฟล์ดังนี้ 
domain1_215sati.php
domain2_215sati.php
domain3_215sati.php
.
.
domain50_215sati.php
ต้องการเปลี่ยนชื่อไฟล์เป็น 
domain1_99packetlove.php
domain2_99packetlove.php
domain3_99packetlove.php
.
.
domain50_99packetlove.php
ถ้ามีเวลาเราก็นั่ง mv ทีละไฟล์ได้ แต่ถ้าเวลาน้อยก็นี่เลย 1 command 
ls -alh | grep 215sati | awk '{print $9}' | grep \.php$ | sed 'p;s/215sati.php/99packetlove.php/' | xargs -n2 mv
เท่านี้ก็เสร็จภายใน 2 วินาที ครับ  8)
			 
			
			- 
				1.txt
2.txt
3.txt
4.txt
5.txt
ทำการ rename นามสกุลไฟล์จาก .txt -> .text
ls | cut -d. -f1 | xargs -I {} mv {}.txt {}.text
เท่านี้ก็จะได้เป็น 
1.text
2.text
3.text
4.text
5.text
 8)
			 
			
			- 
				== create 10 files with extension .txt with xargs 
seq 10 | xargs -I {} touch {}.txt 
			 
			
			- 
				=== delete all *.txt command with xargs faster than "-exec rm {} \;"  ;) 
find . -type f -name "*.txt" | xargs rm 
=="-exec rm" more slowly than "xargs rm"  :-X
find . -type f -name "*.txt" -exec rm {} \;
			 
			
			- 
				=== grep only name of /etc/password and show in 1 line 
cut -d: -f1 < /etc/passwd | sort | xargs
			 
			
			- 
				=== echo all name in "/home" and show as /home/test/{}
cd /home/
ls | xargs -I {} echo "/home/test/{}" 
			 
			
			- 
				==show verbose "-t" and -n "max-args, --max-args=max-args per command line" and show echo filename 
ls | cut -d. -f1 | xargs -t -n 1
			 
			
			- 
				==มีโจทย์ว่าต้องการเปลี่ยน chown ของ folder โดยเพิ่มชื่อโดเมนเข้าไปด้วย แล้วให้เปลี่ยนทุกไฟล์ใน folder นั้นๆ
ls -lah | awk '{print $10}'  | grep vditest01 | xargs -I {} chown -R {}@dpa.or.th:domain\ users@dpa.or.th {}
			 
			
			- 
				=== ถ้าเรา grep output ชื่อสักอย่าง ได้ออกมาเป็น column ที่9 แล้วจะทำให้ ข้อมูลที่เรียงเป็น column นั้นออกมาเป็นแถวเดียวต่อกัน และเว้นช่องว่างด้วย ,_ สามารถทำได้ดังนี้คือ
ls -lah | awk '{print $9}'  | grep [a-z] | xargs -t | sed 's/ /, /g'
 :-[