linux命令之xargs, awk

xargs主要用于从标准输入读取信息给执行的命令,例如:

docker ps -q -a | xargs docker rm

awk是一个可编程的文本处理命令

在awk的命令里,你可以使用变量,逻辑语句,正则表达式等去处理文件中的文本。使用示例:docker rmi $(docker images | grep "" | awk '{print $3}')

https://www.geeksforgeeks.org/awk-command-unixlinux-examples/

操作示例:

How to use xargs


By default xargs reads items from standard input as separated by blanks and executes a command once for each argument. In the following example standard input is piped to xargs and the mkdir command is run for each argument, creating three folders.

echo 'one two three' | xargs mkdir
ls
one two three


How to use xargs with find


The most common usage of xargs is to use it with the find command. This uses find to search for files or directories and then uses xargs to operate on the results. Typical examples of this are removing files, changing the ownership of files or moving files.

find and xargs can be used together to operate on files that match certain attributes. In the following example files older than two weeks in the temp folder are found and then piped to the xargs command which runs the rm command on each file and removes them.

find /tmp -mtime +14 | xargs rm