bash 从文件复制文件列表

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

Copy a list of files from a file

bashshellftp

提问by Pierre de LESPINAY

I have file containing a list of files separated by end of lines

我有包含由行尾分隔的文件列表的文件

$ cat file_list
file1
file2
file3

I want to copy this list of files with FTP

我想用 FTP 复制这个文件列表

How can I do that ? Do I have to write a script ?

我怎样才能做到这一点 ?我必须写一个脚本吗?

回答by Benj

You can turn your list of files into list of ftp commands easily enough:

您可以轻松地将文件列表转换为 ftp 命令列表:

(echo open hostname.host; 
 echo user username; 
 cat filelist | awk '{ print "put " ; }'; 
echo bye) > script.ftp

Then you can just run:

然后你可以运行:

ftp -s script.ftp

ftp -s script.ftp

Or possibly (with other versions of ftp)

或者可能(使用其他版本的 ftp)

ftp -n < script.ftp

ftp -n < script.ftp

回答by straycur

Thanks for the very helpful example of how to feed a list of files to ftp. This worked beautifully for me.

感谢有关如何将文件列表提供给 ftp 的非常有用的示例。这对我来说效果很好。

After creating my ftp script in Linux (CentOs 5.5), I ran the script with:

在 Linux (CentOs 5.5) 中创建我的 ftp 脚本后,我使用以下命令运行脚本:

ftp –n < ../script.ftp

My script (with names changed to protect the innocent) starts with:

我的脚本(改名以保护无辜者)以以下内容开头:

open <ftpsite>
user <userid> <passwd>
cd <remote directory>
bin
prompt
get <file1>
get <file2>

And ends with:

并以:

get <filen-1>
get <filen>
bye

回答by jkgeyti

Something along these lines - the somecommanddepends on what you want to do - I don't get that from your question, sorry.

沿着这些路线的东西 - 这 somecommand取决于你想做什么 - 我没有从你的问题中得到这一点,抱歉。

#!/bin/bash
# Iterate through lines in file
for line in `cat file.txt`;do
#your ftp command here do something    
 somecommand $line
done

edit: If you really want to persue this route for multiple files (you shouldn't!), you can use the following command in place of somecommand $line:

编辑:如果你真的想为多个文件坚持这条路线(你不应该!),你可以使用以下命令代替somecommand $line

ncftpput -m -u username -p password ftp.server.com /remote/folder $line

ncftpput propably also takes an arbitrary number of files to upload in one go, but I havn't checked it. Notice that this approach will connect and disconnect for every single file!

ncftpput 也可以一次性上传任意数量的文件,但我没有检查过。请注意,这种方法将连接和断开每个文件!