How to use FTP from the command line?
This guide is giving examples of how to use the ftp
command line tool to:
- Connect to a FTP server
- Basic of navigations
- Upload a file to the FTP server
- Download a file from a FTP server
- Other tips and tricks
Connect to a server from the CLI
~/$ ftp -i example.com
Connected to example.com.
Name (example.com:mickael): anonymous
331 Password required for example.com.
Password:
230 User anonymous logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
Connected to example.com.
Name (example.com:mickael): anonymous
331 Password required for example.com.
Password:
230 User anonymous logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
Navigate and access a folder
ftp> ls
200 PORT command successful.
150 Opening ASCII mode data connection for file list
drwx------ 7 web site 4096 Jun 9 07:12 .
drwx------ 7 web site 4096 Jun 9 07:12 ..
-rw-r--r-- 1 web site 312 Jun 12 2017 .htaccess
drwx------ 3 web site 4096 Jul 5 2020 public
226 Transfer complete.
ftp> cd public
250 CWD command successful.
ftp> pwd
257 "/public" is current directory.
200 PORT command successful.
150 Opening ASCII mode data connection for file list
drwx------ 7 web site 4096 Jun 9 07:12 .
drwx------ 7 web site 4096 Jun 9 07:12 ..
-rw-r--r-- 1 web site 312 Jun 12 2017 .htaccess
drwx------ 3 web site 4096 Jul 5 2020 public
226 Transfer complete.
ftp> cd public
250 CWD command successful.
ftp> pwd
257 "/public" is current directory.
Create a local file
ftp> lpwd
Local directory is /home/mickael/
ftp> lcd Documents
Local directory now /home/mickael/Documents
ftp> !ls
/bin/bash
ftp> !touch test.txt
/bin/bash
ftp> !ls
/bin/bash
test.txt
Local directory is /home/mickael/
ftp> lcd Documents
Local directory now /home/mickael/Documents
ftp> !ls
/bin/bash
ftp> !touch test.txt
/bin/bash
ftp> !ls
/bin/bash
test.txt
Create / Delete a folder
ftp> mkdir test
257 "/trash" - Directory successfully created
ftp> rmdir test
250 RMD command successful
257 "/trash" - Directory successfully created
ftp> rmdir test
250 RMD command successful
Upload a file to the FTP server
ftp> put test.txt
---> PORT 192,168,100,33,147,71
200 PORT command successful.
---> STOR test.txt
150 Opening BINARY mode data connection for test.txt
226 Transfer complete
---> PORT 192,168,100,33,147,71
200 PORT command successful.
---> STOR test.txt
150 Opening BINARY mode data connection for test.txt
226 Transfer complete
Download a file from the FTP server
ftp> get test.txt
---> TYPE I
200 Type set to I
---> PORT 192,168,100,33,156,59
200 PORT command successful.
---> RETR test.txt
150 Opening BINARY mode data connection for test.txt
226 Transfer complete.
---> TYPE I
200 Type set to I
---> PORT 192,168,100,33,156,59
200 PORT command successful.
---> RETR test.txt
150 Opening BINARY mode data connection for test.txt
226 Transfer complete.
Delete a file
ftp> delete test.txt
---> DELE test.txt 250 DELE command successful.
---> DELE test.txt 250 DELE command successful.
From a script
You might not want to always be the one typing commands, it can be handy to get everything done from say a bash script. You can make it happen like this:
$ ftp -n ftp.gnu.org <<EOF
quote USER anonymous
quote PASS whatever
cd /
get welcome.msg
quit
EOF
quote USER anonymous
quote PASS whatever
cd /
get welcome.msg
quit
EOF
Other tips and tricks
-
the
ftp
command can quickly reach its limits if you need to operate on folder with subfolders to either delete something recursively or copy some folders over. The lftp tool support all those use case. -
exit the server:
ftp> quit
~/$
~/$
- the “Not connected” error. To solve it you need to reopen your FTP like this:
ftp> pwd
Not connected.
ftp> open
Connected to example.com.
Name (example.com:mickael): anonymous
230 User anonymous logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/" is current directory.
Not connected.
ftp> open
Connected to example.com.
Name (example.com:mickael): anonymous
230 User anonymous logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/" is current directory.