sftpRm()
Removes (unlinks) a file from the remote server using SFTP.
Signature
sftpRm(path: string, callback?: CallbackFunction<void>): Promise<void>
Parameters
The path of the file to remove on the remote server.
Optional callback function to handle the result or error.(error: any, response?: void) => void
Returns
A promise that resolves when the file is successfully removed, or rejects with an error if the operation fails.
Example
import SSHClient from '@dylankenneally/react-native-ssh-sftp';
// Using Promise
try {
await client.sftpRm('/home/user/file.txt');
console.log('File removed successfully');
} catch (error) {
console.error('Failed to remove file:', error);
}
// Using callback
client.sftpRm('/home/user/file.txt', (error) => {
if (error) {
console.error('Failed to remove file:', error);
return;
}
console.log('File removed successfully');
});
sftpRename()
Renames a file or directory on the remote server using SFTP.
Signature
sftpRename(oldPath: string, newPath: string, callback?: CallbackFunction<void>): Promise<void>
Parameters
The current path of the file or directory on the remote server.
The new path to rename the file or directory to.
Optional callback function to handle the result or error.(error: any, response?: void) => void
Returns
A promise that resolves when the file or directory is successfully renamed, or rejects with an error if the operation fails.
Example
import SSHClient from '@dylankenneally/react-native-ssh-sftp';
// Using Promise
try {
await client.sftpRename(
'/home/user/old-name.txt',
'/home/user/new-name.txt'
);
console.log('File renamed successfully');
} catch (error) {
console.error('Failed to rename file:', error);
}
// Using callback
client.sftpRename(
'/home/user/old-name.txt',
'/home/user/new-name.txt',
(error) => {
if (error) {
console.error('Failed to rename file:', error);
return;
}
console.log('File renamed successfully');
}
);
sftpChmod()
Changes the permissions of a file or directory on the remote server using SFTP.
This method is only available on Android. It will not work on iOS.
Signature
sftpChmod(path: string, permissions: number, callback?: CallbackFunction<void>): Promise<void>
Parameters
The path of the file or directory on the remote server.
The new permissions to set in octal format (e.g., 0o755 or 493).Common permission values:
0o755 (493): Read, write, execute for owner; read and execute for group and others
0o644 (420): Read and write for owner; read-only for group and others
0o600 (384): Read and write for owner only
Optional callback function to handle the result or error.(error: any, response?: void) => void
Returns
A promise that resolves when the permissions are successfully changed, or rejects with an error if the operation fails.
Example
import SSHClient from '@dylankenneally/react-native-ssh-sftp';
// Using Promise with octal notation
try {
await client.sftpChmod('/home/user/script.sh', 0o755);
console.log('Permissions changed successfully');
} catch (error) {
console.error('Failed to change permissions:', error);
}
// Using Promise with decimal notation
try {
await client.sftpChmod('/home/user/file.txt', 644);
console.log('Permissions changed successfully');
} catch (error) {
console.error('Failed to change permissions:', error);
}
// Using callback
client.sftpChmod('/home/user/script.sh', 0o755, (error) => {
if (error) {
console.error('Failed to change permissions:', error);
return;
}
console.log('Permissions changed successfully');
});
- Android: Fully supported
- iOS: Not supported