コンテンツへスキップ
🛠️ToolsShed

SFTPコマンドリファレンス

SSH経由のファイル転送のための完全なSFTPコマンドチートシート。

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コマンドを1つの場所にまとめ、開発者、システム管理者、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.