bash 从 FTP 服务器获取文件列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15706188/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:31:40  来源:igfitidea点击:

Get list of files from FTP server

bashftp

提问by Raz

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 )

我正在尝试获取我们在服务器上拥有的所有文件的列表(特别是我们在那里拥有的每个 pdf 文件)。我试过使用总指挥官并搜索文件。它在某种程度上有效,例如,我得到了我们在那里拥有的每个 pdf 的列表,但无法导出结果(我们那里有 100.000 多个文件)

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.

我试过使用 bash 脚本来获取信息,但我对 linux 不是很有经验,而且我真的不知道我在做什么。

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.

任何帮助或指出我要搜索的内容将不胜感激。

采纳答案by Patrick B.

Try to configure ftpto use the PASV (passive) mode for data transfers. This done with the -pswitch.

尝试配置ftp为使用 PASV(被动)模式进行数据传输。这是通过-p开关完成的。

I'm not sure if you will be able to do a recursive file listing with this ftp-client. ls -Rin 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.

我不确定您是否能够使用此 ftp 客户端进行递归文件列表。ls -R在我的情况下,只给出了当前工作目录中的文件和目录列表。也许使用单个会话(使用 cURL 或 ftp)在 shell/bash 中列出递归 FTP 目录会对您有所帮助。

回答by user1587276

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

卷曲,这很方便
curl ftp://yourftpserver/dir/--user username:password

回答by Acumenus

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

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

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

请注意,*.pdf上述命令中的 , 等模式确实按预期工作。

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

对于递归,使用-R. 有关更多选项,请参阅man ncftpls

ncftplsis provided by the ncftppackage. For RHEL, this package is available in the epelrepo.

ncftplsncftp包提供。对于 RHEL,这个包在epelrepo 中可用。

回答by Talespin_Kit

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

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

/如果是目录,最后一个是必须的。这适用于 curl 版本 7.29.0

回答by ioCron

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 hHymaned! Instead use at least a protected/restricted file with the username+password and substitute it in your command, e.g.:

正如我在评论中提到的那样,永远不要在命令中使用 user:password 作为纯文本,否则您将面临被历史劫持的风险!而是至少使用带有用户名+密码的受保护/受限文件并将其替换为您的命令,例如:

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

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

而 .userpw 是您的受保护/受限文件,其示例内容为:myusername:mypassword