Skip to content

Instantly share code, notes, and snippets.

@Ugarz
Last active October 9, 2019 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ugarz/81bb71e4b7041d1751a6e3f00b8a4520 to your computer and use it in GitHub Desktop.
Save Ugarz/81bb71e4b7041d1751a6e3f00b8a4520 to your computer and use it in GitHub Desktop.
Manage folder and file rights

Manage folder and file rights with Node.js

var fs = require('fs'),
    path = require('path'),
    assert = require('assert'),
    dir = 'tmp_'+Date.now(),
    file = dir+'/file.txt';

var existsSync = fs.existsSync || path.existsSync;

console.log('working on dir:', dir);

fs.mkdirSync(dir);
console.log('Folder created')
fs.writeFileSync(file, 'asdf');
console.log('File created')
fs.chmodSync(file, '0444'); // -r--r--r--
console.log('Rights modified')
fs.unlinkSync(file);
console.log('Delete file')

assert.equal(existsSync(file), false); // file should be erased since dir is writeable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment