Author Topic: basic command for xargs by golfreeze  (Read 3573 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
basic command for xargs 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

« Last Edit: กันยายน 04, 2022, 10:30:55 AM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: basic command for xargs
« Reply #1 on: กุมภาพันธ์ 03, 2021, 07:23:16 PM »
มีโจทย์อยู่ว่า มีชื่อไฟล์ดังนี้

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

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: basic command for xargs
« Reply #2 on: กรกฎาคม 28, 2021, 10:22:31 PM »
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

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: basic command for xargs
« Reply #3 on: กันยายน 04, 2022, 10:30:43 AM »
== create 10 files with extension .txt with xargs
seq 10 | xargs -I {} touch {}.txt

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: basic command for xargs by golfreeze
« Reply #4 on: กันยายน 04, 2022, 10:34:02 AM »
=== 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 {} \;
« Last Edit: กันยายน 04, 2022, 10:37:00 AM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: basic command for xargs by golfreeze
« Reply #5 on: กันยายน 04, 2022, 11:47:56 AM »
=== grep only name of /etc/password and show in 1 line
cut -d: -f1 < /etc/passwd | sort | xargs

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: basic command for xargs by golfreeze
« Reply #6 on: กันยายน 04, 2022, 11:50:40 AM »
=== echo all name in "/home" and show as /home/test/{}
cd /home/
ls | xargs -I {} echo "/home/test/{}"

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: basic command for xargs by golfreeze
« Reply #7 on: กันยายน 04, 2022, 11:57:30 AM »
==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

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: basic command for xargs by golfreeze
« Reply #8 on: กันยายน 10, 2022, 08:39:24 PM »
==มีโจทย์ว่าต้องการเปลี่ยน chown ของ folder โดยเพิ่มชื่อโดเมนเข้าไปด้วย แล้วให้เปลี่ยนทุกไฟล์ใน folder นั้นๆ
ls -lah | awk '{print $10}'  | grep vditest01 | xargs -I {} chown -R {}@dpa.or.th:domain\ users@dpa.or.th {}

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: basic command for xargs by golfreeze
« Reply #9 on: กันยายน 10, 2022, 08:58:20 PM »
=== ถ้าเรา grep output ชื่อสักอย่าง ได้ออกมาเป็น column ที่9 แล้วจะทำให้ ข้อมูลที่เรียงเป็น column นั้นออกมาเป็นแถวเดียวต่อกัน และเว้นช่องว่างด้วย ,_ สามารถทำได้ดังนี้คือ
ls -lah | awk '{print $9}'  | grep [a-z] | xargs -t | sed 's/ /, /g'

 :-[