SFTP Commands Reference
Complete SFTP commands cheat sheet for file transfer over SSH.
27 SftpCommandsReference.commands
sftpConnectionConnect to SFTP server
SftpCommandsReference.syntax:
sftp [user@]host[:path]Example:
sftp user@example.comsftp -PConnectionConnect on a specific port
SftpCommandsReference.syntax:
sftp -P <port> user@hostExample:
sftp -P 2222 user@example.comsftp -iConnectionConnect using identity/key file
SftpCommandsReference.syntax:
sftp -i <keyfile> user@hostExample:
sftp -i ~/.ssh/id_rsa user@example.comexit / quit / byeConnectionClose the SFTP connection
SftpCommandsReference.syntax:
exitExample:
exitlsNavigationList remote directory contents
SftpCommandsReference.syntax:
ls [-l] [path]Example:
ls -l /home/userllsNavigationList local directory contents
SftpCommandsReference.syntax:
lls [path]Example:
lls ~/DownloadspwdNavigationPrint remote working directory
SftpCommandsReference.syntax:
pwdExample:
pwdlpwdNavigationPrint local working directory
SftpCommandsReference.syntax:
lpwdExample:
lpwdcdNavigationChange remote directory
SftpCommandsReference.syntax:
cd <path>Example:
cd /var/www/htmllcdNavigationChange local directory
SftpCommandsReference.syntax:
lcd <path>Example:
lcd ~/projectsgetTransferDownload file from remote to local
SftpCommandsReference.syntax:
get <remote> [local]Example:
get backup.tar.gzget -rTransferDownload directory recursively
SftpCommandsReference.syntax:
get -r <dir> [local]Example:
get -r /remote/dir ./localputTransferUpload file from local to remote
SftpCommandsReference.syntax:
put <local> [remote]Example:
put index.html /var/www/put -rTransferUpload directory recursively
SftpCommandsReference.syntax:
put -r <dir> [remote]Example:
put -r ./dist /var/www/mgetTransferDownload multiple files matching pattern
SftpCommandsReference.syntax:
mget <pattern>Example:
mget *.logmputTransferUpload multiple files matching pattern
SftpCommandsReference.syntax:
mput <pattern>Example:
mput *.jpgregetTransferResume an interrupted download
SftpCommandsReference.syntax:
reget <remote> <local>Example:
reget bigfile.zip bigfile.ziprmManagementRemove remote file
SftpCommandsReference.syntax:
rm <path>Example:
rm /tmp/old.logrmdirManagementRemove remote directory
SftpCommandsReference.syntax:
rmdir <path>Example:
rmdir /tmp/olddirmkdirManagementCreate remote directory
SftpCommandsReference.syntax:
mkdir <path>Example:
mkdir /var/www/uploadsrenameManagementRename or move remote file
SftpCommandsReference.syntax:
rename <old> <new>Example:
rename old.txt new.txtchmodManagementChange remote file permissions
SftpCommandsReference.syntax:
chmod <mode> <path>Example:
chmod 644 index.htmlchownManagementChange remote file owner
SftpCommandsReference.syntax:
chown <owner> <path>Example:
chown www-data file.phpdfInfoShow remote disk usage
SftpCommandsReference.syntax:
df [-h]Example:
df -hstatInfoShow file attributes
SftpCommandsReference.syntax:
stat <path>Example:
stat /etc/passwdversionInfoShow SFTP protocol version
SftpCommandsReference.syntax:
versionExample:
versionhelpInfoShow all available commands
SftpCommandsReference.syntax:
helpExample:
helpHäufig gestellte Fragen
Code-Implementierung
import subprocess
import os
def sftp_connect(host: str, user: str, port: int = 22, key_file: str = None) -> list[str]:
"""Build an sftp command to connect to a remote host."""
cmd = ["sftp"]
if port != 22:
cmd.extend(["-P", str(port)])
if key_file:
cmd.extend(["-i", key_file])
cmd.append(f"{user}@{host}")
return cmd
def sftp_batch_commands(local_dir: str, remote_dir: str, files: list[str]) -> str:
"""Generate SFTP batch file commands for uploading multiple files."""
lines = [f"cd {remote_dir}", f"lcd {local_dir}"]
for f in files:
lines.append(f"put {f}")
lines.append("bye")
return "\n".join(lines)
# Example: create a batch file for SFTP upload
batch = sftp_batch_commands("/local/uploads", "/remote/data", ["a.csv", "b.csv"])
print("SFTP batch commands:")
print(batch)
# Write to batch file and run
with open("/tmp/sftp_batch.txt", "w") as bfile:
bfile.write(batch)
cmd = sftp_connect("example.com", "myuser") + ["-b", "/tmp/sftp_batch.txt"]
print("\nCommand:", " ".join(cmd))
# subprocess.run(cmd) # Uncomment to actually runComments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.