Get List of Files from Ftp Server

I'm trying to get a list of all the files we have on a server ( specifically every pdf file we have there ). I've tried using total commander and search for the files. It worked to some extent, as in, i got a list of every pdf we had there, but no way of exporting the results ( we have 100.000+ files there )

I've tried using a bash script to get the information, but i'm not very experienced with linux, and i don't really know what i'm doing.

My script looks like this :

#!/bin/bash
hostname="<host>"
ftp -i -nv $hostname << EOF
user <username> <password>
ls -R 
EOF

Running the above script i get

?Invalid command
501 Illegal PORT command
ftp: bind: Address already in use
221 Goodbye

Any help or pointing me on what to search would be greatly appreciated.

6 Answers

With curl, this is handy
curl ftp://yourftpserver/dir/ --user username:password

3

ncftpls ftp://yourftpserver/dir/*.pdf

Note that patterns such as *.pdf, etc. in the above command do work as expected.

For recursive, use -R. For more options, see man ncftpls.

ncftpls is provided by the ncftp package. For RHEL, this package is available in the epel repo.

2
curl ftp://user:password@<ip>/path/

The last / is a must if it is a directory. This worked in curl version 7.29.0

Try to configure ftp to use the PASV (passive) mode for data transfers. This done with the -p switch.

I'm not sure if you will be able to do a recursive file listing with this ftp-client. ls -R in my case just gave the list of files and directories in the current working dir. Maybe Recursive FTP directory listing in shell/bash with a single session (using cURL or ftp) will help you.

0

As mentionend in one of my comments, do not use user:password as plain text in your commands ever, otherwise you are running at risk of beeing history hjacked! Instead use at least a protected/restricted file with the username+password and substitute it in your command, e.g.:

ftp://yourftpserver/dir/ --user "$(cat .userpw)"

Whereas .userpw is your protected/restricted file with the example content: myusername:mypassword

I have always run this FTP script in a linux system, after login ls command is submitted to a ftp connection(passive). List of files is written to a stdout stream. Script is inline in a sh script file.

#!/bin/sh

HOST=11.22.33.44
USER=myuser
PWD="mypwd"

ftp -p -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PWD
ls
quit
END_SCRIPT
exit 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest