当前位置:网站首页>Using skills of xargs -- the way to build a dream
Using skills of xargs -- the way to build a dream
2022-06-24 08:44:00 【The road to dream】
Unix Commands have arguments , Some commands are acceptable ” The standard input (stdin)” As a parameter . Pipeline command (|) The role of , Is to convert the standard output of the command on the left to standard input , The command provided to the right is used as a parameter . although , stay Unix Most commands in the system do not accept standard input as parameters , You can only enter parameters directly on the command line , This makes it impossible to pass parameters with pipeline commands . such as , What we use everyday echo Command does not accept pipeline parameters . and xargs Role of command , Is to convert standard input to command line parameters .
Be careful :xargs The following default is echo command , So it can be used alone
Help information
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
Example :
Basic use :
# Convert standard input to command line arguments
echo "hello rumenz" | xargs echo
# grep Accept pipeline parameters
cat /etc/passwd | grep root
# echo The command does not accept pipeline parameters
echo "hello rumenz" | echo
Parameters :
-d Specify the separator , By default, space division is used
# Space as separator
echo "one two three" | xargs mkdir
# Specify tab as separator
echo -e "a\tb\tc" | xargs -d "\t" echo
-p Print out the command to be executed and ask the user whether to execute
echo 'one two three' | xargs -p touch
-0 To express with null As a separator
find The command has a special parameter -print0, Used to specify the list of files to output null As a separator
find /path -type f -print0 | xargs -0 rm
# Specify how many lines to use as a command line parameter
xargs -L 1 find -name "*.txt"
-n Specify how many items to use as command line arguments at a time
echo {0..9} | xargs -n 2 echo
# Specify an alternate string for each command line parameter
# Pass command line parameters to multiple commands
cat foo.txt
one
two
three
cat foo.txt | xargs -I {} sh -c 'echo {}; mkdir {}'
one
two
three
ls
one two three
# Convert multi line input to single line input
> 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
# Convert single line text to multiple lines
> 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
# Specify the delimiter for line conversion
> echo "rumenz:123:rumenz:456:rumenz:789" | xargs -d : -n 2
rumenz 123
rumenz 456
rumenz 789
# xargs and find combination
find . -type f -name "*.txt" -print | xargs rm -f
# Download files in bulk
cat rumenz.txt | xargs wget -c边栏推荐
- Pyqt common system events
- xtrabackup做数据备份
- uniapp 热更新后台管理
- ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
- K8S部署高可用postgresql集群 —— 筑梦之路
- Application of tidb in Netease games
- 数据库迁移从PostgreSQL迁移到 MYSQL
- [life thinking] planning and self-discipline
- QT source code analysis -- QObject (2)
- There was an error checking the latest version of pip
猜你喜欢

Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)

【关于运维和网工的差别,一文说透】

New technology practice, encapsulating the permission application library step by step with the activity results API

Two methods of QT exporting PDF files

Qt 中发送自定义事件

Vscode install the remote -wsl plug-in to connect to the local WSL

À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL

關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL

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

日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
随机推荐
DataX User Guide
String转Base64
Application of tidb in Netease games
【力扣10天SQL入门】Day2
How to mount a USB hard disk with NTFS file format under RHEL5 system
Video Fusion communication has become an inevitable trend of emergency command communication. How to realize it based on easyrtc?
Redis cluster data skew
Introduction to NC machine tool programming [G-code]
Three categories of financial assets under the new standards: AMC, fvoci and FVTPL
【关于运维和网工的差别,一文说透】
How to replace the web player easyplayerproactivex Key in OCX?
Jenkins自动化部署,连接不到所依赖的服务【已解决】
数据平台简介
After interviewing and tutoring several children, I found some problems!
2021-06-25: a batch of strings consisting only of lowercase letters (a~z) are put
Redis的Cluster集群数据倾斜
How to handle the problem that calling easycvr address integration cannot be played through easyplayer player?
(pkcs1) RSA public private key PEM file parsing
Tencent conference API - get rest API & webhook application docking information
Variable declaration and some special variables in shell