Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest way to get the list of files recursively contained in a directory?

I have a directory that contains millions of files spread out in a hierarchy of folders. This directory is stored on a large remote NFS filesystem. I'd like to retrieve the list of these files as fast as possible.

Is it possible to go faster than find . > list.txt? What factors affect speed? I'm using python, but any solution will go as long as it's fast.

like image 629
static_rtti Avatar asked Sep 14 '12 10:09

static_rtti


People also ask

How do I list all files in a directory recursively?

Try any one of the following commands to see recursive directory listing: ls -R : Use the ls command to get recursive directory listing on Linux. find /dir/ -print : Run the find command to see recursive directory listing in Linux. du -a . : Execute the du command to view recursive directory listing on Unix.

Which command is used to show the total list of files in each directory recursively?

Using the ls Command. The ls command lists the directories and files contained in a directory. The ls command with the -lR options displays the list (in long format) of the sub-directories in the current directory recursively.


1 Answers

On linux, this was the fastest for me. Use (bash) globbing and printf like this:

printf "%s\n" directory/**/file
printf "%s\x00" directory/**/filename-with-special-characters | xargs -0 command

Seems to be a lot faster than

find directory -name file

or

ls -1R directory | grep file

or even, surprisingly,

ls directory/**/file

This was a local file system though: x86_64 system, ext4 filesystem on an SSD, in a directory structure of over 600,000 directories with multiple files in them.

like image 159
LeighC Avatar answered Oct 14 '22 12:10

LeighC