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
And see Questions 1 and 2 at end of "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
So to try and get round this initial wait time, I ran the following command in a
script after bootup:
… so that thereafter, when I list the directory with ls or in pcmanfm, the
files list immediately.
But strangely, running
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
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
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
1 Asked on December 23, 2020 by user3450148
0 Asked on December 23, 2020 by dan-schneider
0 Asked on December 22, 2020 by alpav99
1 Asked on December 21, 2020 by sunwave121
1 Asked on December 20, 2020 by vladius
1 Asked on December 20, 2020 by cdxun
3 Asked on December 19, 2020 by sss
1 Asked on December 19, 2020 by user2358844
1 Asked on December 19, 2020 by kryptic-coconut
1 Asked on December 19, 2020 by jademalo
3 Asked on December 18, 2020 by eduardo-lucio
3 Asked on December 18, 2020 by peter-l
0 Asked on December 18, 2020 by timyorgut
6 Asked on December 17, 2020 by mirage
Get help from others!