Skip to content

Latest commit

 

History

History
243 lines (172 loc) · 5.02 KB

File metadata and controls

243 lines (172 loc) · 5.02 KB

Stat

Get full details on a file or directory.

Usage

var stat = require( '@stdlib/fs/stat' );

stat( path, clbk )

Asynchronously returns file system statistics for a file or directory.

The callback is invoked with an fs.Stats object containing methods and values describing the target path.

stat( __filename, onStat );

function onStat( error, stats ) {
    if ( error ) {
        throw error;
    }
    console.log( stats.isFile() );
    console.log( stats.isDirectory() );
    console.log( stats.isSymbolicLink() );
    console.log( stats.size );
    console.log( stats.mode );
    console.log( stats.atime );
}

stat( path, options, clbk )

Asynchronously returns file system statistics for a file or directory with options.

stat( __filename, {
    'bigint': true
}, onStat );

function onStat( error, stats ) {
    if ( error ) {
        throw error;
    }
    console.log( stats.size );
    console.log( stats.atimeNs );
}

Options:

  • bigint: boolean flag indicating whether numeric values should be returned as bigint. Default: false.
stat( __filename, onStat );

function onStat( error, stats ) {
    if ( error ) {
        throw error;
    }
    console.log( stats.isFile() );
}

stat.sync( path )

Synchronously returns file system statistics for a file or directory.

var out = stat.sync( __filename );
if ( out instanceof Error ) {
    throw out;
}
console.log( out.isFile() );
console.log( out.isSocket() );
console.log( out.size );
console.log( out.mtimeMs );

stat.sync( path, options )

Synchronously returns file system statistics for a file or directory with options.

var out = stat.sync( __filename, {
    'bigint': true
});
if ( out instanceof Error ) {
    throw out;
}
console.log( out.size );
console.log( out.birthtimeNs );

The returned fs.Stats instance provides these type-check methods:

  • isFile()
  • isDirectory()
  • isBlockDevice()
  • isCharacterDevice()
  • isFIFO()
  • isSocket()
  • isSymbolicLink()

The returned fs.Stats instance also provides values such as fs.Stats:

  • dev, ino, mode, nlink, uid, gid, rdev, size, blksize, blocks
  • atime, mtime, ctime, birthtime
  • atimeMs, mtimeMs, ctimeMs, birthtimeMs
  • atimeNs, mtimeNs, ctimeNs, birthtimeNs (available when bigint: true)
var out = stat.sync( __filename );
if ( out instanceof Error ) {
    throw out;
}
console.log( out.isFile() );

Examples

var stat = require( '@stdlib/fs/stat' );

var out = stat.sync( __filename );
if ( out instanceof Error ) {
    throw out;
}
console.log( out.isFile() );

stat( __filename, onStat );

function onStat( error, stats ) {
    if ( error ) {
        throw error;
    }
    console.log( stats.isFile() );
    console.log( stats.isDirectory() );
    console.log( stats.isBlockDevice() );
    console.log( stats.isCharacterDevice() );
    console.log( stats.isFIFO() );
    console.log( stats.isSocket() );
    console.log( stats.isSymbolicLink() );
    console.log( stats.size );
    console.log( stats.mode );
    console.log( stats.uid );
    console.log( stats.gid );
    console.log( stats.atimeMs );
    console.log( stats.mtimeMs );
    console.log( stats.ctimeMs );
    console.log( stats.birthtimeMs );
}

CLI

Usage

Usage: stat [options] <path>

Options:

  -h,    --help                Print this message.
  -V,    --version             Print the package version.

Notes

  • Relative paths are resolved relative to the current working directory.
  • Errors are written to stderr.
  • Results are written to stdout as JSON.