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

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

[{'answer_id': 40195, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 1, 'selected': False, 'text': '<p>How about <code>find ./ -type f</code> (which will find all files in the currently directory)?  Take off the <code>-type f</code> to find everything.</p>\n'}, {'answer_id': 40200, 'author': 'Ryan Ahearn', 'author_id': 75, 'author_profile': 'https://Stackoverflow.com/users/75', 'pm_score': 4, 'selected': False, 'text': '<p>Try using:</p>\n\n<pre><code>find . -type f -maxdepth 1\n</code></pre>\n\n<p>This will only list the files in the directory, leave out the <code>-type f</code> argument if you want to list files and directories.</p>\n'}, {'answer_id': 40201, 'author': 'toolkit', 'author_id': 3295, 'author_profile': 'https://Stackoverflow.com/users/3295', 'pm_score': 1, 'selected': False, 'text': "<p>Things to try:</p>\n\n<p>Check ls isn't aliased?</p>\n\n<pre><code>alias ls\n</code></pre>\n\n<p>Perhaps try find instead?</p>\n\n<pre><code>find . \\( -type d -name . -prune \\) -o \\( -type f -print \\)\n</code></pre>\n\n<p>Hope this helps.</p>\n"}, {'answer_id': 40202, 'author': 'Eric', 'author_id': 4277, 'author_profile': 'https://Stackoverflow.com/users/4277', 'pm_score': 3, 'selected': False, 'text': "<p>You can redirect output and run the ls process in the background.</p>\n\n<pre><code>ls &gt; myls.txt &amp;\n</code></pre>\n\n<p>This would allow you to go on about your business while its running.  It wouldn't lock up your shell.</p>\n\n<p>Not sure about what options are for running ls and getting less data back.  You could always run <code>man ls</code> to check.</p>\n"}, {'answer_id': 40204, 'author': 'wbkang', 'author_id': 2710, 'author_profile': 'https://Stackoverflow.com/users/2710', 'pm_score': 2, 'selected': False, 'text': "<p>I'm assuming you are using GNU ls? \ntry</p>\n\n<pre><code>\\ls\n</code></pre>\n\n<p>It will unalias the usual ls (ls --color=auto).</p>\n"}, {'answer_id': 40206, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 6, 'selected': False, 'text': "<pre><code>ls -U\n</code></pre>\n<p>will do the ls without sorting.</p>\n<p>Another source of slowness is <code>--color</code>. On some linux machines, there is a convenience alias which adds <code>--color=auto'</code> to the ls call, making it look up file attributes for each file found (slow), to color the display. This can be avoided by <code>ls -U --color=never</code> or <code>\\ls -U</code>.</p>\n"}, {'answer_id': 40301, 'author': 'Tanj', 'author_id': 4275, 'author_profile': 'https://Stackoverflow.com/users/4275', 'pm_score': 1, 'selected': False, 'text': '<p>What partition type are you using? </p>\n\n<p>Having millions of small files in one directory it might be a good idea to use JFS or ReiserFS which have better performance with many small sized files.</p>\n'}, {'answer_id': 40433, 'author': 'tonylo', 'author_id': 4071, 'author_profile': 'https://Stackoverflow.com/users/4071', 'pm_score': 0, 'selected': False, 'text': '<p>You should provide information about what operating system and the type of filesystem you are using. On certain flavours of UNIX and certain filesystems you might be able to use the commands <a href="http://www.unix.com/man-page/all/0/ff" rel="nofollow noreferrer"><code>ff</code></a> and <a href="http://www.unix.com/man-page/all/0/ncheck" rel="nofollow noreferrer"><code>ncheck</code></a> as alternatives.</p>\n'}, {'answer_id': 40496, 'author': 'Benedikt Waldvogel', 'author_id': 4308, 'author_profile': 'https://Stackoverflow.com/users/4308', 'pm_score': 2, 'selected': False, 'text': '<p>If a process "doesn\'t come back", I recommend <a href="http://linux.die.net/man/1/strace" rel="nofollow noreferrer">strace</a> to analyze how a process is interacting with the operating system.</p>\n\n<p>In case of ls:</p>\n\n<pre><code>$strace ls\n</code></pre>\n\n<p>you would have seen that it reads all directory entries (<a href="http://linux.die.net/man/2/getdents" rel="nofollow noreferrer">getdents(2)</a>) before it actually outputs anything. (sorting… as it was already mentioned here)</p>\n'}, {'answer_id': 41088, 'author': 'jj33', 'author_id': 430, 'author_profile': 'https://Stackoverflow.com/users/430', 'pm_score': -1, 'selected': False, 'text': '<p>Lots of other good solutions here, but in the interest of completeness:</p>\n\n<pre><code>echo *\n</code></pre>\n'}, {'answer_id': 75731, 'author': 'Jim', 'author_id': 12419, 'author_profile': 'https://Stackoverflow.com/users/12419', 'pm_score': -1, 'selected': False, 'text': "<p>You can also make use of <em>xargs</em>.  Just pipe the output of <em>ls</em> through <em>xargs</em>.</p>\n\n<pre><code>ls | xargs\n</code></pre>\n\n<p>If that doesn't work and the <em>find</em> examples above aren't working, try piping them to <em>xargs</em> as it can help the memory usage that might be causing your problems.</p>\n"}, {'answer_id': 1650700, 'author': 'Rich Homolka', 'author_id': 128439, 'author_profile': 'https://Stackoverflow.com/users/128439', 'pm_score': 1, 'selected': False, 'text': '<p>Some followup:\nYou don\'t mention what OS you\'re running on, which would help indicate which version of ls you\'re using.  This probably isn\'t a \'bash\' question as much as an ls question.  My guess is that you\'re using GNU ls, which has some features that are useful in some contexts, but kill you on big directories.</p>\n\n<p>GNU ls Trying to have prettier arranging of columns.  GNU ls tries to do a smart arrange of all the filenames.  In a huge directory, this will take some time, and memory.</p>\n\n<p>To \'fix\' this, you can try:</p>\n\n<p><code>ls -1</code> # no columns at all</p>\n\n<p>find BSD ls someplace, <a href="http://www.freebsd.org/cgi/cvsweb.cgi/src/bin/ls/" rel="nofollow noreferrer" title="FreeBSD ls source">http://www.freebsd.org/cgi/cvsweb.cgi/src/bin/ls/</a> and use that on your big directories.</p>\n\n<p>Use other tools, such as find</p>\n'}, {'answer_id': 3619973, 'author': 'telent', 'author_id': 419653, 'author_profile': 'https://Stackoverflow.com/users/419653', 'pm_score': 2, 'selected': False, 'text': '<p>This is probably not a helpful answer, but if you don\'t have <code>find</code> you may be able to make do with <code>tar</code></p>\n\n<pre><code>$ tar cvf /dev/null .\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 18464192, 'author': 'plasmafire', 'author_id': 2721409, 'author_profile': 'https://Stackoverflow.com/users/2721409', 'pm_score': 3, 'selected': False, 'text': '<p>This would be the fastest option AFAIK: <code>ls -1 -f</code>.</p>\n\n<ul>\n<li><code>-1</code> (No columns)</li>\n<li><code>-f</code> (No sorting)</li>\n</ul>\n'}, {'answer_id': 26295448, 'author': 'Pouya', 'author_id': 4083267, 'author_profile': 'https://Stackoverflow.com/users/4083267', 'pm_score': 1, 'selected': False, 'text': '<p>There are several ways to get a list of files:</p>\n\n<p>Use this command to get a list without sorting:</p>\n\n<pre><code>ls -U\n</code></pre>\n\n<p>or send the list of files to a file by using:</p>\n\n<pre><code>ls /Folder/path &gt; ~/Desktop/List.txt\n</code></pre>\n'}, {'answer_id': 26691147, 'author': 'Limalski', 'author_id': 4205706, 'author_profile': 'https://Stackoverflow.com/users/4205706', 'pm_score': 3, 'selected': False, 'text': '<p>Using </p>\n\n<pre><code>ls -1 -f \n</code></pre>\n\n<p>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)</p>\n\n<p>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.</p>\n\n<p>You can use my program find_if_more.pl in current directory by typing:</p>\n\n<pre><code>find_if_more.pl 999999999\n</code></pre>\n\n<p>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.</p>\n\n<pre><code>#!/usr/bin/perl\n    use warnings;\n    my ($maxcount) = @ARGV;\n    my $dir = \'.\';\n    $filecount = 0;\n    if (not defined $maxcount) {\n      die "Need maxcount\\n";\n    }\n    opendir(DIR, $dir) or die $!;\n    while (my $file = readdir(DIR)) {\n        $filecount = $filecount + 1;\n        last if $filecount&gt; $maxcount\n    }\n    print $filecount;\n    closedir(DIR);\n    exit 0;\n</code></pre>\n'}, {'answer_id': 26699902, 'author': 'Kannan Mohan', 'author_id': 1198887, 'author_profile': 'https://Stackoverflow.com/users/1198887', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>$ time tar cvf /dev/null . &amp;&gt; /tmp/file-count\n\nreal    37m16.553s\nuser    0m11.525s\nsys     0m41.291s\n\n------------------------------------------------------\n\n$ time echo ./* &amp;&gt; /tmp/file-count\n\nreal    0m50.808s\nuser    0m49.291s\nsys     0m1.404s\n\n------------------------------------------------------\n\n$ time ls &amp;&gt; /tmp/file-count\n\nreal    0m42.167s\nuser    0m40.323s\nsys     0m1.648s\n\n------------------------------------------------------\n\n$ time find . &amp;&gt; /tmp/file-count\n\nreal    0m2.738s\nuser    0m1.044s\nsys     0m1.684s\n\n------------------------------------------------------\n\n$ time ls -U &amp;&gt; /tmp/file-count\n\nreal    0m2.494s\nuser    0m0.848s\nsys     0m1.452s\n\n\n------------------------------------------------------\n\n$ time ls -f &amp;&gt; /tmp/file-count\n\nreal    0m2.313s\nuser    0m0.856s\nsys     0m1.448s\n\n------------------------------------------------------\n</code></pre>\n\n<p>To summarize the results</p>\n\n<ol>\n<li><code>ls -f</code> command ran a bit faster than <code>ls -U</code>. Disabling color might have caused this improvement.</li>\n<li><code>find</code> command ran third with an average speed of 2.738 seconds. </li>\n<li>Running just <code>ls</code> took 42.16 seconds. Here in my system <code>ls</code> is an alias for <code>ls --color=auto</code></li>\n<li>Using shell expansion feature with <code>echo ./*</code> ran for 50.80 seconds.</li>\n<li>And the <code>tar</code> based solution took about 37 miuntes.</li>\n</ol>\n\n<p>All tests were done seperately when system was in idle condition.</p>\n\n<p>One important thing to note here is that the file lists are not printed in the terminal rather\nthey were redirected to a file and the file count was calculated later with <code>wc</code> command.\nCommands ran too slow if the outputs where printed on the screen.</p>\n\n<p>Any ideas why this happens ?</p>\n'}, {'answer_id': 33785526, 'author': 'stu', 'author_id': 12386, 'author_profile': 'https://Stackoverflow.com/users/12386', 'pm_score': 4, 'selected': False, 'text': '<p>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</p>\n\n<pre><code>ls -1U\n</code></pre>\n'}, {'answer_id': 61469784, 'author': 'blacksite', 'author_id': 5015569, 'author_profile': 'https://Stackoverflow.com/users/5015569', 'pm_score': 0, 'selected': False, 'text': '<p>I had a directory with timestamps in the file names. I wanted to check the date of the latest file and found <code>find . -type f -maxdepth 1 | sort | tail -n 1</code> to be about twice as fast as <code>ls -alh</code>.</p>\n'}]
['https://Stackoverflow.com/questions/40193', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1536194/']