Open In App

Node.js fs. lchownSync() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The fs.lchownSync() method is used to synchronously change the owner and group of the given path, however, it does not dereference symbolic links if the path is one, unlike the fs.chownSync() method which does dereference the links to their path. 
The function accepts a user id and group id that can be used to set the respective owner and group. It does not return anything.
Syntax: 
 

fs.lchownSync( fd, uid, gid )

Parameters: This method accepts three 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.

Below examples illustrate the fs.lchownSync() method in Node.js:
Example 1: This example shows the setting of the owner and group using the lchownSync() method.
 

javascript




// Node.js program to demonstrate the
// fs.lchownSync() method
 
// Import the filesystem module
const fs = require('fs');
 
let filepath = "example_file.txt";
let symlinkpath = "symlinkFile";
 
// Create a symlink to the file
fs.symlinkSync(filepath, symlinkpath);
 
// Set the owner and group of the symbolic
// link to a new one
// New owner is "geeksforgeeks" with user id 1200
// New group is "editor" with group id 1201
fs.lchownSync(symlinkpath, 1200, 1201);
console.log("Given uid and gid set successfully");


Before Running the Code: 
 

xubuntu@xubuntu: ~/Desktop/fs-lchownSync$ ls -l
total 4
-rw-rw--w- 1 xubuntu xubuntu 4 Apr 26 05:10 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 05:15 index.js

Output of the Code: 
 

Given uid and gid set successfully

After Running the Code: 
 

xubuntu@xubuntu: ~/Desktop/fs-lchownSync$ ls -l
total 4
-rw-rw--w- 1 xubuntu xubuntu 4 Apr 26 05:10 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 05:15 index.js
lrwxrwxrwx 1 geeksforgeeks editor 16 Apr 26 09:15 symlinkFile -> example_file.txt

Example 2: This example shows the comparison between chownSync() and lchownSync() in dereferencing the symbolic link.
 

javascript




// Node.js program to demonstrate the
// fs.lchownSync() method
 
// Import the filesystem module
const fs = require('fs');
 
let filepath = "example_file.txt";
let symlinkpath1 = "symlinkFile1";
let symlinkpath2 = "symlinkFile2";
 
// Create two symlinks to the file
fs.symlinkSync(filepath, symlinkpath1);
fs.symlinkSync(filepath, symlinkpath2);
 
// Use lchownSync() on the first symlink
// New owner is "geeksforgeeks" with user id 1200
// New group is "owner" with group id 998
fs.lchownSync(symlinkpath1, 1200, 998);
console.log("lchownSync: uid and gid set successfully");
 
// Use chownSync() on the second symLink
// New owner is "sam" with user id 1100
// New group is "editor" with group id 1201
fs.chownSync(symlinkpath2, 1100, 1201);
console.log("chownSync: uid and gid set successfully");


Before Running the Code: 
 

xubuntu@xubuntu: ~/Desktop/fs-lchownSync$ ls -l
total 4
-rw-rw--w- 1 xubuntu xubuntu 6 Apr 26 09:15 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 777 Apr 26 09:16 index.js

Output of the Code: 
 

lchownSync: uid and gid set successfully
chownSync: uid and gid set successfully

After Running the Code: 
 

xubuntu@xubuntu: ~/Desktop/fs-lchownSync$ ls -l
total 4
-rw-rw--w- 1 sam editor 6 Apr 26 09:15 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 777 Apr 26 09:16 index.js
lrwxrwxrwx 1 geeksforgeeks owner 16 Apr 26 09:15 symlinkFile1 -> example_file.txt
lrwxrwxrwx 1 root root 16 Apr 26 09:15 symlinkFile2 -> example_file.txt

Reference: https://nodejs.org/api/fs.html#fs_fs_lchownsync_path_uid_gid
 



Last Updated : 10 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads