bash 快速ls命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40193/
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-09 17:35:35  来源:igfitidea点击:

Quick ls command

bashunixcommandls

提问by Mark Witczak

I've got to get a directory listing that contains about 2 million files, but when I do an lscommand on it nothing comes back. I've waited 3 hours. I've tried ls | tee directory.txt, but that seems to hang forever.

我必须得到一个包含大约 200 万个文件的目录列表,但是当我对其执行ls命令时,什么也没有返回。我已经等了3个小时。我试过了ls | tee directory.txt,但这似乎永远挂起。

I assume the server is doing a lot of inode sorting. Is there any way to speed up the lscommand to just get a directory listing of filenames? I don't care about size, dates, permission or the like at this time.

我假设服务器正在进行大量的 inode 排序。有什么方法可以加快ls命令以获取文件名的目录列表吗?目前我不在乎大小、日期、许可等。

回答by Paul Tomblin

ls -U

will do the ls without sorting.

将在不排序的情况下执行 ls。

回答by Ryan Ahearn

Try using:

尝试使用:

find . -type f -maxdepth 1

This will only list the files in the directory, leave out the -type fargument if you want to list files and directories.

这只会列出目录中的文件,-type f如果要列出文件和目录,请省略参数。

回答by Kannan Mohan

This question seems to be interesting and I was going through multiple answers that were posted. To understand the efficiency of the answers posted, I have executed them on 2 million files and found the results as below.

这个问题似乎很有趣,我正在浏览发布的多个答案。为了了解所发布答案的效率,我对 200 万个文件执行了它们,结果如下。

$ time tar cvf /dev/null . &> /tmp/file-count

real    37m16.553s
user    0m11.525s
sys     0m41.291s

------------------------------------------------------

$ time echo ./* &> /tmp/file-count

real    0m50.808s
user    0m49.291s
sys     0m1.404s

------------------------------------------------------

$ time ls &> /tmp/file-count

real    0m42.167s
user    0m40.323s
sys     0m1.648s

------------------------------------------------------

$ time find . &> /tmp/file-count

real    0m2.738s
user    0m1.044s
sys     0m1.684s

------------------------------------------------------

$ time ls -U &> /tmp/file-count

real    0m2.494s
user    0m0.848s
sys     0m1.452s


------------------------------------------------------

$ time ls -f &> /tmp/file-count

real    0m2.313s
user    0m0.856s
sys     0m1.448s

------------------------------------------------------

To summarize the results

总结结果

  1. ls -fcommand ran a bit faster than ls -U. Disabling color might have caused this improvement.
  2. findcommand ran third with an average speed of 2.738 seconds.
  3. Running just lstook 42.16 seconds. Here in my system lsis an alias for ls --color=auto
  4. Using shell expansion feature with echo ./*ran for 50.80 seconds.
  5. And the tarbased solution took about 37 miuntes.
  1. ls -f命令运行速度比ls -U. 禁用颜色可能导致了这种改进。
  2. findcommand 以 2.738 秒的平均速度排名第三。
  3. 跑步只用ls了 42.16 秒。在我的系统中,这ls是一个别名ls --color=auto
  4. 使用 shell 扩展功能echo ./*运行 50.80 秒。
  5. tar基于解决方案的时间大约为 37 分钟。

All tests were done seperately when system was in idle condition.

所有测试都是在系统处于空闲状态时单独进行的。

One important thing to note here is that the file lists are not printed in the terminal rather they were redirected to a file and the file count was calculated later with wccommand. Commands ran too slow if the outputs where printed on the screen.

这里要注意的一件重要事情是,文件列表不会打印在终端中,而是被重定向到一个文件,文件计数是稍后用wc命令计算的。如果输出打印在屏幕上,则命令运行速度太慢。

Any ideas why this happens ?

任何想法为什么会发生这种情况?

回答by stu

I have a directory with 4 million files in it and the only way I got ls to spit out files immediately without a lot of churning first was

我有一个包含 400 万个文件的目录,我让 ls 立即吐出文件而无需先进行大量搅拌的唯一方法是

ls -1U

回答by Limalski

Using

使用

ls -1 -f 

is about 10 times faster and it is easy to do (I tested with 1 million files, but my original problem had 6 800 000 000 files)

大约快 10 倍,而且很容易做到(我测试了 100 万个文件,但我原来的问题有 6 800 000 000 个文件)

But in my case I needed to check if some specific directory contains more than 10 000 files. If there were more than 10 000 files, I am not anymore interested that how many files there is. I just quit the program so that it will run faster and wont try to read the rest one-by-one. If there are less than 10 000, I will print the exact amount. Speed of my program is quite similar to ls -1 -f if you specify bigger value for parameter than amount of files.

但就我而言,我需要检查某个特定目录是否包含超过 10 000 个文件。如果有超过 10 000 个文件,我对有多少文件不再感兴趣。我只是退出程序,以便它运行得更快,并且不会尝试一个一个地阅读其余部分。如果少于 10 000,我将打印确切的数量。如果您为参数指定比文件数量更大的值,我的程序的速度与 ls -1 -f 非常相似。

You can use my program find_if_more.pl in current directory by typing:

您可以通过键入以下内容在当前目录中使用我的程序 find_if_more.pl:

find_if_more.pl 999999999

If you are just interested if there are more than n files, script will finish faster than ls -1 -f with very large amount of files.

如果您只是对 n 个以上的文件感兴趣,那么脚本将比 ls -1 -f 完成得更快,并且文件数量非常多。

#!/usr/bin/perl
    use warnings;
    my ($maxcount) = @ARGV;
    my $dir = '.';
    $filecount = 0;
    if (not defined $maxcount) {
      die "Need maxcount\n";
    }
    opendir(DIR, $dir) or die $!;
    while (my $file = readdir(DIR)) {
        $filecount = $filecount + 1;
        last if $filecount> $maxcount
    }
    print $filecount;
    closedir(DIR);
    exit 0;

回答by Eric

You can redirect output and run the ls process in the background.

您可以重定向输出并在后台运行 ls 进程。

ls > myls.txt &

This would allow you to go on about your business while its running. It wouldn't lock up your shell.

这将允许您在运行时继续您的业务。它不会锁住你的外壳。

Not sure about what options are for running ls and getting less data back. You could always run man lsto check.

不确定运行 ls 和获取较少数据的选项是什么。你可以随时跑去man ls检查。

回答by telent

This is probably not a helpful answer, but if you don't have findyou may be able to make do with tar

这可能不是一个有用的答案,但如果你没有,find你可以凑合tar

$ tar cvf /dev/null .

I am told by people older than me that, "back in the day", single-user and recovery environments were a lot more limited than they are nowadays. That's where this trick comes from.

比我年长的人告诉我,“回到过去”,单用户和恢复环境比现在更加有限。这就是这个技巧的由来。

回答by plasmafire

This would be the fastest option AFAIK: ls -1 -f.

这将是最快的选项 AFAIK: ls -1 -f

  • -1(No columns)
  • -f(No sorting)
  • -1(无列)
  • -f(无排序)

回答by Benedikt Waldvogel

If a process "doesn't come back", I recommend straceto analyze how a process is interacting with the operating system.

如果进程“不回来”,我建议使用strace来分析进程与操作系统的交互方式。

In case of ls:

在 ls 的情况下:

$strace ls

you would have seen that it reads all directory entries (getdents(2)) before it actually outputs anything. (sorting… as it was already mentioned here)

您会看到它在实际输出任何内容之前读取所有目录条目(getdents(2))。(排序......正如这里已经提到的那样)

回答by wbkang

I'm assuming you are using GNU ls? try

我假设您正在使用 GNU ls?尝试

\ls

It will unalias the usual ls (ls --color=auto).

它将取消通常的 ls (ls --color=auto) 的别名。