AnswerBun.com

What command in a SCRIPT will cause the next "ls" to list the files IMMEDIATELY?

Summary

What command can I run in a bash script that will make the next ls at a
terminal list the files in a directory immediately, instead of having to wait 10
seconds?

And see Questions 1 and 2 at end of "Full Details".

Full Details

I’ve got 700 video files (mp4) in a directory, ranging in size from 100 MB to
1GB. The average file size is 200 MB.

When I list them for the first time after boot (using ls in a terminal, or using
pcmanfm), it takes 10 seconds before the files are listed. Thereafter, when I
list them again (with ls or in pcmanfm), the files are listed almost
immediately.

So to try and get round this initial wait time, I ran the following command in a
script after bootup:

ls /path/to/vid-directory

… so that thereafter, when I list the directory with ls or in pcmanfm, the
files list immediately.

But strangely, running

ls /path/to/vid-directory

in the SCRIPT does NOT cause the NEXT ls or pcmanfm to list the files
immediately (they’re listed after 10 seconds). Also strangely, when the initial
script runs, the files are listed almost immediately.

So, running ls at a terminal seems to store the filenames in RAM cache, whereas
running ls in a script does not*.

Question 1: Why is the latter* happening?

Question 2: What command(s) should I run in my script that WILL cause the
next ls or pcmanfm to list the files immediately?

PS: Whilst doing the testing, I made sure that RAM was initially empty by
running

sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'

Unix & Linux Asked by dave99 on December 30, 2020

2 Answers

2 Answers

The only plausible difference between ls in a terminal and ls in a script is that you've aliased ls to pass an option that requires it not only to read the list of file names, but also to read the file's attributes. The option is probably --color. Reading the list of file names only requires reading from the directory itself, whereas reading the file's attributes requires accessing each individual file's inode (with an lstat system call). So when you run ls in that script, only the file names end up in the cache, not their attributes. The first time you run ls in the terminal or use a GUI file manager, the attributes need to be loaded.

Pass the same options to ls in your script that you use on the terminal.

10 seconds to list only 700 files is abnormally slow though. The size of the files is irrelevant. Typical modern systems only start getting noticeably slow with tens or hundreds of thousands of files if not more.

Correct answer by Gilles 'SO- stop being evil' on December 30, 2020

You can find out what the problem is by running after a reboot:

strace -tt -o ls.strace ls /path/to/directory

The output of ls can be accelerated by ls -U but, of course, that does change the output.

700 files is not a large number. Some acceleration may be achieved by rewriting all the directory entries:

mkdir /tmpdir/on/the/same/filesystem
mv * /tmpdir/on/the/same/filesystem
mv /tmpdir/on/the/same/filesystem/* .

But the most effective solution may be to have the directory be read automatically in the background at boot.

Answered by Hauke Laging on December 30, 2020

Add your own answers!

Related Questions

All processes running on the same core

1  Asked on December 24, 2020 by roofkiller

 

capturing yum update transaction to replicate on another machine

0  Asked on December 23, 2020 by dan-schneider

     

Add Support CRAM-MD5 or DIGEST-MD5 to SMTP server

0  Asked on December 22, 2020 by alpav99

         

Linux delete lines from ini-file where key has no value

1  Asked on December 21, 2020 by sunwave121

     

What caused traffic on port 631?

0  Asked on December 21, 2020 by u20200411

   

Bind/Unbind Linux Module Automatically

1  Asked on December 20, 2020 by vladius

       

ls everything that is _not_ a symlink

1  Asked on December 20, 2020

     

Netgear39 Removal

0  Asked on December 20, 2020 by danny-bender

     

Join two csv files by matching columns, join command

1  Asked on December 20, 2020 by cdxun

   

Arithmetic on values with memory size units

4  Asked on December 20, 2020 by malan

   

case while option not showing last argument in shell script

1  Asked on December 19, 2020 by user2358844

   

Debian update to Buster, iptables throwing an error in

1  Asked on December 19, 2020 by jademalo

     

Logitech Marble Mouse – Linux Scroll Modifier Setup

3  Asked on December 18, 2020 by peter-l

   

ZFS setup, adding drives on the go

0  Asked on December 18, 2020 by timyorgut

         

Linux Font “Lato” is not available to the JVM

0  Asked on December 18, 2020 by valene

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.