无法解压文件?
我编写了一个 shellscript,它尝试从 ftp 服务器中提取 tar 文件并在本地解压它。我需要从 tar 存档中提取特定文件。 tar 文件的文件名包含日期;我需要能够根据该日期选择一个 tar 文件。
abc_myfile_$date.tar
是我从 ftp 服务器提取的文件的格式。
我当前的代码如下所示:
for host in ftpserver
do
ftp -inv host <<END_SCRIPT
user username password
prompt
cd remotepath
lcd localpath
mget *myfile_$date*.tar
quit
END_SCRIPT
done
for next in `ls localpath/*.tar`
do
tar xvf $next *required_file_in_tar_file*.dat
done
当我运行脚本时,无法解压文件,
只有当我提到该文件的确切名称时,我才能从 ftp 服务器获取单个 tar 文件。我想获取一个名称中包含 myfile_$date
的文件。之后,我想将其提取到本地路径,以获取该 tar 文件中的指定文件,其名称由我的 required_files
组成。
I have written a shellscript which tries to pull a tar file from an ftp server and untar it locally. I need to extract specific files from the tar archive. The filename of the tarfile contains a date; I need to be able to select a tar file based on this date.
abc_myfile_$date.tar
is the format of the file I am pulling from the ftp server.
My current code looks like this:
for host in ftpserver
do
ftp -inv host <<END_SCRIPT
user username password
prompt
cd remotepath
lcd localpath
mget *myfile_$date*.tar
quit
END_SCRIPT
done
for next in `ls localpath/*.tar`
do
tar xvf $next *required_file_in_tar_file*.dat
done
when i run the script am not able to untar the files
I am able to get a single tar file from the ftp server only if I mention the exact name of that file. I would like to get a file which has myfile_$date
in its name. After this I would like to extract it to a local path to get the specified files in that tar file whose names consist of my required_files
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将获得
.tar
文件,但使用z
选项对其进行解压缩。压缩文件(需要z
的文件)通常具有.tar.gz
前缀。尝试You get the
.tar
file, but decompress it withz
option. Compressed files (those that requirez
) normally have.tar.gz
prefix. Try首先,如果您想对从服务器获取的文件名使用通配符,则需要使用 mget 而不是 get。通配符文件扩展(*)不适用于 get 命令。
一旦您提取了文件,tar 操作将按预期工作,大多数现代版本的 linux/bsd 都有一个“智能”tar,它不需要“z”命令来指定 tar 文件被压缩 - 它们会确定 tarball 是自行压缩的,并自动解压缩,前提是系统上有适当的压缩/解压缩工具(bzip2 用于 .jz 文件,gzip 用于 .gz 文件)。
Firstly, if you want to use wildcards for the file name that you're getting from the server you need to use mget instead of get. Wildcard file expansion (the *) does not work for the get command.
Once you have pulled the file the tar operation will work as expected, most modern versions of linux/bsd have a 'smart' tar, which doesn't need the 'z' command to specify that the tar file is compressed - they'll figure out that the tarball is compressed on their own and uncompress it automatically, providing the appropriate compression/decompression tool is on the system (bzip2 for .jz files, gzip for .gz files).
我不太确定,但是如果你想下载多个文件,FTP 协议没有命令
mget
吗? (而不是获取
)I'm not quite sure, but does the FTP protocol not have a command
mget
if you want to download multiple files? (instead ofget
)