SFTPコマンドリファレンス
SSH経由のファイル転送のための完全なSFTPコマンドチートシート。
27 SftpCommandsReference.commands
sftp接続Connect to SFTP server
SftpCommandsReference.syntax:
sftp [user@]host[:path]例:
sftp user@example.comsftp -P接続Connect on a specific port
SftpCommandsReference.syntax:
sftp -P <port> user@host例:
sftp -P 2222 user@example.comsftp -i接続Connect using identity/key file
SftpCommandsReference.syntax:
sftp -i <keyfile> user@host例:
sftp -i ~/.ssh/id_rsa user@example.comexit / quit / bye接続Close the SFTP connection
SftpCommandsReference.syntax:
exit例:
exitlsナビゲーションList remote directory contents
SftpCommandsReference.syntax:
ls [-l] [path]例:
ls -l /home/userllsナビゲーションList local directory contents
SftpCommandsReference.syntax:
lls [path]例:
lls ~/DownloadspwdナビゲーションPrint remote working directory
SftpCommandsReference.syntax:
pwd例:
pwdlpwdナビゲーションPrint local working directory
SftpCommandsReference.syntax:
lpwd例:
lpwdcdナビゲーションChange remote directory
SftpCommandsReference.syntax:
cd <path>例:
cd /var/www/htmllcdナビゲーションChange local directory
SftpCommandsReference.syntax:
lcd <path>例:
lcd ~/projectsget転送Download file from remote to local
SftpCommandsReference.syntax:
get <remote> [local]例:
get backup.tar.gzget -r転送Download directory recursively
SftpCommandsReference.syntax:
get -r <dir> [local]例:
get -r /remote/dir ./localput転送Upload file from local to remote
SftpCommandsReference.syntax:
put <local> [remote]例:
put index.html /var/www/put -r転送Upload directory recursively
SftpCommandsReference.syntax:
put -r <dir> [remote]例:
put -r ./dist /var/www/mget転送Download multiple files matching pattern
SftpCommandsReference.syntax:
mget <pattern>例:
mget *.logmput転送Upload multiple files matching pattern
SftpCommandsReference.syntax:
mput <pattern>例:
mput *.jpgreget転送Resume an interrupted download
SftpCommandsReference.syntax:
reget <remote> <local>例:
reget bigfile.zip bigfile.ziprm管理Remove remote file
SftpCommandsReference.syntax:
rm <path>例:
rm /tmp/old.logrmdir管理Remove remote directory
SftpCommandsReference.syntax:
rmdir <path>例:
rmdir /tmp/olddirmkdir管理Create remote directory
SftpCommandsReference.syntax:
mkdir <path>例:
mkdir /var/www/uploadsrename管理Rename or move remote file
SftpCommandsReference.syntax:
rename <old> <new>例:
rename old.txt new.txtchmod管理Change remote file permissions
SftpCommandsReference.syntax:
chmod <mode> <path>例:
chmod 644 index.htmlchown管理Change remote file owner
SftpCommandsReference.syntax:
chown <owner> <path>例:
chown www-data file.phpdf情報Show remote disk usage
SftpCommandsReference.syntax:
df [-h]例:
df -hstat情報Show file attributes
SftpCommandsReference.syntax:
stat <path>例:
stat /etc/passwdversion情報Show SFTP protocol version
SftpCommandsReference.syntax:
version例:
versionhelp情報Show all available commands
SftpCommandsReference.syntax:
help例:
helpよくある質問
コード実装
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.