xargs

如何使用find和xargs查找和处理文件

◇◆丶佛笑我妖孽 提交于 2019-12-04 19:56:27
find 是日常工具箱中功能强大、灵活的 命令 行程序之一。它如它名字所暗示的:查找符合你指定条件的文件和目录。借助 -exec 或 -delete 之类的参数,你可以让它对找到的文件进行操作。 在 命令 行提示系列的这一期中,你将会看到 find 命令的介绍,并学习如何使用内置命令或使用 xargs 命令处理文件。 查找文件 find 至少要加上查找的路径。例如,此命令将查找(并打印)系统上的每个文件: find / 由于一切皆文件,因此你会看到大量的输出。这可能无法帮助你找到所需的内容。你可以更改路径参数缩小范围,但这实际上并没有比使用 ls 命令更好。因此,你需要考虑要查找的内容。 也许你想在家目录中查找所有 JPEG 文件。 -name 参数允许你将结果限制为与给定模式匹配的文件。 find ~ -name '*jpg' 但是等等!如果其中一些扩展名是大写怎么办? -iname 类似于 -name,但不区分大小写: find ~ -iname '*jpg' 很好!但是 8.3 命名方案出自 1985 年。某些图片的扩展名可能是 .jpeg。幸运的是,我们可以将模式使用“或”(-o)进行组合。括号需要转义,以便使 find 命令而不是 shell 程序尝试解释它们。 find ~ \( -iname 'jpeg' -o -iname 'jpg' \) 更进一步。如果你有一些以

Ignore empty results for xargs in Mac OS X

我是研究僧i 提交于 2019-12-04 17:56:03
问题 The code for my website uses this piece of code for automatic deployment on the server (Ubuntu). cmd = 'cd ' + checkout_dir + ' && ' + svn_command + " st | awk '{print $2}' | grep -v ^deploy | tac | xargs -r" + svn_command + " revert -R && " + svn_command + ' up -r ' + options.revision What this command does is it cd into checkout directory, runs svn status , and then extracts the filename ( $2 ), removes the deploy directory and all its files from the list (I don't want to revert it). If

Run a specifiable number of commands in parallel - contrasting xargs -P, GNU parallel, and “moreutils” parallel

邮差的信 提交于 2019-12-04 15:05:10
I'm trying to run multiple mongodump's on 26 servers in a bash script. I can run 3 commands like mongodump -h staging .... & mongodump -h production .... & mongodump -h web ... & at the same time, and when one finishes I want to start another mongodump. I can't run all 26 mongodumps commands at the same time, the server will run out on CPU. Max 3 mongodumps at the same time. mklement0 You can use xarg 's -P option to run a specifiable number of invocations in parallel : Note that the -P option is not mandated by POSIX , but both GNU xargs and BSD/macOS xargs support it. xargs -P 3 -n 1

xargs command length limits

馋奶兔 提交于 2019-12-04 11:48:34
I am using jsonlint to lint a bunch of files in a directory (recursively). I wrote the following command: find ./config/pages -name '*.json' -print0 | xargs -0I % sh -c 'echo Linting: %; jsonlint -V ./config/schema.json -q %;' It works for most files but some files I get the following error: Linting: ./LONG_FILE_NAME.json fs.js:500 return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode); ^ Error: ENOENT, no such file or directory '%' It appears to fail for long filenames. Is there a way to fix this? Thanks. Edit 1: Found the problem. -I replstr Execute utility for each

linux shell脚本语法笔记

你离开我真会死。 提交于 2019-12-04 11:19:26
1.&,&&,|,|| & :除了最后一个cmd,前面的cmd均已后台方式静默执行,执行结果显示在终端上,个别的cmd错误不影响整个命令的执行,全部的cmd同时执行 && :从左到右顺序执行cmd,个别cmd错误不产生影响 | :各个cmd同时在前台被执行,但是除最后的cmd之外,其余的执行结果不会被显示在终端上 || :从左到右顺序执行cmd,只有左侧的cmd执行出错,右边的cmd才会被执行,同时一旦有cmd被成功执行,整个命令就会结束,返回终端 2.xargs xargs 能够捕获一个命令的输出,然后传递给另外一个命令,例如: 2.1. rm 删除多文件 find ~ -name ‘*.log’ -print0 | xargs -0 rm -f 2.2. 获得/etc/ 下所有*.conf 结尾的文件列表 # find /etc -name "*.conf" | xargs ls –l 2.3. 使用xargs 下载所有链接 # cat url-list.txt | xargs wget –c 2.4. 查找所有的.txt文件,并且压缩它 # find / -name *.txt -type f -print | xargs tar -cvzf txtfile.tar.gz 3 5. 拷贝所有的图片文件到一个外部的硬盘驱动 # ls *.jpg | xargs -n1 -i

通过xargs实现redis cluster批量keys操作

纵饮孤独 提交于 2019-12-04 09:41:47
通过xargs实现redis cluster批量keys操作 为什么会出现批量删除keys的脚本,那是因为redis不支持删除(DEL)带有通配符“ * ”的key,而查询支持带通配符“ * ”。 [root@localhost ~]# redis-cli -c -h 10.1.37.113 -p 7002 -a PASSWORD KEYS hello* 1) "hello8" 2) "hello5" [root@localhost ~]# redis-cli -c -h 10.1.37.113 -p 7002 -a PASSWORD DEL hello* (integer) 0 工具简介 xargs This manual page documents the GNU version of xargs. xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments

docker批量操作命令汇总

别说谁变了你拦得住时间么 提交于 2019-12-04 09:09:32
## 背景 > 在开发过程中经常会遇到批量创建容器,拉取镜像等一系列的操作,如果手动一个一个的操作,那将浪费很多时间,而且毫无技术含量,因此要提高工作效率,必须让这些动作能够实现自动化,最不济也要实现半自动化,下面介绍一些我最常用的一些脚本工具。 ##场景分析 - 批量停止并删除容器(filter为需要处理的容器共同标识符) >通常我们在测试某个服务时,可能会涉及多个工具,比如web服务,我们需要启动tomcat服务器、数据库,nginx等(存在一种情况,在启动容器时需要给每个容器设定一个名字,设定名字有一个好处,我们可以把一组相互协作的容器设定为相同的前缀,一方面方便管理,另一方面,易于实现自动化),如果web服务启动失败了或者需要停止容器,则需要重启,重启时就会发生容器名字冲突,一种较好的方式就是清掉启动失败的容器,再重启。 停止容器(适用于具有相同的容器名前缀或者各个镜像的名字类似) ``` docker ps | grep filter | awk '{print $1}' | xargs docker stop #该条命令分为四部分,docker ps, grep filter, awk '{print $1}', xargs docker stop docker ps 用于列出所有正常运行的容器 |grep filter 将上一命令的结果通过管道传给过滤器

Linux rm删除文件过多不执行

孤者浪人 提交于 2019-12-04 08:40:18
刚网上搜索了一个小技巧 ,有时候在删除文件的时候 ,文件很多 ,直接用 rm -rf * ,会报错误 “rm 参数列表过长 ”。 这时候网上的办法一般都是通过类似的办法 :find . -name "" | xargs rm -rf ,这样确实可以删 ,但是很慢。我们遇到这个一个目录下有将近 40万个文件 , 就尝试了一下这个方法, 先执行find . > file , 将file文件中的第一行.删除掉 再执行cat file |xargs rm -f , 两分钟删除完成。 这时候网上的办法一般都是通过类似的办法 :find . -name "" | xargs rm -rf ,这样确实可以删 ,但是很慢。我们遇到这个一个目录下有将近 40万个文件 , 就尝试了一下这个方法, 先执行find . > file , 将file文件中的第一行.删除掉 再执行cat file |xargs rm -f , 两分钟删除完成。 来源: https://www.cnblogs.com/imbasaber/p/11849994.html

xargs and find, rm complaining about \\n (newline) in filename

家住魔仙堡 提交于 2019-12-04 08:27:01
I am trying to delete the oldest file in a tree with a script in Debian. find /home/backups -type f \( -name \*.tgz -o -name \*.gz \) -print0 | xargs -0 ls -t | tail -1 | xargs -0 rm But I am getting an error: rm: cannot remove `/home/backups/tree/structure/file.2011-12-08_03-01-01.sql.gz\n': No such file or directory Any ideas what I am doing wrong (or is there an easier/better way?), I have tried to RTFM, but am lost. Jens The ls appends a newline and the last xargs -0 says the newline is part of the file name. Run the last xargs with -d '\n' instead of -0 . BTW, due to the way xargs works,

how to export ssl key , crt and CA from httpd.conf apache to use it into nginx for all users

人走茶凉 提交于 2019-12-04 07:23:46
问题 use custom setup that use nginx as web engine with cpanel need command to export ssl files to use it into nginx cpanel now use AutoSSL powered by Comodo that give it free and will renew it automatic when any users domains ssl expire example httpd.conf <VirtualHost 4xx30:4433> ServerName xnxxsch.com <IfModule ssl_module> SSLCertificateFile /var/cpanel/ssl/installed/certs/xnh_com_d98c5_67ca3_150707$ SSLCertificateKeyFile /var/cpanel/ssl/installed/keys/d98c5_67ca3_76c14a301e0260891bbe91504$