Linux 終端機下使用 `ftp` 指令進行檔案傳輸,是系統管理員、腳本自動化的基礎技能。本文以實戰範例完整展示連線、列表、上傳、下載、斷開等操作,並整理核心指令速查表。
-bash-3.00$ ftp rws60001rems.us.oracle.com
Connected to rws60001rems.us.oracle.com (139.185.21.141).
220 (vsFTPd 1.2.1)
Name (rws60001rems.us.oracle.com:app7740): xxxxx
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (139,185,21,141,216,189)
150 Here comes the directory listing.
-rw-r--r-- 1 60202384 60402384 524 Aug 08 00:37 temp1.out
226 Directory send OK.
ftp> pwd
257 "/slot/ems2384/appmgr"
ftp> ls temp1*
227 Entering Passive Mode (139,185,21,141,178,113)
150 Here comes the directory listing.
-rw-r--r-- 1 60202384 60402384 524 Aug 08 00:37 temp1.out
226 Directory send OK.
ftp> get temp1.out
local: temp1.out remote: temp1.out
227 Entering Passive Mode (139,185,21,141,126,148)
150 Opening BINARY mode data connection for temp1.out (524 bytes).
226 File send OK.
524 bytes received in 0.0017 secs (3e 02 Kbytes/sec)
ftp> put INVLTENT.plx_bk
local: INVLTENT.plx_bk remote: INVLTENT.plx_bk
227 Entering Passive Mode (139,185,21,141,109,145)
150 FILE: INVLTENT.plx_bk.1
226 File receive OK.
331776 bytes sent in 0.00457 secs (7.1e 04 Kbytes/sec)
ftp> bye
221 Goodbye.

ftp [hostname | ip-address]
# 例: ftp 10.18.34.115
# 接著輸入使用者名稱、密碼
| 指令 | 格式 | 說明 |
|---|---|---|
| get | get [remote-file] [local-file] | 下載單一檔案,可指定本地儲存檔名 |
| mget | mget [remote-files] | 下載多檔案 (支援萬用字元 *)、預設儲存至**本地當前目錄** |
範例:
# 下載單一檔案並重命名
ftp> get /rose/1.bmp 1.bmp
# 下載 /rose 目錄下所有檔案
ftp> cd /rose
ftp> mget *.*
| 指令 | 格式 | 說明 |
|---|---|---|
| put | put local-file [remote-file] | 上傳單一檔案,可指定遠端儲存路徑/檔名 |
| mput | mput local-files | 上傳多檔案 (支援萬用字元)、來源為**本地當前目錄** |
範例:
# 上傳並改名
ftp> put 1.bmp /rose/333.bmp
# 上傳本地所有 .bmp 至遠端 /rose
ftp> cd /rose
ftp> mput *.bmp
| 指令 | 說明 |
|---|---|
| cd <遠端目錄> | 切換遠端工作目錄 |
| lcd <本地目錄> | 切換**本地**工作目錄 (影響 get/put 來源目的地) |
| mkdir / rmdir | 建立/刪除遠端目錄 |
| delete / mdelete | 刪除遠端單一/多檔案 |
| rename | 重新命名遠端檔案 |
| binary / ascii | 設定傳輸模式 (二進位/文字),預設 binary |
| passive / passive | 切換被動/主動模式 (預設被動,穿透 NAT/防火牆) |
| prompt | 切換互動式確認 (mget/mput 逐檔詢問 y/n),關閉可批次無人值守 |
| bye / quit | 中斷連線並離開 ftp |
#!/bin/bash
HOST="ftp.example.com"
USER="user"
PASS="password"
REMOTE_DIR="/upload"
LOCAL_DIR="/data/backup"
cd "$LOCAL_DIR"
ftp -inv $HOST <
關鍵參數:`-i` 關閉互動提示、`-n` 禁用 `.netrc` 自動登入、`-v` 詳細輸出。
熟練掌握 `ftp` 指令集 (連線、cd/lcd、get/mget、put/mput、binary、prompt、bye) 並理解**本地當前目錄**對上傳/下載路徑的影響,是 Linux 伺服器自動化備份、檔案分發、跨主機同步的基石。生產環境建議改用 **SFTP (SSH File Transfer Protocol)** 或 **rsync over SSH**,獲得加密傳輸、斷點續傳、權限保留等更強大功能。