当前位置:网站首页>xargs使用技巧 —— 筑梦之路
xargs使用技巧 —— 筑梦之路
2022-06-24 07:05:00 【筑梦之路】
Unix命令都带有参数,有些命令可以接受”标准输入(stdin)”作为参数。而管道命令(|)的作用,是将左侧命令的标准输出转换为标准输入,提供给右侧命令作为参数使用。虽然,在 Unix 系统中大多数命令都不接受标准输入作为参数,只能直接在命令行输入参数,这导致无法用管道命令传递参数。比如,我们日常使用的 echo 命令就不接受管道传参。而 xargs 命令的作用,就是将标准输入转为命令行参数。
注意:xargs 后面的默认跟的是 echo 命令,所以它可以单独使用
帮助信息
xargs --help
Usage: xargs [OPTION]... COMMAND INITIAL-ARGS...
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.
Mandatory arguments to long options are mandatory for short options too.
Non-mandatory arguments are indicated by [square brackets]
-0, --null Items are separated by a null, not whitespace.
Disables quote and backslash processing
-a, --arg-file=FILE Read arguments from FILE, not standard input
-d, --delimiter=CHARACTER Input items are separated by CHARACTER, not by
blank space. Disables quote and backslash
processing
-E END If END occurs as a line of input, the rest of
the input is ignored.
-e [END], --eof[=END] Equivalent to -E END if END is specified.
Otherwise, there is no end-of-file string
--help Print a summary of the options to xargs.
-I R same as --replace=R (R must be specified)
-i,--replace=[R] Replace R in initial arguments with names
read from standard input. If R is
unspecified, assume {}
-L,-l, --max-lines=MAX-LINES Use at most MAX-LINES nonblank input lines per
command line
-l Use at most one nonblank input line per
command line
-n, --max-args=MAX-ARGS Use at most MAX-ARGS arguments per command
line
-P, --max-procs=MAX-PROCS Run up to 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, run no command.
If this option is not given, COMMAND will be
run at least once.
-s, --max-chars=MAX-CHARS Limit commands to MAX-CHARS at most
--show-limits Show limits on command-line length.
-t, --verbose Print commands before executing them
--version Print the version number
-x, --exit Exit if the size (see -s) is exceeded
示例:
基本使用:
# 将标准输入转为命令行参数
echo "hello rumenz" | xargs echo
# grep接受管道传参
cat /etc/passwd | grep root
# echo 命令不接受管道传参
echo "hello rumenz" | echo
参数:
-d 指定分隔符,默认使用空格分割
# 空格作为分隔符
echo "one two three" | xargs mkdir
# 指定制表符为分隔符
echo -e "a\tb\tc" | xargs -d "\t" echo
-p 打印出要执行的命令并询问用户是否要执行
echo 'one two three' | xargs -p touch
-0 表示用 null 当作分隔符
find命令有一个特别的参数-print0,用来指定输出的文件列表以null作为分隔符
find /path -type f -print0 | xargs -0 rm
# 指定多少行作为一个命令行参数
xargs -L 1 find -name "*.txt"
-n 指定每次将多少项作为命令行参数
echo {0..9} | xargs -n 2 echo
# 指定每一项命令行参数的替代字符串
# 将命令行参数传给多个命令
cat foo.txt
one
two
three
cat foo.txt | xargs -I {} sh -c 'echo {}; mkdir {}'
one
two
three
ls
one two three
# 将多行输入转换成单行输入
> cat rumenz.txt
1 2 3 4
5 6
7 8 9
> cat rumenz.txt | xargs
1 2 3 4 5 6 7 8 9
# 将单行文本转换成多行
> cat rumenz.txt
1 2 3 4 5 6 7 8 9
> cat rumenz.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
# 指定分隔符进行行转换
> echo "rumenz:123:rumenz:456:rumenz:789" | xargs -d : -n 2
rumenz 123
rumenz 456
rumenz 789
# xargs和find结合
find . -type f -name "*.txt" -print | xargs rm -f
# 批量下载文件
cat rumenz.txt | xargs wget -c边栏推荐
- Glusterfs replacement failure brick
- orb slam build bug: undefined reference to symbol ‘_ZN5boost6system15system_categoryEv‘
- JS merge multiple objects and remove duplicates
- 定时备份数据库脚本
- Cloudbase database migration scheme
- ZUCC_编译语言原理与编译_实验06 07 语法分析 LL 分析
- Fund raising, trading and registration
- Using ngrok for intranet penetration
- Shell array
- Three categories of financial assets under the new standards: AMC, fvoci and FVTPL
猜你喜欢

ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar

MySQL 因字符集问题插入中文数据时提示代码 :1366

Redis cluster data skew

Opencv实现图像的基本变换

分布式 | 如何与 DBLE 进行“秘密通话”

2021-03-11 comp9021 class 8 notes
![[explain the difference between operation and maintenance and network engineering]](/img/2b/945f468588e729336e2e973e777623.jpg)
[explain the difference between operation and maintenance and network engineering]

Jenkins自动化部署,连接不到所依赖的服务【已解决】

Markdown to realize text link jump

为什么ping不通,而traceroute却可以通
随机推荐
【生活思考】计划与自律
Why do you want to file? What are the advantages and disadvantages of website filing?
The reason why the qtimer timer does not work
(pkcs1) RSA public private key PEM file parsing
Using ngrok for intranet penetration
uniapp 热更新后台管理
解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
Live broadcast appointment: growth of Mengxin Product Manager
DataX User Guide
win11在cmder中使用vim查看内容的时候空白
MATLAB Camera Calibrator相机标定
05 Ubuntu installing mysql8
更改SSH端口号
Numpy 中的方法汇总
xtrabackup做数据备份
How to improve the customer retention rate in the operation of independent stations? Customer segmentation is very important!
String转Base64
Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)
Qt导出PDF文件的两种方法
利用sonar做代码检查