list bash: /bin/ls: 参数列表太长

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7030124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:42:14  来源:igfitidea点击:

bash: /bin/ls: Argument list too long

listlsfastq

提问by LookIntoEast

I need to make a list of a large number of files (40,000 files) like below:

我需要列出大量文件(40,000 个文件),如下所示:

ERR001268_1_100.fastq  ERR001268_2_156.fastq  ERR001753_2_78.fastq
ERR001268_1_101.fastq  ERR001268_2_157.fastq  ERR001753_2_79.fastq
ERR001268_1_102.fastq  ERR001268_2_158.fastq  ERR001753_2_7.fastq
ERR001268_1_103.fastq  ERR001268_2_159.fastq  ERR001753_2_80.fastq

my command is: ls ERR*_1_*.fastq |sed 's/\.fastq//g'|sort -n > masterlistHowever error is: bash: /bin/ls: Argument list too long

我的命令是:ls ERR*_1_*.fastq |sed 's/\.fastq//g'|sort -n > masterlist但是错误是:bash: /bin/ls: Argument list too long

However can I solve this problem? Any other way to make list like this by perl/python?

但是我能解决这个问题吗?任何其他方式通过 perl/python 制作这样的列表?

thx

谢谢

回答by Jim Lewis

You should be able to replace ls ERR*_1_*.fastqwith find . -name "ERR*_1_*.fastq".
This way, you can avoid having the wildcard expand into a huge argument list.

您应该可以替换ls ERR*_1_*.fastqfind . -name "ERR*_1_*.fastq".
这样,您可以避免将通配符扩展为一个巨大的参数列表。

(The findoutput will include a leading "./", e.g. ./ERR001268_1_100.fastq. If that's undesirable, you can get rid of it with another sedcommand later in the pipeline.)

find输出将包括一个前导“./”,例如./ERR001268_1_100.fastq。如果这是不受欢迎的,您可以sed在管道中稍后使用另一个命令来摆脱它。)

回答by Ben G.

If the files already all exist within your directory, python's "glob" module might have a higher limit than bash's command line.

如果文件已经全部存在于您的目录中,python 的“glob”模块可能比 bash 的命令行具有更高的限制。

From the command line:

从命令行:

python -c "import glob; print glob.glob('ERR_*_1_*.fastq')"

To do the whole thing in python, the you could try something like this:

要在 python 中完成整个事情,你可以尝试这样的事情:

import glob
files = glob.glob("ERR_*_1_*.fastq")
trimmedfiles = [x.replace(".fastq","") for x in files]
trimmedfiles.sort()
for f in trimmedfiles:
    print f

This solution will sort the files alphabetically, and not numerically. For that you might want to add some key=lambda magic to the sort() method:

此解决方案将按字母顺序而不是数字顺序对文件进行排序。为此,您可能希望向 sort() 方法添加一些 key=lambda 魔法:

trimmedfiles.sort(key=lambda f: int(f.split("_")[2]))

回答by j03m

Find might help you - rather then ls use find . -name 'yourpatternhere' -print0 | xargs -0 youractionhere

Find 可能会帮助您 - 而不是 ls 使用 find . -name 'yourpatternhere' -print0 | xargs -0 youractionhere

回答by logan lu

You can use find.

您可以使用find.

Example:

例子:

find /Users/kunlun/Downloads/fu_neg/ -name "*.png" > 
/Users/kunlun/Downloads/fu_neg.txt