VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-fs-fchown-function/

⇱ Node.js fs.fchown() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js fs.fchown() Function

Last Updated : 8 Oct, 2021
The fs.fchown() method is used to change the owner and group of the given file descriptor. The function accepts a user id and group id that can be used to set the respective owner and group. It has a callback function that returns any error that may occur if the function fails. Syntax:
fs.fchown( fd, uid, gid, callback )
Parameters: This method accepts four parameters as mentioned above and described below:
  • fd: It is an integer which denotes the file descriptor of the file of which the owner and group has to be changed.
  • uid: It is a number that denotes the user id that corresponds to the owner to be set.
  • gid: It is a number that denotes the group id that corresponds to the group to be set.
  • callback: It is a function that would be called when the method is executed.
    • err: It is an error that would be thrown if the method fails.
Below examples illustrate the fs.fchown() method in Node.js: Example 1: This example shows the setting of the owner. Before Running the Code:
xubuntu@xubuntu: ~/Desktop/fs-fchown$ ls -l
total 8
-rw-rw--w- 1 xubuntu xubuntu 4 Apr 26 02:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 02:08 index.js
Output of the Code:
Given uid and gid set successfully
After Running the Code:
xubuntu@xubuntu: ~/Desktop/fs-fchown$ ls -l
total 8
-rw-rw--w- 1 geek xubuntu 4 Apr 26 02:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 02:08 index.js
Example 2: This example shows the setting of the group. Before Running the Code:
xubuntu@xubuntu: ~/Desktop/fs-fchown$ ls -l
total 8
-rw-rw--w- 1 xubuntu xubuntu 4 Apr 26 02:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 02:08 index.js
Output of the Code:
Given uid and gid set successfully
After Running the Code:
xubuntu@xubuntu: ~/Desktop/fs-fchown$ ls -l
total 8
-rw-rw--w- 1 raj author 4 Apr 26 02:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 02:08 index.js
Reference: https://nodejs.org/api/fs.html#fs_fs_fchown_fd_uid_gid_callback
Comment
Article Tags:

Explore