Node.js stats.nlink Property from fs.Stats Class
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:
const fs = require( 'fs' );
fs.stat( './filename.txt' , (err, stats) => {
if (err) throw err;
console.log( "using stat: number of "
+ "hard-links for the file is "
+ stats.nlink);
});
fs.lstat( './filename.txt' , (err, stats) => {
if (err) throw err;
console.log( "using lstat: number of "
+ "hard-links for the file is "
+ stats.nlink);
});
fs.stat( './' , (err, stats) => {
if (err) throw err;
console.log( "using stat: number of "
+ "hard-links for the file is "
+ stats.nlink);
});
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:
const fs = require( 'fs' ).promises;
(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
Share your thoughts in the comments
Please Login to comment...