xargs

xargs 命令

江枫思渺然 提交于 2019-11-27 02:30:59
关于 xargs 命令 xargs 是 Unix 系统的一个很有用的命令,但是常常被忽视,很多人不了解它的用法。 本文介绍如何使用这个命令。 一、标准输入与管道命令 Unix 命令都带有参数,有些命令可以接受"标准输入"(stdin)作为参数。 $ cat /etc/passwd | grep root 上面的代码使用了管道命令(|)。管道命令的作用,是将左侧命令(cat /etc/passwd)的标准输出转换为标准输入,提供给右侧命令(grep root)作为参数。 因为 grep 命令可以接受标准输入作为参数,所以上面的代码等同于下面的代码。 $ grep root /etc/passwd 但是,大多数命令都不接受标准输入作为参数,只能直接在命令行输入参数,这导致无法用管道命令传递参数。举例来说,echo 命令就不接受管道传参。 $ echo "hello world" | echo 上面的代码不会有输出。因为管道右侧的 echo 不接受管道传来的标准输入作为参数。 二、xargs 命令的作用 xargs 命令的作用,是将标准输入转为命令行参数。 $ echo "hello world" | xargs echo hello world 上面的代码将管道左侧的标准输入,转为命令行参数 hello world,传给第二个 echo 命令。 xargs 命令的格式如下: $

Is there a grep equivalent for find's -print0 and xargs's -0 switches?

瘦欲@ 提交于 2019-11-27 00:51:33
问题 I often want to write commands like this (in zsh , if it's relevant): find <somebasedirectory> | \ grep stringinfilenamesIwant | \ grep -v stringinfilesnamesIdont | \ xargs dosomecommand (or more complex combinations of greps) In recent years find has added the -print0 switch, and xargs has added -0 , which allow handling of files with spaces in the name in an elegant way by null-terminating filenames instead, allowing for this: find <somebasedirectory> -print0 | xargs -0 dosomecommand

find用法详解

痴心易碎 提交于 2019-11-27 00:00:19
Linux下find命令在目录结构中搜索文件,并执行指定的操作。Linux下find命令提供了相当多的查找条件,功能很强大 find常见命令参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <strong>命令选项:< /strong > -name 按照文件名查找文件。 -perm 按照文件权限来查找文件。 -user 按照文件属主来查找文件。 -group 按照文件所属的组来查找文件。 -mtime -n +n 按照文件的更改时间来查找文件 【-7 7天之内 +7 7天前】 -nogroup 查找无效属组的文件,即该文件所属的组在 /etc/groups 中不存在。 -nouser 查找无效属主的文件,即该文件的属主在 /etc/passwd 中不存在。 -newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。 - type 查找某一类型的文件,诸如: b - 块设备文件。 d - 目录。 c - 字符设备文件。 p - 管道文件。 l - 符号链接文件。 f - 普通文件。 -size n:[c] 查找文件长度为n块的文件,带有c表示文件长度以字节计。 -depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。

What's the equivalent of xargs in PowerShell?

南笙酒味 提交于 2019-11-26 22:53:14
问题 The POSIX-defined xargs command takes all of the items it receives from standard input and passes them as command-line arguments to the command it receives on it's own command line. E.g: grep -rn "String" | xargs rm . What's the equivalent in PowerShell? The following questions all ask this: Convert xargs Bash command to PowerShell? What is the PowerShell equivalent to this Bash command? but there is no correct answer because all the answers either use ForEach-Object , which will process

Running programs in parallel using xargs

北城余情 提交于 2019-11-26 21:39:21
I currently have the current script. #!/bin/bash # script.sh for i in {0..99}; do script-to-run.sh input/ output/ $i done I wish to run it in parallel using xargs. I have tried script.sh | xargs -P8 But doing the above only executed once at the time. No luck with -n8 as well. Adding & at the end of the line to be executed in the script for loop would try to run the script 99 times at once. How do I execute the loop only 8 at the time, up to 100 total. From the xargs man page: This manual page documents the GNU version of xargs. xargs reads items from the standard input, delimited by blanks

GNU parallel not working at all

妖精的绣舞 提交于 2019-11-26 20:19:19
I have been trying to use GNU parallel for some time, but I have never been able to get it to function at all! For example, running (in a non-empty directory!): ls | parallel echo # Outputs single new line ls | parallel echo echo echo # Outputs three new lines. ls | parallel echo {} # /bin/bash: {}: command not found ls | parallel echo '{}' # /bin/bash: {}: command not found ls | parallel 'echo {}' # Outputs: {} ls | parallel -IMM 'echo MM' # Outputs: MM It seems that it is simply executing each argument as a command, which makes no sense. I have tried bash, zsh, tcsh, csh, and sh, to no avail

find 文件查找

孤街醉人 提交于 2019-11-26 19:33:05
在文件系统上查找符合条件的文件 locate :依赖于事先构建的索引,索引的构建是在系统较为空闲时自动进行(周期性任务),手动更新数据库(updatedb) 非实时查找 模糊查找 索引构建过程需要遍历整个根文件系统,极消耗资源 find详解 实时查找工具,通过遍历指定起始路径下文件系统层级结构完成文件查找 ;可以指定一些匹配条件,如按文件名、文件类型、用户甚至是时间戳查找文件。具有以下几个特点: 查找速度略慢 精确查找 实时查找 可能只搜索用户具备读取和执行权限的目录 find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression] find [OPTION]... [查找路径] [查找条件] [处理动作] # 查找路径:指定具体目标路径;默认为当前目录 # 查找条件:指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件 # 处理动作:对符合条件的文件做操作,默认输出至屏幕 比如: find /var -user root -a -group mail -ls #表示查找/var目录下属主为root同时属组为mail的文件,并ll出来 1.常用选项 1.1 -name,-iname 根据文件名查找   -name "文件名称" 文件名称严格区分大小写  -iname "文件名称"

Calling multiple commands through xargs

你说的曾经没有我的故事 提交于 2019-11-26 18:04:11
cat a.txt | xargs -I % echo % In the example above, xargs takes echo % as the command argument. But in some cases, I need multiple commands to process the argument instead of one. For example: cat a.txt | xargs -I % {command1; command2; ... } But xargs doesn't accept this form. One solution I know is that I can define a function to wrap the commands, but it's not a pipeline, I don't prefer it. Is there another solution? cat a.txt | xargs -I % sh -c 'command1; command2; ...' Note that this is a Useless Use Of cat . I'd write it as: < a.txt xargs -I % sh -c 'command1; command2; ...' (Yes, the

Why does “locate filename | xargs vim” cause strange terminal behaviour?

谁说胖子不能爱 提交于 2019-11-26 17:10:19
问题 When I do "locate 50local.policy | xargs vim", I get the error "Vim: Warnung: Die Eingabe kommt nicht von einem Terminal" (translation: Vim: Warning: The input does not come from a terminal). I can edit successfully with vim but after I close it my terminal behaves strangely (I can't type letters and when I hit enter the shell prompt simply gets repeated. When I do it with "xargs gedit" it does not create those problems. I use Ubuntu 11.10 with Gnome 3 and Gnome-Terminal 3.0.1. 回答1: Vim

Running programs in parallel using xargs

若如初见. 提交于 2019-11-26 06:18:46
问题 I currently have the current script. #!/bin/bash # script.sh for i in {0..99}; do script-to-run.sh input/ output/ $i done I wish to run it in parallel using xargs. I have tried script.sh | xargs -P8 But doing the above only executed once at the time. No luck with -n8 as well. Adding & at the end of the line to be executed in the script for loop would try to run the script 99 times at once. How do I execute the loop only 8 at the time, up to 100 total. 回答1: From the xargs man page: This manual