Common scenarios in xargs work

Xargs has a variety of Sao operations. This article only lists some of the more critical ones that are commonly used in work. When executing single-line commands, xargs looks concise, but it will inevitably cause reading difficulties (especially if you are not familiar with xargs) , If you are writing scripts, it is recommended not to use xargs, normal use of loops, maintenance is more convenient

Horizontal text vertical display

This kind of operation can facilitate us to filter text content in some scenarios

命令
==========
echo "1 2 3 4 5 6 7 8 9 10" | xargs -n 1 echo


输出演示
==========
[root@192_168_31_106 ~]# echo "1 2 3 4 5 6 7 8 9 10" | xargs -n 1 echo
1
2
3
4
5
6
7
8
9
10


相关解释
==========
echo "1 2 3" | xargs -n 1 echo 
# 上面这条命令等价于下面这样
echo 1
echo 2
echo 3
# 当然我们也可以利用循环实现此需求
for i in 1 2 3 4 5 6 7 8 9 10; do echo $i; done


扩展理解
==========
[root@192_168_31_106 ~]# echo "1 2 3 4 5 6 7 8 9 10" | xargs -n 2 echo
1 2
3 4
5 6
7 8
9 10
#可以看到,这里就相当于 "每次echo 2个"

[root@192_168_31_106 ~]# echo "1 2 3 4 5 6 7 8 9 10" | xargs -n 3 echo
1 2 3
4 5 6
7 8 9
10
#可以看到,这里就相当于 "每次echo 3个"

[root@192_168_31_106 ~]# echo "1 2 3 4 5 6 7 8 9 10" | xargs echo
1 2 3 4 5 6 7 8 9 10
#可以看到,这里就相当于 "每次echo 全部",跟直接echo输出的信息内容一致了

The echo here can be omitted, the default followed by xargs is the echo command

Vertical text display horizontally

Here is purely for us to fully understand the working mechanism of xargs

命令
==========
seq 10 | xargs echo


输出演示
==========
[root@192_168_31_106 ~]# seq 5
1
2
3
4
5
[root@192_168_31_106 ~]# seq 5 | xargs echo
1 2 3 4 5
[root@192_168_31_106 ~]# seq 10 | xargs -n 5 echo
1 2 3 4 5
6 7 8 9 10

With the above foreshadowing, let's list some more practical scenarios that can be used in work. What needs to be pointed out is that basically the scenarios that can be achieved by xargs can be achieved by writing for loops, but the writing is troublesome to write. Write in multiple lines in a loop

The echo here can be omitted, the default followed by xargs is the echo command

Batch kill process

ps -ef | grep ping | grep -v grep | awk '{print $2}' | xargs -n 1 kill
#上面这条命令相当于 
kill pid1
kill pid2

ps -ef | grep ping | grep -v grep | awk '{print $2}' | xargs kill
#上面这条命令相当于
kill pid1 pid2

可以看到这两种写法等价,因为对于kill命令来说,这两种写法等价

Use with find

1 将/data目录下的修改时间是7天以前,并且大于100k的文件复制到到/tmp目录下
===================
find /data/ -mtime +7 -type f -size +100k | xargs cp -t /tmp/
这条命令就有点意思了,首先我们直到find命令返回的是垂直排列的文件名列表,而使用cp拷贝文件,常用的方法是下面这样
cp file1 file2 file3 /tmp
可以看到,需要拷贝的文件在中间,而xargs需要的参数在末尾,因此我们利用cp命令的 -t 参数


2 查找出系统中大于50k且小于100k的文件,把文件中的的hello替换为为world
===================
find / -type f -size +50k -size -100k | xargs sed -i 's#hello#world#g'

-I parameter temporary replacement

#前面提到过的下面这个用法,如果大家觉得理解不方便
find /data/ -mtime +7 -type f -size +100k | xargs cp -t /tmp/
#我们还可以这样写
find /data/ -mtime +7 -type f -size +100k | xargs -I mytmp cp mytmp /tmp/
#可以看到,这里我们用mytmp来代指前面xargs找到的内容,这里的mytmp可以任意起一个名称

Delete files in bulk

find /data/ -mtime +7 -type f -size +100k | xargs rm -rf
上面的情况不用xargs可以写喜循环实现,还可以下面这样
rm -rf `find /data/ -mtime +7 -type f -size +100k`

If you delete too many files, such as hundreds of thousands or millions, it is not recommended to use xargs. The performance is not good. Once I used xargs to delete more than 200,000 Kafka topics at work and deleted them overnight, so we honestly use loops. Just delete it, I usually use something like the following

for i in `find /data/ -mtime +7 -type f -size +100k`; do
echo "rm -rf $i" >> /tmp/tmp.sh
done

However, we still need to understand this deletion method to avoid someone showing off their skills in front of us, or other people's scripts. We can only do what we are doing here.

The processed file name contains spaces

If there are spaces in the file to be processed, and some special characters (the file name should not contain special characters), an additional parameter is required, as shown in the following example

touch hello-world
touch "hello world1"
touch "hello world2"

[root@192_168_31_106 ~/xtmp]# ls | xargs ls
ls: cannot access hello: No such file or directory
ls: cannot access world1: No such file or directory
ls: cannot access hello: No such file or directory
ls: cannot access world2: No such file or directory
hello-world

可以看到,由于"hello world1"被拆分成了两个文件,所以报错文件找不到,我们可以通过 --null (或 -0)参数解决次问题
find . -type f  -print0 | xargs --null -n 1 ls

Find -print0and xargs --nullare often used in conjunction to deal with the problem of
file names with spaces. There are many pits in file names that contain spaces. We must first carefully sort out the business scenarios. Why do the file names contain spaces and solve the problem from the source as much as possible , To prevent the existence of spaces in the file name

Print out the command to be executed

# -t OR --verbose 参数
[root@192_168_31_106 ~/xtmp]# echo 'one two three' | xargs -n 1 -t  touch
touch one 
touch two 
touch three 
[root@192_168_31_106 ~/xtmp]# ls
one  three  two

另一个测试
ls | xargs -t -I mytmp cp -a mytmp /tmp/

Give a confirmation prompt before executing the command

[root@192_168_31_106 ~/xtmp]# echo 'hello1 hello2 hello3' | xargs -n 1 -p  touch
touch hello1 ?...y
touch hello2 ?...y
touch hello3 ?...y
[root@192_168_31_106 ~/xtmp]# ls
hello1  hello2  hello3

可以输入 y/Y/yes 来表示确认执行

Reference

http://www.ruanyifeng.com/blog/2019/08/xargs-tutorial.html

Guess you like

Origin blog.csdn.net/xys2015/article/details/113870654