xargs
命令的作用,是将标准输入转为命令行参数(使用xargs的原因是, 很多命令不支持标准输入传递参数, 如echo
, ls
)
默认情况下, xargs
将换行符和空格作为分隔符, 把标准输入分解成一个个命令行参数
-d 参数可以更改分隔符
input file names are terminated by the specified character delim instead of by whitespace
-L max-lines
Use at most max-lines non-blank input lines per command line.
for -L
the argument is mandatory.
Trailing blanks cause an input line to be logically continued on the next input line
The -l
form of this option is deprecated in favour of the POSIX-compliant -L
option.
-n max-args
Use at most max-args arguments per command line
-I 指定每一项命令行参数的替代字符串
replace occurrences of replace-str in the initial arguments with names read from standard input.
the -i
option is deprecated in favour of the -I
option
-t
print the command line on the standard error output before executing it
1
2
3
4
5
6
|
# 列出当前文件夹下文件内容包含 'drafts' 的文件并移动到 /root/ 文件夹下
grep -rl 'drafts' . | xargs mv -t /root/
# 复制所有图片文件到 /data/images 目录下
ls *.jpg | xargs -n1 -I {} cp {} /data/images
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# xargs
# Execute a command with piped arguments coming from another command, a file, etc.
# The input is treated as a single block of text and split into separate pieces on spaces, tabs, newlines and end-of-file.
# More information: <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/xargs.html>.
# Run a command using the input data as arguments:
arguments_source | xargs command
# Run multiple chained commands on the input data:
arguments_source | xargs sh -c "command1 && command2 | command3"
# Delete all files with a `.backup` extension (`-print0` uses a null character to split file names, and `-0` uses it as delimiter):
find . -name '*.backup' -print0 | xargs -0 rm -v
# Execute the command once for each input line
# replacing any occurrences of the placeholder (here marked as `_`) with the input line:
arguments_source | xargs -I _ command _ optional_extra_arguments
|
xargs 命令教程 - 阮一峰的网络日志
xargs options (GNU Findutils 4.9.0)
Linux xargs 命令 | 菜鸟教程