10 xargs command example in linux

本文详细介绍了Unix/Linux环境下xargs命令的多种应用实例,包括与find、grep等命令结合使用的方法,用于处理文件名列表、查找特定关键词、删除临时文件、处理包含空格或换行符的文件名等场景。通过这些示例,读者可以更好地掌握xargs命令的用法,提升在Unix/Linux系统中的操作效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:http://javarevisited.blogspot.com/2012/06/10-xargs-command-example-in-linux-unix.html

为了可以不翻墙看,转载在此。


10 xargs command example in Linux - Unix tutorial

xargs command in unix or Linux is a powerful command used in conjunction with find and grep command in UNIX to divide a big list of arguments into small list received from standard input. find and grep command produce long list of file names and we often want to either remove them or do some operation on them but many unix operating system doesn't accept such a long list of argument. UNIX xargs command divide that list into sub-list with acceptable length and made it work. This Unix tutorial is in continuation of my earlier post on Unix like 10 examples of chmod command in Unix and How to update soft link in Linux. If you haven’t read those unit tutorial than check them out. By the way In this tutorial we will see different example of unix xargs command to learn how to use xargs command with find and grep and other unix command and make most of it. Though what you can do with xargs in unix can also be done by using options provided in find but believe xargs is much easy and powerful.

Unix Xargs command example

Following is list of examples of xargs command which shows how useful knowledge of xargs can be. Feel free to copy and use this command and let me know if it didn’t work in any specific Unix operating system like AIX or Solaris.

Xargs Example 1- with and without xargs
in this example of xargs command we will see how output changes with use of xargs command in unix or Linux. Here is the output of find command without xargs first and than with xargs, you can clearly see that multiline output is converted into single line:

devuser @system: /etc find . -name "*bash*"
. /bash.bashrc
. /bash.bash_logout
. /defaults /etc /bash.bashrc
. /defaults /etc /bash.bash_logout
. /defaults /etc /skel /.bashrc
. /defaults /etc /skel /.bash_profile
. /postinstall /bash.sh.done
. /setup /bash.lst.gz
. /skel /.bashrc
. /skel /.bash_profile

devuser @system: /etc find . -name "*bash*" | xargs
. /bash.bashrc . /bash.bash_logout . /defaults /etc /bash.bashrc . /defaults /etc /bash.bash_logout . /defaults /etc /skel /.bashrc . /defaults /etc /skel /.bash_profile . /postinstall /bash.sh.done . /setup /bash.lst.gz . /skel /.bashrc . /skel /.bash_profile


Xargs Example 2 – xargs and grep
Another common use fo unix xargs command is to first find the files and then look for specific keyword on that file using grep command. here is an example of xargs command that does it

find . -name "*.java" |xargsgrep "Stock"

This will first find all java files from current directory or below and than on each java file look for word "Stocks"if you have your development environment setup in Linux or unix this is a great tool to find function references or class references on java files.

Xargs Example 3 – delete temporary file using find and xargs
Another common example of xargs command in unix is removing temporary files from system.

find/ tmp -name "*.tmp" |xargsrm

This will remove all .tmp file from /tmp or below directory. xargs in unix is very fast as compared to deleting single file at a time which can also be done by using find command alone. By the way this is also a very popular Unix interview question.

Xargs Example 4 – xargs -0 to handle space in file name
xargs command examples in Unix and Linux - TutorialAbove example of xargs command in unix will not work as expected if any of file name contains space or new line on it. to avoid this problem we use find -print0 to produce null separated file name and xargs-0 to handle null separated items. Here is an example of xargs command in unix which can handle file name with spaces and newline:

find/ tmp -name "*.tmp" -print0 |xargs -0 rm

Xargs Example 5 – xargs and cut command in Unix
Though most of xargs examples in unix will be along with find and grep command but xargs is not just limited to this two it can also be used with any command which generated long list of input for example we can use xargs with cut command in unix. In below example of unix xargs we will xargs example with cut command. for using cut command let's first create a .csv file with some data e.g.

devuser @system: /etc cat smartphones.csv
Iphone,Iphone4S
Samsung,Galaxy
LG,Optimus
HTC,3D

Now we will display name of mobile companies from first column using xargs command in one line:

devuser @system: /etc cut -d, -f1 smartphones.csv | sort | xargs
HTC Iphone LG Samsung

xargs Example 6 – command convert muti line output into single line
One more common example of xargs commands in Linux is by converting output of one command into one line. For example you can run any command and then combine xargs to convert output into single line. here is an example xargs in unix which does that.

devuser @system:~ /perl ls -1 *.txt
derivatives.txt
futures.txt
fx.txt
options.txt
stock.txt
swaps.txt

devuser @system:~ /perl ls -1 *.txt | xargs
derivatives.txt futures.txt fx.txt options.txt stock.txt swaps.txt


Xargs Example 7 - Counting number of lines in each file using xargs and find.
In this example of xargs command in unix we will combine "wc" with xargs and find to count number of lines in each  file, just like we did in our previous example with grep where we tried to find specific word in each Java file.

devuser @system:~ /perl ls -1 *.txt | xargs wc -l
  0 derivatives.txt
  2 futures.txt
  0 fx.txt
  1 options.txt
  3 stock.txt
  0 swaps.txt

Xargs command Example 7 - Passing subset of arguments to xargs in Linux.
Some commands in unix can only work at certain number of argument e.g. diff command needs two argument. when used with xargs you can use flag "-n" to instruct xargs on how many argument it should pass to given command. this xargs command line option is extremely useful on certain situation like repeatedly doing diff etc. xargs in unix or Linux will continue to pass argument in specified number until it exhaust all input. here is an example of unix xargs command with limited argument:

devuser @system:~ /perl ls -1 *.txt | xargs -n 2 echo
derivatives.txt futures.txt
fx.txt options.txt
stock.txt swaps.txt

In this example,  xargs is passing just two files at a time to echo as specified with "-n 2" xargs command line option.

Xargs example 9 - avoid "Argument list too long"
xargs in unix or Linux was initially use to avoid "Argument list too long" errors and by using xargs you send sub-list to any command which is shorter than "ARG_MAX" and that's how xargs avoid "Argument list too long" error. You can see current value of "ARG_MAX" by using getconf ARG_MAX. Normally xargs own limit is much smaller than what modern system can allow, default is 4096. You can override xargs sub list limit by using "-s" command line option.

Xargs Example 10 – find –exec vs find + xargs
xargs with find command is much faster than using -exec on find. since -exec runs for each file while xargs operates on sub-list level. to give an example if you need to change permission of 10000 files xargs with find will be almost 10K time faster than find with -exec because xargs change permission of all file at once. For more examples of find and xargs see my post 10 frequently used find command examples in Linux


Important points on xargs command in Unix and Linux

Now let’s revise some important points about xargs command in Unix which is worth remembering :

1. An important point to note about xargs is that it doesn't handle files which has newlines or white space in its name and to avoid this problem one should always use "xargs -0". xargs -o change separator to null character so its important that input feed to xargs is also use null as separator. for example gnu find command use -print0 to produce null separated file names.

2. Xargs command receives command from standard input which is by default separated with space or newlines and
execute those commands, you can still use double quotes or single quote to group commands.

3. If you don't give any command to xargs in unix, default command executed by xargs is /bin/echo and it will simply display file names.

4. One rare problem with xargs is end of file string, by default end of file string is "_" and if this string occurs in input the rest of input is ignored by xargs. Though you can change end of file string by using option "-eof".

5. Use man xargs or xargs --help to get help on xargs online while working in unix or Linux.

In short xargs command in Unix or Linux is an essential tool which enhances functionality of front line commands like find, grep or cut and gives more power to your shell script. These xargs command examples are good start for anyone wants to learn more about xargs command.Though these xargs examples are tested in Linux environment they will applicable for other Unix systems likes Solaris or AIX also. let us know if you face any issue while using these examples.

Other Linux command tutorials from Javarevisited blog:

Read more: http://javarevisited.blogspot.com/2012/06/10-xargs-command-example-in-linux-unix.html#ixzz2OKE7sC53
【基于QT的调色板】是一个使用Qt框架开发的色彩选择工具,类似于Windows操作系统中常见的颜色选取器。Qt是一个跨平台的应用程序开发框架,广泛应用于桌面、移动和嵌入式设备,支持C++和QML语言。这个调色板功能提供了横竖两种渐变模式,用户可以方便地选取所需的颜色值。 在Qt中,调色板(QPalette)是一个关键的类,用于管理应用程序的视觉样式。QPalette包含了一系列的颜色角色,如背景色、前景色、文本色、高亮色等,这些颜色可以根据用户的系统设置或应用程序的需求进行定制。通过自定义QPalette,开发者可以创建具有独特视觉风格的应用程序。 该调色板功能可能使用了QColorDialog,这是一个标准的Qt对话框,允许用户选择颜色。QColorDialog提供了一种简单的方式来获取用户的颜色选择,通常包括一个调色板界面,用户可以通过滑动或点击来选择RGB、HSV或其他色彩模型中的颜色。 横渐变取色可能通过QGradient实现,QGradient允许开发者创建线性或径向的色彩渐变。线性渐变(QLinearGradient)沿直线从一个点到另一个点过渡颜色,而径向渐变(QRadialGradient)则以圆心为中心向外扩散颜色。在调色板中,用户可能可以通过滑动条或鼠标拖动来改变渐变的位置,从而选取不同位置的颜色。 竖渐变取色则可能是通过调整QGradient的方向来实现的,将原本水平的渐变方向改为垂直。这种设计可以提供另一种方式来探索颜色空间,使得选取颜色更为直观和便捷。 在【colorpanelhsb】这个文件名中,我们可以推测这是与HSB(色相、饱和度、亮度)色彩模型相关的代码或资源。HSB模型是另一种常见且直观的颜色表示方式,与RGB或CMYK模型不同,它以人的感知为基础,更容易理解。在这个调色板中,用户可能可以通过调整H、S、B三个参数来选取所需的颜色。 基于QT的调色板是一个利用Qt框架和其提供的色彩管理工具,如QPalette、QColorDialog、QGradient等,构建的交互式颜色选择组件。它不仅提供了横竖渐变的色彩选取方式,还可能支持HSB色彩模型,使得用户在开发图形用户界面时能更加灵活和精准地控制色彩。
标题基于Spring Boot的二手物品交易网站系统研究AI更换标题第1章引言阐述基于Spring Boot开发二手物品交易网站的研究背景、意义、现状及本文方法与创新点。1.1研究背景与意义介绍二手物品交易的市场需求和Spring Boot技术的适用性。1.2国内外研究现状概述当前二手物品交易网站的发展现状和趋势。1.3论文方法与创新点说明本文采用的研究方法和在系统设计中的创新之处。第2章相关理论与技术介绍开发二手物品交易网站所涉及的相关理论和关键技术。2.1Spring Boot框架解释Spring Boot的核心概念和主要特性。2.2数据库技术讨论适用的数据库技术及其在系统中的角色。2.3前端技术阐述与后端配合的前端技术及其在系统中的应用。第3章系统需求分析详细分析二手物品交易网站系统的功能需求和性能需求。3.1功能需求列举系统应实现的主要功能模块。3.2性能需求明确系统应满足的性能指标和安全性要求。第4章系统设计与实现具体描述基于Spring Boot的二手物品交易网站系统的设计和实现过程。4.1系统架构设计给出系统的整体架构设计和各模块间的交互方式。4.2数据库设计详细阐述数据库的结构设计和数据操作流程。4.3界面设计与实现介绍系统的界面设计和用户交互的实现细节。第5章系统测试与优化说明对系统进行测试的方法和性能优化的措施。5.1测试方法与步骤测试环境的搭建、测试数据的准备及测试流程。5.2测试结果分析对测试结果进行详细分析,验证系统是否满足需求。5.3性能优化措施提出针对系统性能瓶颈的优化建议和实施方案。第6章结论与展望总结研究成果,并展望未来可能的研究方向和改进空间。6.1研究结论概括本文基于Spring Boot开发二手物品交易网站的主要发现和成果。6.2展望与改进讨论未来可能的系统改进方向和新的功能拓展。
1. 用户与权限管理模块 角色管理: 学生:查看个人住宿信息、提交报修申请、查看卫生检查结果、请假外出登记 宿管人员:分配宿舍床位、处理报修申请、记录卫生检查结果、登记晚归情况 管理员:维护楼栋与房间信息、管理用户账号、统计住宿数据、发布宿舍通知 用户操作: 登录认证:对接学校统一身份认证(模拟实现,用学号 / 工号作为账号),支持密码重置 信息管理:学生完善个人信息(院系、专业、联系电话),管理员维护所有用户信息 权限控制:不同角色仅可见对应功能(如学生无法修改床位分配信息) 2. 宿舍信息管理模块 楼栋与房间管理: 楼栋信息:名称(如 "1 号宿舍楼")、层数、性别限制(男 / 女 / 混合)、管理员(宿管) 房间信息:房间号(如 "101")、户型(4 人间 / 6 人间)、床位数量、已住人数、可用状态 设施信息:记录房间内设施(如空调、热水器、桌椅)的配置与完好状态 床位管理: 床位编号:为每个床位设置唯一编号(如 "101-1" 表示 101 房间 1 号床) 状态标记:标记床位为 "空闲 / 已分配 / 维修中",支持批量查询空闲床位 历史记录:保存床位的分配变更记录(如从学生 A 调换到学生 B 的时间与原因) 3. 住宿分配与调整模块 住宿分配: 新生分配:管理员导入新生名单后,宿管可按专业集中、性别匹配等规则批量分配床位 手动分配:针对转专业、复学学生,宿管手动指定空闲床位并记录分配时间 分配结果公示:学生登录后可查看自己的宿舍信息(楼栋、房间号、床位号、室友列表) 调整管理: 调宿申请:学生提交调宿原因(如室友矛盾、身体原因),选择意向宿舍(需有空位) 审批流程:宿管审核申请,通过后执行床位调换,更新双方住宿信息 换宿记录:保存调宿历史(申请人、原床位、新床位、审批人、时间) 4. 报修与安全管理模块 报修管理: 报修提交:学生选择宿舍、设施类型(如 "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值