lftpの使い方

目次

lftpというコマンドラインでファイル転送が行えるプログラムの使い方メモ。 lftpはFTPS(FTP over SSL)に対応しているため安全にファイル転送を行うことができます。

ここで使用するOSは"Ubuntu 20.04.2 LTS"です。

lftpのインストール

公式ページはhttps://lftp.yar.ru/です。

インストールはAPTで行えます。

$ sudo apt install lftp

インストール結果を確認します。

$ lftp --version
LFTP | Version 4.8.4 | Copyright (c) 1996-2017 Alexander V. Lukyanov

LFTP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with LFTP.  If not, see <http://www.gnu.org/licenses/>.

Send bug reports and questions to the mailing list <lftp@uniyar.ac.ru>.

Libraries used: GnuTLS 3.6.13, idn2 2.2.0, Readline 8.0, zlib 1.2.11

lftpの設定

LFTPでFTPSを使うための設定を行います。

自分のホームディレクトリに".lftprc"ファイルを作成し以下を設定します。

set ftp:ssl-auth TLS
set ftp:ssl-force true
set ftp:ssl-allow true
set ftp:ssl-protect-list true
set ftp:ssl-protect-data true
set ftp:ssl-protect-fxp true
設定項目 説明
ftp:ssl-auth (string) AUTHコマンドのデフォルトをTLS(Transport Layer Security)にします。
ftp:ssl-force (boolean) trueの場合、サーバーがSSLをサポートしていないときに、パスワードを(暗号でなく)平文で送信することを拒否します。
ftp:ssl-allow (boolean) trueの場合、アクセスのためにSSL接続することを交渉します。
ftp:ssl-protect-list (boolean) trueの場合、ファイルリスト転送のためにSSL接続を要求します。
ftp:ssl-protect-data (boolean) trueの場合、データ転送のためにSSL接続を要求します。
ftp:ssl-protect-fxp (boolean) trueの場合、FXP(File eXchange Protocol)モードの2つのFTPサーバー間でのデータ転送のためにSSL接続を要求します。

サーバーに接続する

端末でlftpを実行して接続します。

$ lftp [-d] [-u ユーザー名] [サーバー]

$ lftp -u username ftp://destination.com

-dオプションで詳細なデバッグログを表示することができます。

パスワードを同時に指定することもできます。

$ lftp [-u ユーザー名,パスワード] [サーバー]

しかし、パスワードがコマンド履歴に残ってしまうのでやらない方が良いです。

基本的なコマンド

lftpは対話モードでコマンドを使用することができます。

open サーバー
    サーバーにFTP接続します。

close
    サーバーから切断します。

pwd
    サーバーのカレントディレクトリを表示します。

lpwd
    ローカルのカレントディレクトリを表示します。

cd ディレクトリ名
    サーバーでディレクトリに移動します。

lcd ディレクトリ名
    ローカルでディレクトリに移動します。

ls [ディレクトリ名]
    サーバーのファイルを一覧表示します。表示したいディレクトリ名を指定することもできます。

find [ディレクトリ名]
    ファイルを再帰的に一覧表示します。

mkdir ディレクトリ名
    サーバーにディレクトリを作成します。

put ファイル名
    サーバーにファィルをアップロードする。

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

rm ファイル名
    サーバーのファイルを削除する。

cat ファイル名
    ファイルの内容を表示します。

mirror -R
    ローカルのカレントディレクトリの内容をサーバーのカレントディレクトリへアップロードする。

exit
    lftpを終了します。

help
    ヘルプを表示します。

ファイルアップロードの例

lftpでFTPサーバーへファイルをアップロードしてみます。

$ lftp -u username ftp://server.com <<<lftpでユーザーを指定してサーバーへ接続
パスワード:  <<<FTPサーバーのパスワードを入力します
lftp username@server.com:~> pwd <<<サーバーのカレントディレクトリを確認
ftp://username@server.com
lftp username@server.com:~> cd www <<<サーバーのカレントディレクトリをアップロード先に変更
lftp username@server.com:~/www> lpwd <<<ローカルのカレントディレクトリを確認
/home/izumi
lftp username@server.com:~/www> lcd blog/public <<<サーバーのカレントディレクトリをアップロード元に変更
lcd 成功、ローカルの cwd=/home/izumi/blog/public
lftp username@server.com:~/www> put index.html <<<サーバーへファイルをアップロード
6093 bytes transferred
lftp username@server.com:~/www> ls <<<サーバーのファイル一覧を確認
drwxr-xr-x   2 username users         512 May 17 15:05 .
drwx---r-x  10 username users         512 Apr 19 21:25 ..
-rw----r--   1 username users        6093 May 16 20:49 index.html
lftp username@server.com:~/www> close <<<サーバーと切断
lftp username@server.com:~/www> exit <<<lftpを終了
$

まとめてアップロードする

mirrorコマンドで/blog/publicディレクトリの内容をまとめてwebサーバーへアップロードすることができます。

$ lftp -u username ftp://server.com
lftp username@server.com:~> cd www
cd 成功、cwd=/home/username/www
lftp username@server.com:~/www> lcd blog/public
lcd 成功、ローカルの cwd=/home/izumi/blog/public
lftp username@server.com:~/www> mirror -R <<<サイトのファイルそのままサーバーへアップロード
合計: 24 個のディレクトリ、31 個のファイル、0 個のシンボリックリンク]
新規: 30 個のファイル、0 個のシンボリックリンク
136884 バイト転送済、17 秒経過 (8.1 KiB/s)
lftp username@server.com:~/www> close
lftp username@server.com:~/www> exit
$ 

findコマンドでサーバーのファイルとディレクトリを確認することができます。

lftp username@server.com:~/www> find
./
./404.html
./categories/
./categories/blog/
./categories/blog/index.html
./categories/blog/index.xml
./categories/blog/page/
./categories/blog/page/1/
./categories/blog/page/1/index.html
./categories/index.html
./categories/index.xml
./categories/page/
./categories/page/1/
./categories/page/1/index.html
<-- 以下略 -->