Friday, May 3, 2024
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 1423  / 2 Years ago, sat, october 15, 2022, 8:06:13

I have a directory containing 0.5 million text files, and I want to open one or more with vim to check the text pattern. How can I get same ten file names quickly to open as samples? All the file names are unknown.



I've tried ls directory, only to find that it's too slow to finish listing all the files. Please don't provide me with answers which need checking time stamps or others like randomly or something else. Here the point is time. As fast as possible.



I get to know from @the_velour_fog that there is a direct solution using vim (:args `find . -type f | head -n10`, if someone doesn't need to run vim with plugins like nerdtree and etc this method is highly recommended) but please give me a Ubuntu answer.


More From » command-line

 Answers
4

If your main concern is time and you want to list any 10 files, you should look at the -U option which means to list entries without sorting.



So you can do:



ls -U


For 10 files only, assuming no filenames with newline(s) or any unusual characters:



ls -U | head -10





A bit better would be to do this only by shell itself:



printf '%s
' *


For 10 files:



printf '%s
' * | head -10





Here is the timing stat on my system for 2000 files on my zsh:



( ls --color=auto foobar* | head -10; )  0.044 total
( ls --color=auto -U foobar* | head -10; ) 0.020 total
( printf '%s
' foobar* | head -10; ) 0.013 total


Also note that, some of these might well trigger ARG_MAX if you have many files in the directory, in that case use need to use some other ways like find or a shell loop.


[#14024] Sunday, October 16, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ncharm

Total Points: 255
Total Questions: 105
Total Answers: 118

Location: Virgin Islands (U.S.)
Member since Sat, May 6, 2023
1 Year ago
;