跳到内容
🛠️ToolsShed

SFTP Commands Reference

完整的SFTP命令速查表,用于通过SSH进行文件传输。

27 命令

sftp连接

Connect to SFTP server

语法:

sftp [user@]host[:path]

示例:

sftp user@example.com
sftp -P连接

Connect on a specific port

语法:

sftp -P <port> user@host

示例:

sftp -P 2222 user@example.com
sftp -i连接

Connect using identity/key file

语法:

sftp -i <keyfile> user@host

示例:

sftp -i ~/.ssh/id_rsa user@example.com
exit / quit / bye连接

Close the SFTP connection

语法:

exit

示例:

exit
ls导航

List remote directory contents

语法:

ls [-l] [path]

示例:

ls -l /home/user
lls导航

List local directory contents

语法:

lls [path]

示例:

lls ~/Downloads
pwd导航

Print remote working directory

语法:

pwd

示例:

pwd
lpwd导航

Print local working directory

语法:

lpwd

示例:

lpwd
cd导航

Change remote directory

语法:

cd <path>

示例:

cd /var/www/html
lcd导航

Change local directory

语法:

lcd <path>

示例:

lcd ~/projects
get传输

Download file from remote to local

语法:

get <remote> [local]

示例:

get backup.tar.gz
get -r传输

Download directory recursively

语法:

get -r <dir> [local]

示例:

get -r /remote/dir ./local
put传输

Upload file from local to remote

语法:

put <local> [remote]

示例:

put index.html /var/www/
put -r传输

Upload directory recursively

语法:

put -r <dir> [remote]

示例:

put -r ./dist /var/www/
mget传输

Download multiple files matching pattern

语法:

mget <pattern>

示例:

mget *.log
mput传输

Upload multiple files matching pattern

语法:

mput <pattern>

示例:

mput *.jpg
reget传输

Resume an interrupted download

语法:

reget <remote> <local>

示例:

reget bigfile.zip bigfile.zip
rm管理

Remove remote file

语法:

rm <path>

示例:

rm /tmp/old.log
rmdir管理

Remove remote directory

语法:

rmdir <path>

示例:

rmdir /tmp/olddir
mkdir管理

Create remote directory

语法:

mkdir <path>

示例:

mkdir /var/www/uploads
rename管理

Rename or move remote file

语法:

rename <old> <new>

示例:

rename old.txt new.txt
chmod管理

Change remote file permissions

语法:

chmod <mode> <path>

示例:

chmod 644 index.html
chown管理

Change remote file owner

语法:

chown <owner> <path>

示例:

chown www-data file.php
df信息

Show remote disk usage

语法:

df [-h]

示例:

df -h
stat信息

Show file attributes

语法:

stat <path>

示例:

stat /etc/passwd
version信息

Show SFTP protocol version

语法:

version

示例:

version
help信息

Show all available commands

语法:

help

示例:

help

关于此工具

SFTP(SSH 文件传输协议)是通过 SSH 连接在计算机之间安全传输文件的方法。与 FTP 等较旧的协议不同,SFTP 提供端到端加密,保护您的数据免受拦截,并确保身份验证凭据的保密性。此参考指南将最常用的 SFTP 命令汇总在一处,使开发人员、系统管理员和 DevOps 工程师能够轻松执行文件操作,而无需记忆语法。

通过命令行使用 SFTP,您可以连接到远程服务器、浏览目录结构、上传和下载文件,以及管理权限——所有操作都通过安全隧道进行。无论您是在部署应用程序、备份配置还是传输日志,get、put、ls 和 cd 等 SFTP 命令都是工作流中的基本工具。速查表格式使您能够快速查找命令语法和选项,而无需在多个文档页面之间切换。

对于在安全性和可靠性至关重要的生产环境中使用远程服务器的任何人来说,此工具最具价值。DevOps 团队、系统管理员和开发人员可以通过拥有 SFTP 语法的快速参考而获益,尤其是在排查文件传输问题或为需要学习安全文件处理实践的新团队成员提供培训时。

常见问题

代码实现

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 run

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.