Remote file system operations/file transfer over SSH2 session without SFTP
- File operations via SSH2 shell/command session (no SFTP required)
- Supports all common file system operations
- Both ESM and CJS exports
- TypeScript support
npm install ssh2-scpimport { Client } from 'ssh2'
import { createSshFs } from 'ssh2-scp'
const client = new Client()
client.on('ready', () => {
const fs = createSshFs(client)
// List directory
const files = await fs.list('/path/to/dir')
// Read file
const content = await fs.readFile('/path/to/file.txt')
// Write file
await fs.writeFile('/path/to/file.txt', 'Hello World')
// Get file stats
const stat = await fs.stat('/path/to/file')
console.log(stat.isFile(), stat.isDirectory(), stat.size)
// ... and more
})createSshFs(client)client- An authenticated ssh2 Client instance
readFile(remotePath)- Read file content as stringwriteFile(remotePath, content, mode?)- Write string content to filereadFileBase64(remotePath)- Read file as base64 encoded stringwriteFileBase64(remotePath, base64Content)- Write base64 content to file
list(remotePath)- List directory contentsmkdir(remotePath, options?)- Create directoryrmdir(remotePath)- Remove directory
cp(from, to)- Copy file or directorymv(from, to)- Move/rename file or directoryrename(oldPath, newPath)- Rename file or directoryrm(remotePath)- Remove filetouch(remotePath)- Create empty file or update timestamp
stat(remotePath)- Get file stats (follows symlinks)lstat(remotePath)- Get file stats (does not follow symlinks)realpath(remotePath)- Get canonical pathreadlink(remotePath)- Read symlink targetgetFolderSize(folderPath)- Get folder size and file count
chmod(remotePath, mode)- Change file permissions
getHomeDir()- Get home directoryrunExec(command)- Execute raw shell command
import { Transfer } from 'ssh2-scp/transfer'
const transfer = new Transfer(fs, {
type: 'download', // or 'upload'
remotePath: '/remote/path',
localPath: '/local/path',
chunkSize: 32768,
onProgress: (transferred, total) => {
console.log(`Progress: ${transferred}/${total}`)
}
})
await transfer.startTransfer()type- Transfer type:'download'or'upload'remotePath- Remote file/folder pathlocalPath- Local file/folder pathchunkSize- Chunk size for transfer (default: 32768)onProgress- Progress callback(transferred, total) => voidonData- Data callback(count) => void
MIT