xargs

xargs with multiple arguments

梦想与她 提交于 2019-11-27 11:27:51
问题 I have a source input, input.txt a.txt b.txt c.txt I want to feed these input into a program as the following: my-program --file=a.txt --file=b.txt --file=c.txt So I try to use xargs , but with no luck. cat input.txt | xargs -i echo "my-program --file"{} It gives my-program --file=a.txt my-program --file=b.txt my-program --file=c.txt But I want my-program --file=a.txt --file=b.txt --file=c.txt Any idea? 回答1: None of the solutions given so far deals correctly with file names containing space.

[linux] 大批量删除任务

让人想犯罪 __ 提交于 2019-11-27 11:12:06
一不小心投了巨多任务,或者投递的资源不合理时,想批量杀掉这些任务。 kill的方法就不说了,我这里用qdel的方法。 用了这么一条命令: qstat |sed '1,2d' |awk -F' ' '{print $1}' |sed ':x;N;s/\n/ /;b x'|cat 再用 qdel 删除即可。这里还是用了两步,而且用 sed 将换行符替换为空格很复杂的样子,不友好。 其实可以直接用 xargs 简化: qstat |sed '1,2d' |awk -F' ' '{print $1}' | xargs qdel 也可根据用户或者运行状态有选择性地删除,如只删等待状态的任务而不删除运行中的任务。 qstat -u USERNAME | grep "qw" | cut -d" " -f1 | xargs qdel 同样,删除运行中的任务: qstat -u USERNAME | grep "r" | cut -d" " -f1 | xargs qdel cut 和 awk 均可 来源: https://www.cnblogs.com/jessepeng/p/11361239.html

How can I use xargs to copy files that have spaces and quotes in their names?

谁都会走 提交于 2019-11-27 09:59:59
I'm trying to copy a bunch of files below a directory and a number of the files have spaces and single-quotes in their names. When I try to string together find and grep with xargs , I get the following error: find .|grep "FooBar"|xargs -I{} cp "{}" ~/foo/bar xargs: unterminated quote Any suggestions for a more robust usage of xargs? This is on Mac OS X 10.5.3 (Leopard) with BSD xargs . You can combine all of that into a single find command: find . -iname "*foobar*" -exec cp -- "{}" ~/foo/bar \; This will handle filenames and directories with spaces in them. You can use -name to get case

shell笔记

自作多情 提交于 2019-11-27 09:57:55
1. sed -i '/virtual.*= 0;/s/= 0;/{printf("file[%s], function[%s], line[%d]\n", __FILE__, __FUNCTION__, __LINE__);}/g' Include/database/otlv4.h 2. cygwin安装完,没有passwd的文件,如果在cygwin安装zsh,无法修改默认为zsh,所以 mkpasswd -l > /etc/passwd mkpasswd是生成对应的文件信息,然后导入到passwd,修改passwd对应的用户shell就可以 3. 查看服务器详细信息 lsb_release -a 4. 匹配F:\,不要用双引号 grep -nr 'F:\\' 5. xargs可以把前面的结果传递到后面,就不用``,更好用 find -name 'file' | xargs grep -rl 'keyword' | xargs sed -i 's/keyword/replaceword/g' find -name 'file' | xargs grep -rl 'keyword' | xargs sed -i '/\"keyword\"/,+2d' 来源: https://www.cnblogs.com/studywithallofyou/p/11357423.html

How to use > in an xargs command?

匆匆过客 提交于 2019-11-27 08:56:43
问题 I want to find a bash command that will let me grep every file in a directory and write the output of that grep to a separate file. My guess would have been to do something like this ls -1 | xargs -I{} "grep ABC '{}' > '{}'.out" but, as far as I know, xargs doesn't like the double-quotes. If I remove the double-quotes, however, then the command redirects the output of the entire command to a single file called '{}'.out instead of to a series of individual files. Does anyone know of a way to

Make xargs execute the command once for each line of input

那年仲夏 提交于 2019-11-27 05:48:29
How can I make xargs execute the command exactly once for each line of input given? It's default behavior is to chunk the lines and execute the command once, passing multiple lines to each instance. From http://en.wikipedia.org/wiki/Xargs : find /path -type f -print0 | xargs -0 rm In this example, find feeds the input of xargs with a long list of file names. xargs then splits this list into sublists and calls rm once for every sublist. This is more efficient than this functionally equivalent version: find /path -type f -exec rm '{}' \; I know that find has the "exec" flag. I am just quoting an

xargs的用法

旧街凉风 提交于 2019-11-27 04:54:31
处理带有空格的 文件名 #我们创建了3个日志文件, 且故意让文件名称中都含有空格 [roc@roclinux ~]$ for((i=0;i<3;i++)); do touch "test ${i}.log";done #我们列出创建的文件 [roc@roclinux ~]$ ls -1F test 0.log test 1.log test 2.log xargs 提供了 -0 选项,允许将 NULL 作为分隔符,而 find 命令也心有灵犀地提供了对应的选项来产生以 NULL 字符作为分隔符的输出。 find 命令提供的对应方法是 -print0 选项,在文件名之后输出 NULL,而不像 -print 选项那样输出换行符(换行符会被 xargs 替换成空格)。 [roc@roclinux ~]$ find . -name '*.log' -print0 | xargs -0 rm -f 需要用户确认 如果在前一个命令的标准输出中,会有一些参数是你不希望或者不确定是否要传送给后面命令的,这个时候我们就希望 xargs 在传送参数前和我们确认一下。而 -p 选项恰好可以实现这个愿望,我们可以输入 y 或者 n 来选择是否要执行当前命令: [roc@roclinux ~]$ find . -type f |xargs -p rm -f rm -f ./china.txt ./usa

linux探路-shell scripts

梦想与她 提交于 2019-11-27 03:38:33
一直相对shell脚本进行系统性学习,由于时间的原因,每次都是看一点,过一段时间又忘了,这次狠下心做一次shell script的学习和分享,和大家一起掌握好shell,下面先大体描述下技术层面的shell,后面主要就是练习。 首先什么是shell script呢? 它是利用shell的功能所写出来的一个程序,这个程序是使用纯文本文件,将一些shell提供的命令写在里面,再搭配正则表达式、管线、数据流导向等功能,以达到我们想要处理的目的。 那script的执行方式有哪些,它们之间又有什么区别?一共三种分别是./script、sh script以及source script。 在执行./script、sh script时,该脚本都会使用一个子程序(新的bash)环境来执行脚本内的命令, 当子程序完成后,在子程序内的各种变量或动作将会结束而不会传回父程序中 ,即你在子程序中声明的变量,在父程序中是使用不了的。 而在执行source script时,脚 本是在父进程中执行的,所有脚本执行完,其中的变量还是能继续使用。 下面主要就是实战: 一、善用判断: 1、test: 2、利用判断符号[]: [ "${name}" == "name" -o "${name}" == "Name" ] 数字的比较: -eq 相等(equal) -ne 不等(not equal) -gt 大于

Is it possible to distribute STDIN over parallel processes?

我只是一个虾纸丫 提交于 2019-11-27 03:28:02
问题 Given the following example input on STDIN: foo bar bar baz === qux bla === def zzz yyy Is it possible to split it on the delimiter (in this case '===') and feed it over stdin to a command running in parallel? So the example input above would result in 3 parallel processes (for example a command called do.sh) where each instance received a part of the data on STDIN, like this: do.sh (instance 1) receives this over STDIN: foo bar bar baz do.sh (instance 2) receives this over STDIN: qux bla do

How to ignore xargs commands if stdin input is empty?

折月煮酒 提交于 2019-11-27 03:12:45
Consider this command: ls /mydir/*.txt | xargs chown root The intention is to change owners of all text files in mydir to root The issue is that if there are no .txt files in mydir then xargs thows an error saying there is no path specified. This is a harmless example because an error is being thrown, but in some cases, like in the script that i need to use here, a blank path is assumed to be the current directory. So if I run that command from /home/tom/ then if there is no result for ls /mydir/*.txt and all files under /home/tom/ have their owners changed to root. So how can I have xargs