LoginSignup
12
5

More than 1 year has passed since last update.

SFTPを使ってファイルのダウンロード/アップロードする

Last updated at Posted at 2022-01-26

はじめに

サーバーからファイルをダウンロードしたい。
macのファイルをサーバーへアップロードしたい。
そんな時は SFTP コマンドを使うと便利です。

SCP というファイルをやり取りするコマンドがありますが
昨今このコマンドは非推奨となっています。

SCP の代わりに使えるコマンドは SFTP や RSYNC です。
この記事では SFTP についてお伝えします。

概要

SFTPを使ってサーバーとファイルのアップロードやダウンロードを行う。

対象者

  • macでターミナルを使っている
  • ターミナルでファイルの操作が行える
  • テキストエディタを使える
  • シェルはzsh(最近のmacはデフォルトでzsh:2022年時点)

関連記事

目次

サーバー接続

sftp -i 鍵ファイルへのパス ユーザー@ホスト

サンプル

sftp -i ~/keys/hoge-key.pem hoge-user@hoge-server

ssh/configを設定していれば、以下のような感じで簡単に接続できます。

sftp hoge

設定の仕方はこちらの記事を参考にしてください。
「SSH接続をconfigファイルを使ってラクする」

接続するとこんな感じになります。

$ sftp -i ~/keys/hoge-key.pem hoge-user@hoge-server

Connected to XXXXXXXX
sftp> 

接続の終了

sftp> quit

現在のディレクトリ名を確認する

サーバー側の現在のディレクトリ名を確認する

sftp> pwd

ローカル側の現在のディレクトリ名を確認する

sftp> lpwd

現在のディレクトリの中身を確認する

サーバー側の現在のディレクトリの中身を確認する

sftp> ls

ローカル側の現在のディレクトリの中身を確認する

sftp> lls

ディレクトリを移動する

サーバー側でディレクトリを移動する

sftp> cd ディレクトリ名

ローカル側でディレクトリを移動する

sftp> lcd ディレクトリ名

ローカル側のコマンド

接頭辞に小文字のエル ”l” をつける。”local” の ”l”

sftp> lpwd
sftp> lls
sftp> lcd ディレクトリ名

サーバーからファイルをダウンロードする

すでにファイルが存在する場合は上書きされるので注意

ファイル指定

sftp> get ファイル名

ワイルドカードも使えます

例:拡張子がpngのファイルをカレントディレクトリからダウンロード

sftp> get *.png

ディレクトリ指定:サブディクレトリもダウンロード

sftp> get -r ディレクトリ名

macのファイルをサーバーへアップロードする

すでにファイルが存在する場合は上書きされるので注意

ファイル指定

サーバー側ローカル側共にカレントディクレトリ。pwd、lpwdで確認できる。

sftp> put ファイル名

ワイルドカードも使えます

例:拡張子がpngのファイルをmacのカレントディレクトリからアップロード

sftp> put *.png

ディレクトリ指定:サブディクレトリもアップロード

sftp> put -r ディレクトリ名

その他のコマンド

ヘルプコマンドがあります。

sftp> help

使えるコマンド一覧が表示されます。

sftp> help
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp [-h] grp path                Change group of file 'path' to 'grp'
chmod [-h] mode path               Change permissions of file 'path' to 'mode'
chown [-h] own path                Change owner of file 'path' to 'own'
df [-hi] [path]                    Display statistics for current directory or
                                   filesystem containing 'path'
exit                               Quit sftp
get [-afpR] remote [local]         Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-afpR] local [remote]         Upload file
pwd                                Display remote working directory
quit                               Quit sftp
reget [-fpR] remote [local]        Resume download file
rename oldpath newpath             Rename remote file
reput [-fpR] local [remote]        Resume upload file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

最後に

ターミナルでファイル操作ができる人は、SFTPでも同様に操作することができると思います。

アップロードやダウンロード時にすでにファイルが存在する場合にファイルが上書きされる点に注意して使いましょう。

12
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
5