如何在linux命令行中使用ftp命令上传和下载文件

这里介绍在 Linux shell 中如何使用 ftp 命令。包括如何连接 FTP 服务器,上传或下载文件以及创建文件夹。尽管现在有许多不错的 FTP 桌面应用,但是在服务器、SSH、远程会话中命令行 ftp 命令还是有很多应用的。比如。需要服务器从 ftp 仓库拉取备份。

步骤一:建立FTP连接

想要连接 FTP 服务器,在命令上中先输入ftp然后空格跟上 FTP 服务器的域名 'domain.com' 或者 IP 地址

ftp domain.com
ftp 192.168.0.1
ftp user@ftpdomain.com

注意: 本例中使用匿名服务器。
替换下面例子中 IP 或域名为你的服务器地址。

步骤 2: 使用用户名密码登录

绝大多数的 FTP 服务器是使用密码保护的,因此这些 FTP 服务器会询问'username'和'password'.
如果你连接到被称作匿名 FTP 服务器(LCTT 译注:即,并不需要你有真实的用户信息即可使用的 FTP 服务器称之为匿名 FTP 服务器),可以尝试anonymous作为用户名以及使用空密码:

Name: anonymous
Password:

之后,终端会返回如下的信息

230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>

表示登录成功。

步骤 3: 目录操作

FTP 命令可以列出、移动和创建文件夹,如同我们在本地使用我们的电脑一样。ls可以打印目录列表,cd可以改变目录,mkdir可以创建文件夹。
使用安全设置列出目录
ftp> ls
服务器将返回:

200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
directory list
....
....
226 Directory send OK.

改变目录

ftp> cd directory

服务器将返回:

250 Directory succesfully changed.

步骤 4: 使用 FTP 下载文件

在下载一个文件之前,我们首先需要使用lcd命令设定本地接受目录位置。

lcd /home/user/yourdirectoryname

如果你不指定下载目录,文件将会下载到你登录 FTP 时候的工作目录。
现在,我们可以使用命令 get 来下载文件,比如:
get file

文件会保存在使用lcd命令设置的目录位置。
服务器返回消息:

local: file remote: file
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for file (xxx bytes).
226 File send OK.
XXX bytes received in x.xx secs (x.xxx MB/s).

使用 put命令上传文件:
put file
当文件不再当前本地目录下的时候,可以使用绝对路径:
put /path/file
同样,可以上传多个文件:
mput *.xls

步骤 6: 关闭 FTP 连接

bye
exit
quit

任意一个命令可以断开FTP服务器连接并返回:

221 Goodbye

需要更多帮助,在使用 ftp 命令连接到服务器后,可以使用help获得更多帮助。via: https://www.howtoforge.com/tutorial/how-to-use-ftp-on-the-linux-shell/

案例:从NCBI下载igblast安装包。

$ftp ftp.ncbi.nih.gov
Connected to ftp.wip.ncbi.nlm.nih.gov.
220-
This warning banner provides privacy and security notices consistent with
applicable federal laws, directives, and other federal guidance for accessing
this Government system, which includes all devices/storage media attached to
this system. This system is provided for Government-authorized use only.
Unauthorized or improper use of this system is prohibited and may result in
disciplinary action and/or civil and criminal penalties. At any time, and for
any lawful Government purpose, the government may monitor, record, and audit
your system usage and/or intercept, search and seize any communication or data
transiting or stored on this system. Therefore, you have no reasonable
expectation of privacy. Any communication or data transiting or stored on this
system may be disclosed or used for any lawful Government purpose.
220 FTP Server ready.
Name (ftp.ncbi.nih.gov:dushiyi): anonymous

上面输入Name;

331 Anonymous login ok, send your complete email address as your password
Password:

230 Anonymous access granted, restrictions apply
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls

200 PORT command successful
150 Opening BINARY mode data connection for file list
dr-xr-xr-x   4 ftp      anonymous     4096 Jul 22 02:48 1000genomes
-r--r--r--   1 ftp      anonymous 107375230976 Nov 17  2017 100GB
-r--r--r--   1 ftp      anonymous 10738466816 Nov 17  2017 10GB
-r--r--r--   1 ftp      anonymous 1074790400 Nov 17  2017 1GB
-r--r--r--   1 ftp      anonymous 53688139776 Nov 17  2017 50GB
-r--r--r--   1 ftp      anonymous 5369757696 Nov 17  2017 5GB
-r--r--r--   1 ftp      anonymous     2037 Oct 12  2017 README.ftp
dr-xr-xr-x   8 ftp      anonymous     4096 Feb  8  2016 SampleData
lr--r--r--   1 ftp      anonymous       29 Oct 12  2017 asn1-converters -> toolbox/ncbi_tools/converters
dr-xr-xr-x  12 ftp      anonymous   184320 Nov 22  2014 bigwig
dr-xr-xr-x   5 ftp      anonymous     4096 Jul 21 15:10 bioproject
dr-xr-xr-x   2 ftp      anonymous     4096 Jul 21 09:20 biosample
dr-xr-xr-x  10 ftp      anonymous     4096 Jul 22 02:48 blast
dr-xr-xr-x   3 ftp      anonymous     4096 Sep 13  2004 cgap
dr-xr-xr-x   4 ftp      anonymous     4096 Oct  3  2014 cn3d
dr-xr-xr-x  34 ftp      anonymous     4096 Dec 14  2018 dbgap
dr-xr-xr-x   2 ftp      anonymous     4096 Sep 21  2017 diffexpIR-notebook
dr-xr-xr-x  12 ftp      anonymous     4096 Jul 18  2013 entrez
dr-xr-xr-x   7 ftp      anonymous     4096 Oct 13  2011 epigenomics
dr-xr-xr-x   4 ftp      anonymous     4096 Sep 19  2017 eqtl
dr-xr-xr-x   6 ftp      anonymous     4096 Aug  4  2006 fa2htgs
-r--r--r--   1 ftp      anonymous     3262 Oct 12  2017 favicon.ico
dr-xr-xr-x  13 ftp      anonymous   258048 Jul 22 02:48 genbank
dr-xr-xr-x   6 ftp      anonymous     4096 Feb 28  2018 gene
dr-xr-xr-x 561 ftp      anonymous    45056 Jul 22 02:48 genomes
dr-xr-xr-x 1073741824 ftp      anonymous        0 Jul 21 18:22 geo
dr-xr-xr-x   4 ftp      anonymous     4096 Jul 22 02:48 giab
dr-xr-xr-x  25 ftp      anonymous     4096 Sep 20  2011 hapmap
dr-xr-xr-x   9 ftp      anonymous     4096 Jul  2  2018 hmm
dr-xr-xr-x  15 ftp      anonymous     4096 Mar 22 15:50 mmdb
dr-xr-xr-x   7 ftp      anonymous    86016 Jun 20 18:26 ncbi-asn1
dr-xr-xr-x   2 ftp      anonymous        6 Oct 12  2017 nist-immsa
dr-xr-xr-x   3 ftp      anonymous     8192 Jul  2 16:32 osiris
dr-xr-xr-x   9 ftp      anonymous     4096 Jul 22 02:48 pathogen
dr-xr-xr-x 162 ftp      anonymous     8192 Apr 19 16:07 pub
dr-xr-xr-x  28 ftp      anonymous     4096 Jul 22 02:48 pubchem
dr-xr-xr-x  21 ftp      anonymous     4096 May 13 15:10 pubmed
dr-xr-xr-x  21 ftp      anonymous     4096 Jul 12 16:56 refseq
dr-xr-xr-x  57 ftp      anonymous     4096 Jul 22 02:48 repository
-r--r--r--   1 ftp      anonymous       26 Oct 12  2017 robots.txt
dr-xr-xr-x   3 ftp      anonymous     4096 Jul 22 02:48 seqc
dr-xr-xr-x   6 ftp      anonymous     4096 Jun  4 14:50 sequin
dr-xr-xr-x   4 ftp      anonymous     4096 Jun 23  2016 sky-cgh
dr-xr-xr-x   8 ftp      anonymous     4096 Jul 22 02:48 snp
dr-xr-xr-x  23 ftp      anonymous     4096 Jul 22 02:48 sra
dr-xr-xr-x   2 ftp      anonymous     4096 Sep 29  2004 tech-reports
dr-xr-xr-x  11 ftp      anonymous     4096 Jul 25  2013 toolbox
dr-xr-xr-x   6 ftp      anonymous     4096 Sep  5  2018 tpa
dr-xr-xr-x   5 ftp      anonymous     4096 Aug  9  2016 variation
226 Transfer complete

上面列出当前目录下内容,并且进入指定目录。

ftp> cd blast/executables/igblast/release/
250 CWD command successful
ftp> ls
200 PORT command successful
150 Opening BINARY mode data connection for file list
dr-xr-xr-x   2 ftp      anonymous     4096 Nov  1  2018 1.10.0
dr-xr-xr-x   2 ftp      anonymous     4096 Nov  1  2018 1.11.0
dr-xr-xr-x   2 ftp      anonymous     4096 Nov 29  2018 1.12.0
dr-xr-xr-x   2 ftp      anonymous     4096 Mar  7 19:49 1.13.0
dr-xr-xr-x   2 ftp      anonymous     4096 Apr 29 16:52 1.14.0
dr-xr-xr-x   2 ftp      anonymous     4096 Mar 11  2014 1.3.0
dr-xr-xr-x   2 ftp      anonymous     4096 Jul 30  2014 1.4.0
dr-xr-xr-x   2 ftp      anonymous     4096 Nov  1  2016 1.5.0
dr-xr-xr-x   2 ftp      anonymous     4096 Nov  1  2016 1.6.1
dr-xr-xr-x   2 ftp      anonymous     4096 Oct 13  2017 1.7.0
dr-xr-xr-x   2 ftp      anonymous     4096 Oct 13  2017 1.8.0
dr-xr-xr-x   2 ftp      anonymous     4096 Sep  6  2018 1.9.0
lr--r--r--   1 ftp      anonymous        7 Apr 29 17:40 LATEST -> 1.14.0/
-r--r--r--   1 ftp      anonymous       86 Oct 13  2017 README
dr-xr-xr-x   3 ftp      anonymous     4096 Apr 17 16:59 database
-r--r--r--   1 ftp      anonymous      624 Feb  1  2018 edit_imgt_file.pl
dr-xr-xr-x   8 ftp      anonymous     4096 Apr 29  2014 internal_data
dr-xr-xr-x   3 ftp      anonymous     4096 Apr 29  2014 optional_file
226 Transfer complete
ftp> cd 1.14.0
250 CWD command successful
ftp> ls
200 PORT command successful
150 Opening BINARY mode data connection for file list
-r--r--r--   1 ftp      anonymous     3478 Apr 30 16:15 ChangeLog
-r--r--r--   1 ftp      anonymous 17687750 Apr 29 16:52 ncbi-igblast-1.14.0+.dmg
-r--r--r--   1 ftp      anonymous       59 Apr 29 16:52 ncbi-igblast-1.14.0+.dmg.md5
-r--r--r--   1 ftp      anonymous 14713607 Apr 29 16:52 ncbi-igblast-1.14.0-1.src.rpm
-r--r--r--   1 ftp      anonymous       64 Apr 29 16:52 ncbi-igblast-1.14.0-1.src.rpm.md5
-r--r--r--   1 ftp      anonymous 24002792 Apr 29 16:52 ncbi-igblast-1.14.0-1.x86_64.rpm
-r--r--r--   1 ftp      anonymous       67 Apr 29 16:52 ncbi-igblast-1.14.0-1.x86_64.rpm.md5
-r--r--r--   1 ftp      anonymous 18402750 Apr 29 16:52 ncbi-igblast-1.14.0-src.tar.gz
-r--r--r--   1 ftp      anonymous       65 Apr 29 16:52 ncbi-igblast-1.14.0-src.tar.gz.md5
-r--r--r--   1 ftp      anonymous 22317024 Apr 29 16:52 ncbi-igblast-1.14.0-src.zip
-r--r--r--   1 ftp      anonymous       62 Apr 29 16:52 ncbi-igblast-1.14.0-src.zip.md5
-r--r--r--   1 ftp      anonymous 12628211 Apr 29 16:52 ncbi-igblast-1.14.0-win64.exe
-r--r--r--   1 ftp      anonymous       64 Apr 29 16:52 ncbi-igblast-1.14.0-win64.exe.md5
-r--r--r--   1 ftp      anonymous 40771400 Apr 29 16:52 ncbi-igblast-1.14.0-x64-linux.tar.gz
-r--r--r--   1 ftp      anonymous       71 Apr 29 16:52 ncbi-igblast-1.14.0-x64-linux.tar.gz.md5
-r--r--r--   1 ftp      anonymous 23297125 Apr 29 16:52 ncbi-igblast-1.14.0-x64-macosx.tar.gz
-r--r--r--   1 ftp      anonymous       72 Apr 29 16:52 ncbi-igblast-1.14.0-x64-macosx.tar.gz.md5
-r--r--r--   1 ftp      anonymous 16309449 Apr 29 16:52 ncbi-igblast-1.14.0-x64-win64.tar.gz
-r--r--r--   1 ftp      anonymous       71 Apr 29 16:52 ncbi-igblast-1.14.0-x64-win64.tar.gz.md5
226 Transfer complete
ftp> get ncbi-igblast-1.14.0-x64-linux.tar.gz
local: ncbi-igblast-1.14.0-x64-linux.tar.gz remote: ncbi-igblast-1.14.0-x64-linux.tar.gz
200 PORT command successful
150 Opening BINARY mode data connection for ncbi-igblast-1.14.0-x64-linux.tar.gz (40771400 bytes)
226 Transfer complete
40771400 bytes received in 16.39 secs (2.3726 MB/s)

下载完成。

mget * # 下载所有
lcd  #修改下载路径
prompt off #关闭
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容