使用单个会话在 shell/bash 中递归 FTP 目录列表(使用 cURL 或 ftp)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2614319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursive FTP directory listing in shell/bash with a single session (using cURL or ftp)
提问by Timo
I am writing a little shellscript that needs to go through all folders and files on an ftp server (recursively). So far everything works fine using cURL - but it's pretty slow, becuase cURL starts a new session for every command. So for 500 directories, cURL preforms 500 logins.
我正在编写一个小 shellscript,它需要遍历 ftp 服务器上的所有文件夹和文件(递归)。到目前为止,使用 cURL 一切正常——但速度很慢,因为 cURL 为每个命令启动一个新会话。因此,对于 500 个目录,cURL 执行 500 次登录。
Does anybody know, whether I can stay logged in using cURL (this would be my favourite solution) or how I can use ftp with only one session in a shell script?
有谁知道,我是否可以使用 cURL 保持登录状态(这将是我最喜欢的解决方案),或者我如何在 shell 脚本中仅通过一个会话使用 ftp?
I know how to execute a set of ftp commands and retrieve the response, but for the recursive listing, it has to be a little more dynamic...
我知道如何执行一组 ftp 命令并检索响应,但是对于递归列表,它必须更加动态...
Thanks for your help!
谢谢你的帮助!
回答by freethinker
The command is actually ncftpls -R. It will recursively list all the files in a ftp folder.
命令实际上是ncftpls -R. 它将递归列出 ftp 文件夹中的所有文件。
回答by malat
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 servermay not implement ls -R. Simply replace 123.456.789.100 with the actual IP adress of the ftp server in the following examples:
只是总结一下其他人到目前为止所说的话。如果您正在尝试编写用作批处理文件的便携式 shell 脚本,那么您需要使用 lftp 解决方案,因为某些 FTP 服务器可能无法实现ls -R. 在以下示例中,只需将 123.456.789.100 替换为 ftp 服务器的实际 IP 地址:
$ lftp -c "open 123.456.789.100 && find -l && exit" > listing.txt
See the manpage of lftp, go to the findsection:
看到manlftp的页面,进入find部分:
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.
递归列出目录(默认为当前目录)中的文件。这可以帮助缺少 ls -R 支持的服务器。您可以重定向此命令的输出。
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:
但是,如果您有办法确定远程 ftp 服务器是否实现了对 的适当支持ls -lR,那么更好(=更快)的解决方案将是:
$ 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.
仅供参考,如果我执行第一个命令 (lftp+find),检索完整列表需要 0m55.384s,而如果我执行第二个命令 (ftp+ls-R),则需要 0m3.225s。
回答by Nitram Saneco
If it's possible, try usign lftp script:
如果可能,请尝试使用 lftp 脚本:
# lftp script "myscript.lftp"
open your-ftp-host
user username password
cd directory_with_subdirs_u_want_to_list
find
exit
Next thing u need is bash script to run this lftp command and write it to file:
接下来你需要的是 bash 脚本来运行这个 lftp 命令并将其写入文件:
#!/bin/bash
lftp -f myscript.lftp > myOutputFile
myOutputFilenow contains the full dump of directories.
myOutputFile现在包含目录的完整转储。
回答by Marian
You could connect to the ftp server in a manner that it accepts commands from stdin and writes to stdout. Create two named pipes ("fifos", man mkfifo), redirect stdin and stdout of the ftp command each to one of them. Then you can write commands to the stdin-connected-fifo and read them (line-by-line with bash's readfor example) from the stdout-fifo. Then use the results to see where you need to send another listing command (and print it or whatever you want to do)
您可以通过接受来自 stdin 的命令并写入 stdout 的方式连接到 ftp 服务器。创建两个命名管道(“fifos”,man mkfifo),将 ftp 命令的 stdin 和 stdout 重定向到其中之一。然后,您可以将命令写入 stdin-connected-fifo 并read从 stdout-fifo 中读取它们(例如,使用 bash 逐行读取)。然后使用结果来查看您需要发送另一个列表命令的位置(并打印它或您想做的任何事情)
In short: Not something bash scripting is suitable for :) (Until you find a tool that does what you want by itself of course)
简而言之:不是 bash 脚本适合的东西:)(直到你找到一个工具,它当然可以自己做你想做的事)
回答by ghostdog74
if you just want to create a listing of all files and folders, you can use sshinstead. Something like this (but check the documentation on correct usage)
如果您只想创建所有文件和文件夹的列表,则可以ssh改用。像这样的东西(但请检查有关正确用法的文档)
$ ssh user@host "ls -R /path"

