News for hackers. Stuff that matters.
Re:Linux Command One-Liners by Foivos on Thursday November 12, 2020 @09:38AM (#25070329)
>_ cat <file> > /dev/null

It mentions that this command loads the file to RAM for faster usage. I do not understand how this works. How do you access the "RAM version" of the file.

Also, I thought that /dev/null was just an "abyss" you would send unwanted outputs.

Re:Linux Command One-Liners by dspillett on Thursday November 12, 2020 @10:04AM (#25070604)
You can't specifically access the cached version, but the OS will (assuming the file isn't too large) remember the blocks read from disk in the cache for a while (until the memory is needed by something else) so that next time the file is ready those blocks do not need to be read from disk. This can make a huge difference on traditional drives.

/dev/null is the abyss, but the cat command will read all the blocks comprising the file from the filesystem before sending them to that abyss.

You can see the effect like so:

  dspillett@swann2:/tmp$ time cat a.bigish.file > /dev/null
  real    0m3.830s
  user    0m0.000s
  sys     0m0.468s
  dspillett@swann2:/tmp$ time cat a.bigish.file > /dev/null
  real    0m0.165s
  user    0m0.004s
  sys     0m0.164s
  dspillett@swann2:/tmp$ time cat a.bigish.file > /dev/null
  real    0m0.148s
  user    0m0.000s
  sys     0m0.144s
(if "a.bigish.file" has only just been created, it will likely already be in RAM, run

  sync; echo 3 > /proc/sys/vm/drop_caches
as root or other privileged user to clear the not-currently-active cache & buffers, or chose an older file) (note though that this clears all cache machine-wide, not just cached information from your current session)
Re:Linux Command One-Liners by Foivos on Thursday November 12, 2020 @10:27AM (#25070856)
Thank you for the detailed explanation.

So any interaction with the file would put it in memory will do. Of course assuming that you interact with the whole file.

So if you have a big file and use "head", I guess it will not be enough. But you use "less" and scroll the whole thing you achieve the same result as in the command above right?

Re:Linux Command One-Liners by dspillett on Thursday November 12, 2020 @10:36AM (#25070947)
Basically, yes.

There are some complications:

* If the file is large then blocks at the start will be dropped from cache as you move further in, or will quickly be pushed out by IO activity elsewhere on your system.

* Some commands open files with options that inhibit caching, so not everything will have this effect.

> Of course assuming that you interact with the whole file.

Reading some of the file will have the effect for that portion of the file, so if subsequent commands only touch the same parts then the effect is the same.

Re:Linux Command One-Liners by Foivos on Thursday November 12, 2020 @10:51AM (#25071103)
Great explanation! Thank you!
Re:Linux Command One-Liners by augusto-moura on Thursday November 12, 2020 @09:44AM (#25070396)
Maybe is relying on the linux caching the file in memory? Then if you read it again is already in memory.

If so I don't think it is particularly useful, given that using the file anyway already puts it into the cache for later use

And yes, /dev/null is a abyss and everything written in it is discarded

Re:Linux Command One-Liners by butterthebuddha on Thursday November 12, 2020 @09:45AM (#25070397)
Re:Linux Command One-Liners by rovr138 on Thursday November 12, 2020 @09:42AM (#25070368)
Because you read the file, until the ram doesn't get purged, the file will be cached there (assuming it all fits)