Linux 如何tar一组文件?

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

How to tar a set of files?

linuxtar

提问by Li'

I am new to the tarcommand. I have many files in a directory: 1_1_1.txt, 1_1_2.txt, 2_1_1.txt, 2_1_2.txt..

我是这个tar命令的新手。我在一个目录中有很多文件: 1_1_1.txt, 1_1_2.txt, 2_1_1.txt, 2_1_2.txt..

I know there is a command:

我知道有一个命令:

tar cf file.tar *.txt

which will tarall the four files into file.tar. ButI need to tar 1_1_1.txtand 1_1_2.txtinto one tar file and 2_1_1.txtand 2_1_2.txtinto another tar file. How can I accomplish this?

这将tar所有四个文件转换为file.tar. 布提需要焦油1_1_1.txt1_1_2.txt成一个tar文件,并2_1_1.txt2_1_2.txt到另一个tar文件。我怎样才能做到这一点?

采纳答案by iwanek

To simply create a tarball of these files I would just do:

要简单地创建这些文件的 tarball,我会这样做:

tar cf ones.tar 1_*.txt
tar cf twos.tar 2_*.txt

Most likely you want to compress the tarballs, so use the zoption:

很可能您想压缩 tarball,因此请使用以下z选项:

tar czf ones.tar.gz 1_*.txt
tar czf twos.tar.gz 2_*.txt

View the contents of your tarballs with tar tf <tarball>.

查看 tarball 的内容tar tf <tarball>

回答by iwanek

Try this one:

试试这个:

find . -maxdepth 1 -name "*.txt" -exec tar -rf test.tar {} \;

find . -maxdepth 1 -name "*.txt" -exec tar -rf test.tar {} \;

If you want to include all files including subfolders, remove "-maxdepth 1"

如果要包含所有文件,包括子文件夹,请删除“-maxdepth 1”

you can use patterns like "1_1*.txt"

你可以使用像“1_1*.txt”这样的模式

回答by Aromal

You can simply use the below commands

您可以简单地使用以下命令

tar cf file1.tar 1_1_1.txt 1_1_2.txt

tar cf file2.tar 2_1_1.txt 2_1_2.txt