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


![](/data/attachment/album/201512/16/154338zlee4piakx0ra4ke.jpg)


### 步骤 1: 建立 FTP 连接


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


#### 例如:



```
ftp domain.com

ftp 192.168.0.1

ftp user@ftpdomain.com

```

**注意: 本例中使用匿名服务器。**


替换下面例子中 IP 或域名为你的服务器地址。


![FTP 登录](/data/attachment/album/201512/16/154341nh3ff5iuw6t35v63.png)


### 步骤 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>

```

登录成功。


![FTP 登录成功](/data/attachment/album/201512/16/154341edii7w7qi575i933.png)


### 步骤 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.

```

![打印目录](/data/attachment/album/201512/16/154341pakaohk26w3jn499.png)


#### 改变目录:


改变目录可以输入：



```
ftp> cd directory

```

服务器将会返回：



```
250 Directory succesfully changed.

```

![FTP中改变目录](/data/attachment/album/201512/16/154344axwpz44944obbpp4.png)


### 步骤 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).

```

![使用FTP下载文件](/data/attachment/album/201512/16/154344h8xiccxo5nsii5zx.png)


下载多个文件可以使用通配符及 `mget` 命令。例如，下面这个例子我打算下载所有以 .xls 结尾的文件。



```
mget *.xls

```

### 步骤 5: 使用 FTP 上传文件


完成 FTP 连接后，FTP 同样可以上传文件


使用 `put`命令上传文件：



```
put file

```

当文件不再当前本地目录下的时候，可以使用绝对路径：



```
put /path/file

```

同样，可以上传多个文件：



```
mput *.xls

```

### 步骤 6: 关闭 FTP 连接


完成FTP工作后，为了安全起见需要关闭连接。有三个命令可以关闭连接：



```
bye
exit
quit

```

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



```
221 Goodbye

```

![](/data/attachment/album/201512/16/154346mejv995e3vh39tf9.png)


需要更多帮助，在使用 ftp 命令连接到服务器后，可以使用`help`获得更多帮助。


![](/data/attachment/album/201512/16/154349e2op76bzpzh4mm24.png)




---


via: <https://www.howtoforge.com/tutorial/how-to-use-ftp-on-the-linux-shell/>


译者：[VicYu](http://vicyu.net) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# How to use the Linux ftp command to up- and download files on the shell

FTP, or File Transfer Protocol, is a standard network protocol used for transferring files between a client and a server over a network, such as the internet. In the Linux shell, FTP is commonly used for uploading, downloading, and managing files on remote servers. It is useful because it enables users to securely transfer large amounts of data using FTPS (FTP over SSL/TLS), manage file permissions, and organize directories on remote systems directly from the command line. FTP is particularly valuable for server administrators, developers, and website managers who need to transfer files efficiently between local and remote machines. Additionally, its support for interactive sessions and automation through scripts makes it a flexible tool for file management tasks in the Linux environment.

In this tutorial, I will explain how to use the Linux ftp command on the shell. I will show you how to connect to an FTP server, up- and download files and create directories. While there are many nice desktops FTP clients available, the FTP command is still useful when you work remotely on a server over an SSH session and e.g. want to fetch a backup file from your FTP storage.

## Step 1: Establishing an FTP connection

To connect to the FTP server, we have to type in the terminal window 'ftp' and then the domain name 'domain.com' or IP address of the FTP server.

**Examples:**

ftp domain.com

ftp 192.168.0.1

ftp[[email protected]]

Note: for this example we used an anonymous server.

Replace the IP and domain in the above examples with the IP address or domain of your FTP server.

## Step 2: Login with User and Password

Most FTP servers logins are password protected, so the server will ask us for a 'username' and a 'password'.

If you connect to a so-called anonymous FTP server, then try to use "anonymous" as username and an empty password:

Name: anonymous

Password:

The terminal will return a message like this:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

ftp>

When you are logged in successfully.

## Step 3: Working with Directories

The commands to list, move and create folders on an FTP server are almost the same as we would use the shell locally on our computer, ls stands for list, cd to change directories, mkdir to create directories...

### Listing directories with security settings:

ftp> ls

The server will return:

200 PORT command successful. Consider using PASV.

150 Here comes the directory listing.

directory list

....

....

226 Directory send OK.

### Changing Directories:

To change the directory we can type:

ftp> cd directory

The server will return:

250 Directory succesfully changed.

## Step 4: Downloading files with FTP

Before downloading a file, we should set the local FTP file download directory by using 'lcd' command:

lcd /home/user/yourdirectoryname

If you dont specify the download directory, the file will be downloaded to the current directory where you were at the time you started the FTP session.

Now, we can use the command 'get' command to download a file, the usage is:

get file


The file will be downloaded to the directory previously set with the 'lcd' command.

The server will return the next message:

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).

To download several files we can use wildcards. In this example, I will download all files with the .xls file extension.

mget *.xls

## Step 5: Uploading Files with FTP


We can upload files that are in the local directory where we made the FTP connection.

To upload a file, we can use 'put' command.

put file

When the file that you want to upload is not in the local directory, you can use the absolute path starting with "/" as well:

put /path/file

To upload several files we can use the mput command similar to the mget example from above:

mput *.xls

## Step 6: Closing the FTP connection

Once we have done the FTP work, we should close the connection for security reasons. There are three commands that we can use to close the connection:

bye

exit

quit

Any of them will disconnect our PC from the FTP server and will return:

221 Goodbye

If you need some additional help, once you are connected to the FTP server, type 'help' and this will show you all the available FTP commands.