shell xargs

2023-12-16 01:58
文章标签 shell xargs

本文主要是介绍shell xargs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

xargs命令是在管道操作符之后,并通过提供命令行参数执行其他命令。
 
1、多行变成单行
-bash-3.2# cat test.txt 
a b c d e f
g o p q
-bash-3.2# cat test.txt |xargs
a b c d e f g o p q
 
 
2、单行变成多行
-bash-3.2# cat test.txt 
a b c d e f g o p q
-bash-3.2# cat test.txt |xargs -n 2
a b
c d
e f
g o
p q
 
 
3、删除某个重复的字符来做定界符
-bash-3.2# cat test.txt 
aaaagttttgyyyygcccc
-bash-3.2# cat test.txt |xargs -d g
aaaa tttt yyyy cccc
 
 
4、删除某个重复的字符来做定界符后,变成多行
-bash-3.2# cat test.txt |xargs -d g -n 2
aaaa tttt
yyyy cccc
 
 
5、用find找出文件以txt后缀,并使用xargs将这些文件删除
-bash-3.2# find /root/ -name "*.txt" -print        #查找
/root/2.txt
/root/1.txt
/root/3.txt
/root/4.txt
-bash-3.2# find /root/ -name "*.txt" -print0 |xargs -0 rm -rf   #查找并删除
-bash-3.2# find /root/ -name "*.txt" -print          #再次查找没有
 
 
6、查找普通文件中包括thxy这个单词的
-bash-3.2# find /root/ -type f -print |xargs grep "thxy"
/root/1.doc:thxy
 
 
7、查找权限为644的文件,并使用xargs给所有加上x权限
-bash-3.2# find /root/ -perm 644 -print
/root/1.c
/root/5.c
/root/2.doc
/root/3.doc
/root/1.doc
/root/2.c
/root/4.doc
/root/4.c
/root/3.c
-bash-3.2# find /root/ -perm 644 -print|xargs chmod a+x
-bash-3.2# find /root/ -perm 755 -print
/root/1.c
/root/5.c
/root/2.doc
/root/3.doc
/root/1.doc
/root/2.c
/root/4.doc
/root/4.c
/root/3.c

这篇关于shell xargs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/498693

相关文章

shell脚本自动删除30天以前的文件(最新推荐)

《shell脚本自动删除30天以前的文件(最新推荐)》该文章介绍了如何使用Shell脚本自动删除指定目录下30天以前的文件,并通过crontab设置定时任务,此外,还提供了如何使用Shell脚本删除E... 目录shell脚本自动删除30天以前的文件linux按照日期定时删除elasticsearch索引s

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s

shell脚本快速检查192.168.1网段ip是否在用的方法

《shell脚本快速检查192.168.1网段ip是否在用的方法》该Shell脚本通过并发ping命令检查192.168.1网段中哪些IP地址正在使用,脚本定义了网络段、超时时间和并行扫描数量,并使用... 目录脚本:检查 192.168.1 网段 IP 是否在用脚本说明使用方法示例输出优化建议总结检查 1

jenkins 插件执行shell命令时,提示“Command not found”处理方法

首先提示找不到“Command not found,可能我们第一反应是查看目标机器是否已支持该命令,不过如果相信能找到这里来的朋友估计遇到的跟我一样,其实目标机器是没有问题的通过一些远程工具执行shell命令是可以执行。奇怪的就是通过jenkinsSSH插件无法执行,经一番折腾各种搜索发现是jenkins没有加载/etc/profile导致。 【解决办法】: 需要在jenkins调用shell脚

站长常用Shell脚本整理分享(全)

站长常用Shell脚本整理分享 站长常用Shell脚本整理分享1-10 站长常用Shell脚本整理分享11-20 站长常用Shell脚本整理分享21-30 站长常用Shell脚本整理分享31-40 站长常用Shell脚本整理分享41-50 站长常用Shell脚本整理分享51-59 长期更新

Shell脚本实现自动登录服务器

1.登录脚本 login_server.sh #!/bin/bash# ReferenceLink:https://yq.aliyun.com/articles/516347#show all host infos of serverList.txtif [[ -f ./serverList.txt ]]thenhostNum=`cat ./serverList.txt | wc -l`e

[轻笔记]ubuntu shell脚本切换conda环境

source /home/yourhostname/anaconda3/etc/profile.d/conda.sh # 关键!!!conda activate env_name

[轻笔记] ubuntu Shell脚本实现监视指定进程的运行状态,并能在程序崩溃后重启动该程序

根据网上博客实现,发现只能监测进程离线,然后对其进行重启;然而,脚本无法打印程序正常状态的信息。自己通过不断修改测试,发现问题主要在重启程序的命令上(需要让重启的程序在后台运行,不然会影响监视脚本进程,使其无法正常工作)。具体程序如下: #!/bin/bashwhile [ 1 ] ; dosleep 3if [ $(ps -ef|grep exe_name|grep -v grep|

adb shell 执行后台程序后断开adb后台进程被结束的解决办法

环境:Android 版本 Android8 通常让程序后台执行就是在命令 最后加上 &即可,但是在Android 8上实验发现,程序的确后台了,但是拔掉USB线再连接上发现进程已结束。不确定Android早期版本是否存在此问题。 参考网上一些Linux方法,如加nohup 仍然无效,还是会结束。看来Android adb shell 与 Linux shell 还是有一定区别。 后来在网上

shell脚本中变量中字符串替换的测试 /和//的区别

test_char=abbbcbbbf echo "bf:test_char = " $test_char test_char=${test_char/bbb/ddd} echo "af:test_char = " $test_char 输出: bf:test_char =  abbbcbbbf af:test_char =  adddcbbbf 只匹配第一个