Linux xargs命令解析及递归执行dos2unix命令(Linux xargs command parsing and recursive execution of dos2unix commands)

一、xargs 命令的作用

xargs命令的作用,是将标准输入转为命令行参数。

$ echo "hello world" | xargs echo
hello world

上面的代码将管道左侧的标准输入,转为命令行参数hello world,传给第二个echo命令。

xargs命令的格式如下。

$ xargs [-options] [command]

真正执行的命令,紧跟在xargs后面,接受xargs传来的参数。

xargs的作用在于,大多数命令(比如rm、mkdir、ls)与管道一起使用时,都需要xargs将标准输入转为命令行参数。

$ echo "one two three" | xargs mkdir

上面的代码等同于mkdir one two three。如果不加xargs就会报错,提示mkdir缺少操作参数。

二、-0 参数与 find 命令

由于xargs默认将空格作为分隔符,所以不太适合处理文件名,因为文件名可能包含空格。

find命令有一个特别的参数-print0,指定输出的文件列表以null分隔。然后,xargs命令的-0参数表示用null当作分隔符。

$ find /path -type f -print0 | xargs -0 rm

上面命令删除/path路径下的所有文件。由于分隔符是null,所以处理包含空格的文件名,也不会报错。

还有一个原因,使得xargs特别适合find命令。有些命令(比如rm)一旦参数过多会报错”参数列表过长”,而无法执行,改用xargs就没有这个问题,因为它对每个参数执行一次命令。

$ find . -name "*.txt" | xargs grep "abc"

上面命令找出所有 TXT 文件以后,对每个文件搜索一次是否包含字符串abc。

三、递归执行dos2unix命令

查找出所有文件,并使用null分隔文件名,同时对每个文件都执行dos2unix命令

$ find . -type f -print0 | xargs -0 dos2unix
————————

1、 What the xargs command does

The xargs command converts standard input to command line arguments.

$ echo "hello world" | xargs echo
hello world

The above code converts the standard input on the left side of the pipeline into the command line parameter Hello world and passes it to the second echo command.

The format of the xargs command is as follows.

$ xargs [-options] [command]

The command that is actually executed, immediately following xargs, accepts the parameters passed by xargs.

The purpose of xargs is that when most commands (such as RM, MKDIR, LS) are used with pipes, xargs needs to convert standard input into command-line parameters.

$ echo "one two three" | xargs mkdir

The above code is equivalent to MKDIR one two three. If xargs is not added, an error will be reported, indicating that MKDIR lacks operating parameters.

2、 – 0 parameter and find command

Because xargs uses spaces as delimiters by default, it is not appropriate to deal with file names because they may contain spaces.

The find command has a special parameter – print0, which specifies that the output file list is separated by null. Then, the – 0 parameter of the xargs command indicates null as the delimiter.

$ find /path -type f -print0 | xargs -0 rm

The above command deletes all files under the / path path path. Since the delimiter is null, there will be no error when processing file names containing spaces.

Another reason is that xargs is particularly suitable for the find command. If some commands (such as RM) have too many parameters, they will report an error “the parameter list is too long” and cannot be executed. If xargs is used, there is no problem, because it executes the command once for each parameter.

$ find . -name "*.txt" | xargs grep "abc"

After the above command finds all txt files, search each file for the string ABC.

3、 Recursive execution of dos2unix commands

Find all the files, separate the file names with null, and execute the dos2unix command for each file

$ find . -type f -print0 | xargs -0 dos2unix