使用与文件相同的名称但在不同的文件夹中创建tar [英] Create tar with same name as file but in different folder

查看:59
本文介绍了使用与文件相同的名称但在不同的文件夹中创建tar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文件夹中有多个文件.有什么方法可以从每个与文件同名的文件中创建单独的tar文件,并将这些tar文件保存在不同的文件夹中.

I have multiple files in a folder. Is there any way to create individual tar file from each of these file with same name as file and save those tar files in different folder.

例如,假设我在一个文件夹中有文件"file1","file2","file3",那么我想创建名为"file1.tar.gz","file2.tar.gz","file3.tar"的tar文件..gz"放在其他文件夹中.

e.g suppose I have files 'file1', 'file2', 'file3' in a folder then I want to create tar files named 'file1.tar.gz', 'file2.tar.gz', 'file3.tar.gz' in some different folder.

推荐答案

只需从目录内部运行以下简单的shell脚本即可:

Just run this simple shell script from inside your directory:

for file in *; do tar -czf $file.tar.gz $file; done

对于您所在目录中的每个文件,结果将是一个新的.tar.gz文件.如果要在另一个目录中创建tar归档文件,只需更改-cf标志后的路径即可.例如,如果您希望在与当前目录位于同一目录的另一个目录中创建档案,请写入:

The result will be a new .tar.gz file for each one of the files inside the directory you're in. If you wish to create the tar archives in another directory, just change the path that's right after the -cf flags. For example, if you wish to create the archives in another directory that's located in the same directory as your current one, write:

for file in *; do tar -czf ../<Other_Directory>/$file.tar.gz $file; done

这篇关于使用与文件相同的名称但在不同的文件夹中创建tar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆