当前位置:网站首页>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边栏推荐
- (pkcs1) RSA public private key PEM file parsing
- [explain the difference between operation and maintenance and network engineering]
- Battle history between redis and me under billion level traffic
- Opencv实现图像的基本变换
- AUTO PWN
- Scheduled database backup script
- ZUCC_ Principles of compiling language and compilation_ Experiment 01 language analysis and introduction
- MySQL 因字符集问题插入中文数据时提示代码 :1366
- liunx 更改 vsftpd 的端口号
- MATLAB Camera Calibrator相机标定
猜你喜欢

2021-03-09 comp9021 class 7 Notes

ZUCC_编译语言原理与编译_实验04 语言与文法

数据库迁移从PostgreSQL迁移到 MYSQL

ZUCC_编译语言原理与编译_实验03 编译器入门

什么是SRE?一文详解SRE运维体系

A preliminary study of IO model

uniapp 热更新后台管理

Permission model DAC ACL RBAC ABAC
![Fundamentals of 3D mathematics [17] inverse square theorem](/img/59/bef931d96883288766fc94e38e0ace.png)
Fundamentals of 3D mathematics [17] inverse square theorem

ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language
随机推荐
xtrabackup做数据备份
Glusterfs replacement failure brick
表单图片上传在Chorme中无法查看请求体的二进制图片信息
Two methods of QT exporting PDF files
Scénarios d'utilisation de la promesse
Qt源码分析--QObject(2)
jwt(json web token)
Application of tidb in Netease games
Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
Easycvr invokes the interface parameter acquisition method and precautions of device video recording on the page
Vscode install the remote -wsl plug-in to connect to the local WSL
Easynvr and easyrtc platforms use go language to manage projects. Summary of the use of govendor and gomod
利用ngrok做内网穿透
Increase insert speed
api平台通用签名机制
After interviewing and tutoring several children, I found some problems!
ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
更改SSH端口号
2021-03-16 comp9021 class 9 notes
ZUCC_编译语言原理与编译_实验01 语言分析与简介