Open In App

Node.js fsPromises.chown() Method

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The fsPromises.chown() method is used to change the ownership of a file then resolves the Promise with no arguments upon success. The function accepts a user id and group id that can be used to set the respective owner and group.

Syntax:

fsPromises.chown( path, uid, gid)

Parameters: This method accepts threeparameters as mentioned above and described below:

  • path: It is a String, Buffer or URL that denotes the path of the file of which the owner and group has to be changed.
  • uid: It is an integer that denotes the user id that corresponds to the owner to be set.
  • gid: It is an integer that denotes the group id that corresponds to the group to be set.

Javascript




// Node.js program to demonstrate the 
// fsPromises.chown() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
let filepath = "example_file.txt"
  
// Set the owner to a new one keeping the group same 
// New owner is "GeeksforGeeks" with user id 4567
fsPromises.chown(filepath, 4567, 999 => { 
    console.log("uid and gid set successfully."); 
}); 


Before Running the Code:

xubuntu@xubuntu: ~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 xubuntu xubuntu 4 May 26 04:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:08 index.js

Output of the Code:

Given uid and gid set successfully
.

After Running the Code:

xubuntu@xubuntu: ~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 geeksforgeeks xubuntu 4 May 26 04:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:08 index.js

Example 2: This example shows the setting of the group.

Javascript




// Node.js program to demonstrate the 
// fsPromises.chown() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
let filepath = "example_file.txt"
  
// Set the owner and group both to a new one 
// New owner is "nitin" with owner id 8900 
// New group is "author" with group id 1024 
fsPromises.chown(filepath, 8900, 1024 => { 
  
console.log("uid and gid set successfully!"); 
  
}); 


Before Running the Code:

xubuntu@xubuntu: ~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 xubuntu xubuntu 4 May 26 04:19 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:19 index.js

Output of the Code:

Given uid and gid set successfully!

After Running the Code:

xubuntu@xubuntu: ~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 nitin author 4 May 26 04:19 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:19 index.js
 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads