Open In App

Node.js stats.nlink Property from fs.Stats Class

Improve
Improve
Like Article
Like
Save
Share
Report

The stats.nlink property is an inbuilt application programming interface of the fs.Stats class which is used to get the number of hard-links for the file.

Syntax:

stats.nlink;

Return Value: It returns a number or BigInt value which represents the number of hard-links for the file.

Below examples illustrate the use of stats.nlink property in Node.js:

Example 1:




// Node.js program to demonstrate the   
// stats.nlink property
  
// Accessing fs module
const fs = require('fs');
  
// Calling fs.Stats stats.nlink for 
// files using stat
fs.stat('./filename.txt', (err, stats) => {
  if (err) throw err;
  console.log("using stat: number of "
    + "hard-links for the file is "
    + stats.nlink);
});
   
// Using lstat
fs.lstat('./filename.txt', (err, stats) => {
  if (err) throw err;
  console.log("using lstat: number of "
    + "hard-links for the file is "
    + stats.nlink);
});
  
// For directories
// Using stat
fs.stat('./', (err, stats) => {
  if (err) throw err;
  console.log("using stat: number of "
    + "hard-links for the file is "
    + stats.nlink);
});
   
// Using lstat
fs.lstat('./', (err, stats) => {
  if (err) throw err;
  console.log("using lstat: number of "
    + "hard-links for the file is "
    + stats.nlink);
});


Output:

using stat: number of hard-links for the file is  1
using lstat: number of hard-links for the file is  1
using stat: number of hard-links for the file is  1
using lstat: number of hard-links for the file is  1

Example 2:




// Node.js program to demonstrate the   
// stats.nlink property
  
// Accessing fs module
const fs = require('fs').promises;
  
// Calling fs.Stats stats.nlink
(async () => {
    const stats = await fs.stat('./filename.txt');
    console.log("using stat synchronous: number "
        + "of hard-links for the file is "
        + stats.nlink);
})().catch(console.error)


Output:

(node:13780) ExperimentalWarning: The fs.promises API 
is experimental 
using stat synchronous: number of hard-links for the file is 1

Note: The above program will compile and run by using the node filename.js command and use the file_path correctly.

Reference: https://nodejs.org/api/fs.html#fs_stats_nlink



Last Updated : 25 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads