Skip to content
Advertisement

What is the time complexity of ‘ls’ command when run on ext4 filesystem?

I have a couple hundred thousand files in one of the directory on a Redhat Linux server.

I need to execute ls -t to fetch the name of the last edited file and upload the filename to a database. Lately I have started facing problems as the number of files in the directory keeps growing and want to know the time complexity of 'ls' command when it is executed on ext4 filesystem.

Also, what is a better way to do it?

Advertisement

Answer

As you are running on linux os, you can simply use inotify tools. There are some very simple to use command line tools to monitor every change on the filesystem using the kernels inotify interface.

Example:

> inotifywait -m -e create -e modify test/ --format '%:e %f'

The above command waits endless (-m -> monitor ) for the event (-e ) create and modify of files inside test/ which is a directory and outputs with the formatted string where %e is replaced with the event and %f is replaced with the created or modified filename.

if you now run in a separate shell some checks like:

> touch test/newfile1
> touch test/newfile2
> echo hallo > test/newfile2

The output of the command is:

Setting up watches.
Watches established. 
CREATE newfile2
MODIFY newfile2

Quite easy to simply write the file name now to a database without reading the whole directory again.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement