xrags命令的常见用法

2021-03-07  本文已影响0人  tinychen
image

本文主要介绍在xrags命令的一些参数和常见的使用方法。

使用Linux命令的时候,我们经常使用管道符号|来进行命令之间的输入输出的传递,例如

cat example.txt | grep uuid | sort

这一条命令就可以将example.txt这个文件的内容全部打印到屏幕上,然后使用grep来查找带有uuid字符的行,接着使用sort命令来进行排序,当然我们也可以直接这样:

grep uuid example.txt | sort

两者的效果是一样的,但是对于很多命令来说管道符号|却不一定能用

例如我们需要查找所有进程中带有nginx关键字的进程并且将其全部kill掉,我们可以这样获取到对应的进程ID信息

# ps -ef | grep nginx | grep -v grep | awk '{print $2}'
12245
29913

理论上我们只需要将对应的PID全部kill掉就可以完成操作,但是却不能直接使用管道符号,因为kill命令没办法接收这样的输入信息

# 这样操作是不行的
# ps -ef | grep nginx | grep -v grep | awk '{print $2}' | kill -9

这个时候我们就可以使用xargs命令来进行操作,xargs命令会将前面命令的输入结果逐个传递给后面的kill命令

ps -ef | grep nginx | grep -v grep | awk '{print $2}' | xargs kill -9

接着我们来查看一下xargs命令的使用说明:

[/root]# xargs --help
Usage: xargs [OPTION]... COMMAND [INITIAL-ARGS]...
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.

Mandatory and optional arguments to long options are also
mandatory or optional for the corresponding short option.
  -0, --null                   items are separated by a null, not whitespace;
                                 disables quote and backslash processing and
                                 logical EOF processing
  -a, --arg-file=FILE          read arguments from FILE, not standard input
  -d, --delimiter=CHARACTER    items in input stream are separated by CHARACTER,
                                 not by whitespace; disables quote and backslash
                                 processing and logical EOF processing
  -E END                       set logical EOF string; if END occurs as a line
                                 of input, the rest of the input is ignored
                                 (ignored if -0 or -d was specified)
  -e, --eof[=END]              equivalent to -E END if END is specified;
                                 otherwise, there is no end-of-file string
  -I R                         same as --replace=R
  -i, --replace[=R]            replace R in INITIAL-ARGS with names read
                                 from standard input; if R is unspecified,
                                 assume {}
  -L, --max-lines=MAX-LINES    use at most MAX-LINES non-blank input lines per
                                 command line
  -l[MAX-LINES]                similar to -L but defaults to at most one non-
                                 blank input line if MAX-LINES is not specified
  -n, --max-args=MAX-ARGS      use at most MAX-ARGS arguments per command line
  -P, --max-procs=MAX-PROCS    run at most MAX-PROCS processes at a time
  -p, --interactive            prompt before running commands
      --process-slot-var=VAR   set environment variable VAR in child processes
  -r, --no-run-if-empty        if there are no arguments, then do not run COMMAND;
                                 if this option is not given, COMMAND will be
                                 run at least once
  -s, --max-chars=MAX-CHARS    limit length of command line to MAX-CHARS
      --show-limits            show limits on command-line length
  -t, --verbose                print commands before executing them
  -x, --exit                   exit if the size (see -s) is exceeded
      --help                   display this help and exit
      --version                output version information and exit

Report bugs to <bug-findutils@gnu.org>.

这里着重说明几个参数

参数 作用
-t 在执行对应的命令之前先将其打印出来
-a 从指定的文件中读取变量运行命令而不是标准的输入
-i 使用{}可以替换对应的变量

对于-i参数的使用可能不太能理解,我们举例进行说明:

假设我们需要对当前目录下的所有txt文件进行查找,对于文件中含有tinychen关键字的文件名全部添加一个own的后缀

grep tinychen *.log 2>/dev/null | awk -F : '{print $1}' | sort | uniq | xargs -t -i mv {} {}.own

对于上面的这个命令中使用了-i参数之后,会将前面读取到的输入与{}相关联,对于使用像mv这样的需要输入不止一个变量的命令的时候特别有用。

再举个例子,假设我们需要查找当前目录下面的txt文件,将所有带有192.168.192.168字段的行注释掉

cat *.txt 2>/dev/null | grep 192.168.192.168 | xargs -t -i sed -i 's/{}/#{}/g' *.txt
上一篇 下一篇

猜你喜欢

热点阅读