aamadmi
aamadmi

Reputation: 2730

Linux: find command performance issue

I need the list of files in some directory and so I am using this command to find files on linux machine:

find /some_directory -depth -maxdepth 1 -mindepth 1 -exec basename {} \;

But it is taking a lot of time (~ 35 mins) as there are more than 200k files. Can this be optimized or is there a better alternative?

Upvotes: 0

Views: 2251

Answers (2)

John1024
John1024

Reputation: 113834

You can use -printf to emulate basename and this will eliminate the need for any additional processes:

find /some_directory -maxdepth 1 -mindepth 1 -printf '%f\n'

Upvotes: 2

DYZ
DYZ

Reputation: 57033

The low performance is due to the overhead of calling basename 200,000 times. Run find without the -exec option and pipe the output to a script in Python or some other good language that will convert path names to basenames, e.g.:

basename.py:

import os,sys
for line in sys.stdin:
    print(os.path.basename(line), end='')

Shell:

find /some_directory -maxdepth 1 -mindepth 1 | python basename.py

Upvotes: 2

Related Questions