xargs

xargs命令

浪尽此生 提交于 2019-11-28 03:28:59
xargs命令从stdin处读取一系列参数,然后使用这些参数来执行指定命令。它能将单行或多行输入文本转换成其他格式,例如单行变多行或是多行变单行。 将多行输入转换成单行输出 xargs默认的echo命令可以用来将多行输入转换成单行输出。 example.txt 1 2 3 4 5 6 7 8 9 10 11 12 执行:cat example.txt | xargs 输出:1 2 3 4 5 6 7 8 9 10 11 12 将单行输入转换成多行输出 xargs的-n选项可以限制每次调用命令时用到的参数个数。 未完待续...... 来源: https://www.cnblogs.com/lfjn/p/11389231.html

How to delete many 0 byte files in linux?

≡放荡痞女 提交于 2019-11-28 03:15:17
I've a directory with many number of 0 byte files in it. I can't even see the files when I use the ls command. I'm using a small script to delete these files but sometimes that does not even delete these files. Here is the script: i=100 while [ $i -le 999 ];do rm -f file${i}*; let i++; done Is there any other way to do this more quickly? Use find combined with xargs . find . -name 'file*' -size 0 -print0 | xargs -0 rm You avoid to start rm for every file. With GNU's find (see comments), there is no need to use xargs : find -name 'file*' -size 0 -delete You can use the following command: find .

统计一个目录下所有普通文件的总行数

旧时模样 提交于 2019-11-27 23:45:17
1. 统计一个目录下所有普通文件的总行数 #实用场景:毕业设计时,需要统计自己的代码行数#    方案一:find . -type f -name "*.c" -exec cat {} \; | grep -v '^$' | wc -l     解释:find . -type f -name "*.c" 查找当前目录下所有以.c结尾的文件,如果不考虑文件类型,可以直接实用find . -type f即可。 -exec cat {} \; 是将找到的文件采用cat命令输出; grep -v '^$' 是将不是空行的内容进行统计,如果是空行就不统计,它相当于一个过滤器。 wc -l 统计输出来的代码的行数。如果不需要 对空行进行过滤的话,可以省去grep -v '^$'   方案二:find . -type f -exec wc -l {} \; | awk '{sum+=$1}END{print sum}'     解释:find命令同方案一。-exec wc -l 是指将找到的文件依次计算其中的行数,此时如果输出的话,会输出类似于 22 main.c 43 head1.h 67 head1.c 所以还要采用awk将第一列进行相加,由sum+=$1,很明显,比较容易理解awk的含义。但此种方法没有对空行进行过滤。   方案三:find . -type f | xargs wc -l

linux基础命令:xargs

牧云@^-^@ 提交于 2019-11-27 21:30:30
linux下xargs命令详解 xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理。通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从文件的输出中读取数据。xargs的默认命令是echo,这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代。 例子一 1. 当你尝试用rm 删除太多的文件,你可能得到一个错误信息:/bin/rm Argument list too long. 用xargs 去避免这个问题 find ~ -name ‘*.log’ -print0 | xargs -0 rm -f 2. 获得/etc/ 下所有*.conf 结尾的文件列表,有几种不同的方法能得到相同的结果,下面的例子仅仅是示范怎么实用xargs ,在这个例子中实用 xargs将find 命令的输出传递给ls -l # find /etc -name "*.conf" | xargs ls –l 3. 假如你有一个文件包含了很多你希望下载的URL, 你能够使用xargs 下载所有链接 # cat url-list.txt | xargs wget –c 4. 查找所有的jpg 文件,并且压缩它 # find / -name *.jpg -type f

xargs doesn't recognize bash aliases

こ雲淡風輕ζ 提交于 2019-11-27 20:10:03
问题 I'm trying to run the following command: find . -iname '.#*' -print0 | xargs -0 -L 1 foobar where "foobar" is an alias or function defined in my .bashrc file (in my case, it's a function that takes one parameter). Apparently xargs doesn't recognize these as things it can run. Is there a clever way to remedy this? 回答1: Since only your interactive shell knows about aliases, why not just run the alias without forking out through xargs ? find . -iname '.#*' -print0 | while read -r -d '' i; do

What's the equivalent of xargs in PowerShell?

自古美人都是妖i 提交于 2019-11-27 20:05:50
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 items one-at-a-time (like xargs -n1 ) which gets the desired result for the examples given, or store the

Make xargs handle filenames that contain spaces

女生的网名这么多〃 提交于 2019-11-27 16:44:26
$ ls *mp3 | xargs mplayer Playing Lemon. File not found: 'Lemon' Playing Tree.mp3. File not found: 'Tree.mp3' Exiting... (End of file) My command fails because the file "Lemon Tree.mp3" contains spaces and so xargs thinks it's two files. Can I make find + xargs work with filenames like this? Ray The xargs command takes white space characters (tabs, spaces, new lines) as delimiters. You can narrow it down only for the new line characters ('\n') with -d option like this: ls *.mp3 | xargs -d '\n' mplayer It works only with GNU xargs. For BSD systems, use the -0 option like this: ls *.mp3 | xargs

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

梦想与她 提交于 2019-11-27 15:28:01
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. sehe Vim expects to be connected to a real terminal and sends codes appropriate to that. Reset the terminal with

How can I pass all arguments with xargs in middle of command in linux

落花浮王杯 提交于 2019-11-27 14:45:07
问题 I want to pass all the files as a single argument on Linux but I am not able to do that. This is working ls | sort -n | xargs -i pdftk {} cat output combinewd2.pdf This passes a single argument per command, but I want all in one command. 回答1: This is one way to do it pdftk $(ls | sort -n) cat output combinewd2.pdf or using backtick pdftk `ls | sort -n` cat output combinewd2.pdf As pointed out in the comments this will not work on filenames containing spaces. In that case you could use eval

xargs,rename,awk,cut,seq,bc,cal

天大地大妈咪最大 提交于 2019-11-27 13:40:10
文章目录 xargs命令 rename 命令 awk命令 cut 命令 seq命令 bc 命令 cal命令 last,lastlog命令 xargs命令 [root@oldboyedu oldboy]# cat file a b c d e f g h i j k l m n o p q r s t u v w x y z [root@oldboyedu oldboy]# cat file | xargs a b c d e f g h i j k l m n o p q r s t u v w x y z xargs的默认命令是echo,空格是默认定界符。这意味着通过管道传递给xargs的输入会包含换行和空白。不过通过xargs的处理,换行和空白将被 空格取代 -n -n选项,指定 输出时每行输出的列数 -d选项,自定义列分割符 [root@oldboyedu oldboy]# echo “a/b/c#d/e/f” | xargs -d/ a b c#d e f -o -o选项相当于就是用来告诉xargs文件的结束标志已经改成NULL [root@oldboyedu oldboy]# find . -name “hello*” -print0 | xargs -0 rm rename 命令 [root@oldboyedu oldboy]# rename txt py 9.txt