Is it possible to check if a file exists without making a request to hard disk in Linux?

StackOverflow https://stackoverflow.com/questions/8886081

  •  16-04-2021
  •  | 
  •  

Question

This is my first post here.

I want to check if a file exists but without using hard disk. For example look at this code:

<?php
$filename = '/path/to/foo.txt';

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?>

As far as I know ext4 is journaling file system. Does it mean that the script can check if the file exists by looking into it journal, that is main memory without touching hard disk?

Thanks for your time.

UPD.

Is there any filesystem that keeps hard disk's journal in main memory, so that file exist's operation can be done without accessing the hard disk?

Was it helpful?

Solution

The journal (a) lives on the disk, and (b) solves another problem entirely.

When you make a call like this, the kernel will ask the file system. If the necessary blocks of the file structure are cached in memory, it won't access the disk. If they're not, it will.

OTHER TIPS

No. A filesystem journal is not kept entirely in memory, and in any event does not contain all metadata in the file system. It's solely used as a tool to make recovery from an unclean unmount faster.

It will have to at least access the hard disk to see if it is there. Now actually opening the file? I would think not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top