23

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.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Raz
  • 682
  • 1
  • 7
  • 19

7 Answers7

48

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

user1587276
  • 906
  • 6
  • 4
7

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.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
  • Thank you @A-B-B, I didn't know about this utility. Now I can do this to get the name of the latest `coreutils` source code tarball: `ncftpls -x "-t" ftp://ftp.gnu.org/gnu/coreutils/coreutils*.xz|head -1` – Setaa Jun 19 '19 at 22:09
  • A shame this does not use .netrc – mckenzm Jun 19 '21 at 07:43
7
curl ftp://user:password@<ip>/path/

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

Talespin_Kit
  • 20,830
  • 29
  • 89
  • 135
6

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.

Community
  • 1
  • 1
Patrick B.
  • 11,773
  • 8
  • 58
  • 101
4

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

ioCron
  • 873
  • 8
  • 9
3

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
Whome
  • 10,181
  • 6
  • 53
  • 65
0
curl --list-only ftp://user:password@ftp.adress/directory/JPG/

with curl request lists files