All about unix linux freebsd and FAQ for Packetlove.com Web hosting , Mail hosting , VoIP + IP PBX server > command , shell script ,tool , crontab
basic command for xargs by golfreeze
golfreeze:
=== 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
golfreeze:
มีโจทย์อยู่ว่า มีชื่อไฟล์ดังนี้
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)
golfreeze:
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)
golfreeze:
== create 10 files with extension .txt with xargs
seq 10 | xargs -I {} touch {}.txt
golfreeze:
=== 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 {} \;
นำร่อง
[0] ดัชนีข้อความ
[#] หน้าถัดไป
Go to full version