42

I am bit confused with the different between stat fstat and lstat functions in node.js. Could someone help to understand what is the major difference btwn stat, fstat and lstat functions in node.js and When to use stat, fstat and lstat functions.

bharath
  • 845
  • 2
  • 11
  • 23
  • **FYI**: this follows [conventions in linux & c](https://stackoverflow.com/q/32895019/1366033) as well – KyleMit Oct 11 '20 at 17:08

1 Answers1

63
  • stat follows symlinks. When given a path that is a symlink, it returns the stat of the target of the symlink.
  • lstat doesn't follow symlinks. When given a path that is a symlink it returns the stat of the symlink and not its target.
  • fstat takes a file descriptor rather than a path.
Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • what does symbolic links means – bharath Sep 09 '15 at 11:52
  • 7
    They are a type of file that contain a path. Their target which is used instead when opening the file. They are created by the `ln` command or the `link(2)` function which is available in Node as `fs.link`. – Dan D. Sep 09 '15 at 11:58
  • 8
    @bharath they're like shortcuts in windows. – Omar Alshaker Dec 13 '16 at 10:35
  • @OmarAlshaker Sort of. Shortcuts in Windows are just files with a `.lnk` extension. Explorer knows how to read them, but most other software just sees them as a regular file (because they are). – Clonkex Oct 31 '22 at 02:06