Recursive FTP directory listing in shell/bash with a single session (using cURL or ftp) Recursive FTP directory listing in shell/bash with a single session (using cURL or ftp) curl curl

Recursive FTP directory listing in shell/bash with a single session (using cURL or ftp)


The command is actually ncftpls -R. It will recursively list all the files in a ftp folder.


Just to summarize what others have said so far. If you are trying to write a portable shell script which works as batch file, then you need to use the lftp solution since some FTP server may not implement ls -R. Simply replace 123.456.789.100 with the actual IP adress of the ftp server in the following examples:

$ lftp -c "open 123.456.789.100 && find -l && exit" > listing.txt

See the man page of lftp, go to the find section:

List files in the directory (current directory by default) recursively. This can help with servers lacking ls -R support. You can redirect output of this command.

However if you have a way to figure out whether or not the remote ftp server implements proper support for ls -lR, then a much better (=faster) solution will be:

$ echo ls -lR | ftp 123.456.789.100 > listing.txt

Just for reference if I execute the first command (lftp+find) it takes 0m55.384s to retrieve the full listing, while if I execute the second one (ftp+ls-R), it takes 0m3.225s.


If it's possible, try usign lftp script:

# lftp script "myscript.lftp"open your-ftp-hostuser username passwordcd directory_with_subdirs_u_want_to_listfindexit

Next thing u need is bash script to run this lftp command and write it to file:

#!/bin/bashlftp -f myscript.lftp > myOutputFile

myOutputFile now contains the full dump of directories.